Unity執行平臺判斷
阿新 • • 發佈:2018-11-10
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.net/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. |