Unity3D判断当前平台

原文地址:http://blog.csdn.net/inlet511/article/details/46829433

官方文档:http://docs.unity3d.com/Manual/PlatformDependentCompilation.html

文档上有详细说明,我在这里再补充两点:

如果想判断多个条件,可以用 || 或 & 来组合

例如:

#if UNITY_IOS || UNITY_ANDROID
...//这里的代码在IOS和Android平台都会编译
#endif

#if UNITY_ANDROID && UNITY_EDITOR
...//这里的代码只有在发布设置设置的是Android,且在编辑器里运行时才会编译
#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

还有一个我之前一直疑惑的误区:

一直以为这些平台判断只是真正在该平台上才会起作用,后来实践说明,只要在发布设置里切换到了该平台,就会起作用。其实这就是“在编译阶段起作用”的意义,是我之前对它理解不够深入。

例子说话:

#if UNITY_WEBPLAYER
//只要发布设置里切换到了WebPlayer平台,这里的代码就会被编译
#endif

#if UNITY_EDITOR
//当然,如果是在编辑器里测试,无论发布设置里是怎么设置的,这里的代码都会被编译
#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

还有运行时判断平台的方法

API:http://docs.unity3d.com/ScriptReference/RuntimePlatform.html

上代码:

if(Application.platform == RuntimePlatform.WindowsWebPlayer)
{
//只有在windows系统的webplayer平台上才会执行
}

posted on 2017-05-16 19:50  ZhYQ_note  阅读(345)  评论(0)    收藏  举报

导航