【分享】Unity判斷是PC/手機/平板/iOS/Android 平台
由於如果開發中會針對不同平台做不同處理
就可以用官方文件提供的方法來選擇不同的code
像是開發的時候常常會需要在PC(IDE上)跟目標平台不斷測試與切換就會用到
例如在PC上是mouseClick,在移動裝置上是touch
先直接上Code:
///Check the current device is tablet or not private void checkTheCurrentDevice() { #if UNITY_IPHONE string deviceModel = SystemInfo.deviceModel.ToLower().Trim(); if(deviceModel.StartsWith("ipad")) { bIsTablet = true; } else { bIsTablet = false; } #elif UNITY_ANDROID float physicScreenSize = Mathf.Sqrt(Screen.width * Screen.width + Screen.height * Screen.height) / Screen.dpi; if(physicScreenSize >= 7f) //If the screen size is >= 7 inches, it‘s a tablet { bIsTablet = true; } else { bIsTablet = false; } #else bIsTablet = false; #endif // bIsTablet = false; }此外Unity官方文件有提供判斷不同平台的方法
http://docs.unity3d.com/Manual/PlatformDependentCompilation.html
簡易範例:
#if UNITY_EDITOR
Debug.Log("Unity Editor");
#elif UNITY_IPHONE
Debug.Log("Unity iPhone");
#else
Debug.Log("Any other platform");
#endif
來源與補充:
http://www.tqcto.com/article/mobile/135196.html
http://www.lxway.com/422815844.htm