unity是跨平台的,一般都是在PC ,Andriod 和IOS用的比较多。
平台判断代码:
#if UNITY_ANDROID
Debug.Log("这里是安卓设备");
#endif
#if UNITY_IPHONE
Debug.Log("这里是苹果设备");
#endif
#if UNITY_STANDALONE_WIN
Debug.Log("我是从Windows的电脑上运行的");
#endif
或者是写这种:
if (Application.platform == RuntimePlatform.Android ||
Application.platform == RuntimePlatform.IPhonePlayer)
{
}
转自http://blog.csdn/alayeshi/article/details/52872962
|
以后我们能够依据如上宏定义就能够去轻而易举的非常easy去在我们代码中增加推断了。我举个简单样例,例如以下:
- using UnityEngine;
- using System.Collections;
- public class Recompile : MonoBehaviour
- {
- private string platform = string.Empty;
- // Use this for initialization
- void Start()
- {
- DebugPlatformMesaage();
- }
- void DebugPlatformMesaage()
- {
- #if UNITY_EDITOR
- platform = "hi,大家好,我是在unity编辑模式下";
- #elif UNITY_XBOX360
- platform="hi,大家好,我在XBOX360平台";
- #elif UNITY_IPHONE
- platform="hi,大家好,我是IPHONE平台";
- #elif UNITY_ANDROID
- platform="hi,大家好,我是ANDROID平台";
- #elif UNITY_STANDALONE_OSX
- platform="hi,大家好,我是OSX平台";
- #elif UNITY_STANDALONE_WIN
- platform="hi,大家好,我是Windows平台";
- #endif
- Debug.Log("Current Platform:" + platform);
- }
- }
上面假设我是在Editor状态下的话,就能看见打印出:
我们也能够自定义宏定义,在PlayerSetting中定义:
比如我在上面圈起来的地方填写一个CUSTOM_ITF这个预编译指令。然后在DebugPlatformMesaage函数尾部增加这句话
- //新加入的内容
- #if CUSTOM_ITF
- customMsg = "我自己定义了预编译";
- #endif
- Debug.Log(customMsg);
然后我们执行看下,你就能在控制台看见我们输出的信息了:
当我们把我们自己定义的宏定义给去掉的时候,我们打印的信息就不会出来。
除了这些,unity中还有各个版本号的宏定义,一般开发插件的大牛都会用到。
UNITY_2_6 | Platform define for the major version of Unity 2.6. |
UNITY_2_6_1 | Platform define for specific version 1 from the major release 2.6. |
UNITY_3_0 | Platform define for the major version of Unity 3.0. |
UNITY_3_0_0 | Platform define for the specific version 0 of Unity 3.0. |
UNITY_3_1 | Platform define for major version of Unity 3.1. |
UNITY_3_2 | Platform define for major version of Unity 3.2. |
UNITY_3_3 | Platform define for major version of Unity 3.3. |
UNITY_3_4 | Platform define for major version of Unity 3.4. |
UNITY_3_5 | Platform define for major version of Unity 3.5. |
UNITY_4_0 | Platform define for major version of Unity 4.0. |
UNITY_4_0_1 | Platform define for major version of Unity 4.0.1. |
UNITY_4_1 | Platform define for major version of Unity 4.1. |
UNITY_4_2 | Platform define for major version of Unity 4.2. |
转自http://blog.csdn/oskytonight/article/details/40111401
更多推荐
Unity运行平台判断
发布评论