這里就介紹幾種常見的,也是便於使用的幾種平台判斷的方法。
1.先說第一種,也是我用的順手的一個。利用RuntimePlatform判斷,API上的解釋是[The platform application is running. Returned by Application.platform.]
舉個栗子:if (Application.platform == RuntimePlatform.WindowsEditor) { }
一般常用的是三個平台,安卓[Android],蘋果[IphonePlayer],Unity編輯器[WindowsEditor]。
2.在編輯器中的平台依賴編譯[Platform Dependent Compilation],可以使用if...else...對不同平台的代碼進行區分。
栗子代碼:
void Awake() {
#if UNITY_ANDROID
Debug.Log("這里安卓設備");
#endif
#if UNITY_IPHONE
Debug.Log("這里蘋果設備");
#endif
#if UNITY_STANDALONE_WIN
Debug.Log("電腦上運行o");
#endif
}
3.看見別人貼出來的代碼,我就粘了過來,以備使用。
switch (Application.platform)
{
case RuntimePlatform.WindowsEditor:
print("Windows");
break;
case RuntimePlatform.Android:
print("Android");
break;
case RuntimePlatform.IPhonePlayer:
print("Iphone");
break;
}
最后,貼出官網的API,具體的平台在官網上都有介紹,官網地址:https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
