[英]How does Unity distinguish between Android and iOS
Let's say we have make a mobile game based on accelerometer. 假设我们已经制作了基于加速度计的手机游戏。 We're using Input.accelerometer class.
我们正在使用Input.accelerometer类。
using UnityEngine;
public class ExampleClass : MonoBehaviour {
public float speed = 10.0F;
void Update() {
Vector3 dir = Vector3.zero;
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
}
}
This code works on Andoid and on iOS. 此代码可在Andoid和iOS上使用。 Im wondering, how does Unity know if we're running the game on Android or iOS?
我想知道,Unity如何知道我们是在Android还是iOS上运行游戏? I have checked UnityEngine.dll file and I did not find any if-else statement between the operating systems.
我已经检查了UnityEngine.dll文件,但在操作系统之间找不到任何if-else语句。
There basically two sides of Unity which are the managed and native side. 基本上,Unity的两个方面是托管方面和本机方面。 The managed part is just the C# API and the native side is just the native side of the code written in C++ that communicates with Object-C Swift for iOS or Java for Android to access.
受管部分只是C#API,本机端只是用C ++编写的代码的本机端,可以与iOS的Object-C Swift或Android的Java通信。
The managed part extension is usually .dll and the native side for Android are usually .so , .aar , .jar or just a .dex . 受管部分扩展名通常是.dll ,而Android的本机端通常是.so , .aar , .jar或只是.dex 。
The managed part extension for iOS native side are usually .a , .m , .mm , .c , .cpp . iOS本机端的受管部件扩展名通常是.a , .m , .mm , .c和.cpp 。 For iOS, some manage part are not compiled and still in form of .cpp until you build the generated project with xCode.
对于iOS,直到您使用xCode构建生成的项目之前,某些管理部分都不会编译,而是仍为.cpp形式。
This code works on Andoid and on iOS.
此代码可在Andoid和iOS上使用。 Im wondering, how does Unity know if we're running the game on Android or iOS?
我想知道,Unity如何知道我们是在Android还是iOS上运行游戏?
They don't do this during run-time. 他们在运行时不这样做。 There are so many Unity API and doing this every time would be redundant and messy.
Unity API太多了,每次都这样做是多余的且混乱的。
To simplify this, it uses different managed and native files for different platforms. 为了简化此操作,它为不同平台使用了不同的托管文件和本机文件。 These files are included during build-time and this decision is made in the Editor once you click the build button.
这些文件将在构建期间包括在内,单击构建按钮后,即可在编辑器中做出此决定。
First, go to <UnityInstallationDirecory>\\Editor\\Data\\PlaybackEngines
首先,转到
<UnityInstallationDirecory>\\Editor\\Data\\PlaybackEngines
To avoid using some preprocessor directives , Application.platform
or mising different platforms codes, this is done in the Editor during build time. 为避免使用某些预处理程序指令 ,
Application.platform
或混淆其他平台代码,此操作在构建时在编辑器中完成。 If you select iOS and build the project, it will include UnityEngine.dll
for iOS located in the C:\\Program <UnityInstallationDirecory>\\Editor\\Data\\PlaybackEngines\\iOSSupport
path. 如果您选择iOS和建设项目,将包括
UnityEngine.dll
为位于C的iOS:\\ PROGRAM <UnityInstallationDirecory>\\Editor\\Data\\PlaybackEngines\\iOSSupport
路径。 It will do the-same for Android but in the <UnityInstallationDirecory>\\Editor\\Data\\PlaybackEngines\\AndroidPlayer
path. 它将与Android相同,但位于
<UnityInstallationDirecory>\\Editor\\Data\\PlaybackEngines\\AndroidPlayer
路径中。
There are different UnityEngine.dll for each platform. 每个平台都有不同的UnityEngine.dll 。 You can see them in the path I mentioned above.
您可以在我上面提到的路径中看到它们。 This UnityEngine.dll and other managed dlls is where you will see a call to the native side of the code with
DllImport
or with AndroidJavaClass
. 在该UnityEngine.dll和其他托管dll中,您将看到使用
DllImport
或AndroidJavaClass
对代码本机端的AndroidJavaClass
。 Note that UnityEngine.dll is not the only file included in that build. 请注意, UnityEngine.dll不是该版本中包含的唯一文件。 Most files in these paths, with the extensions I mentioned above are included.
这些路径中的大多数文件都带有我上面提到的扩展名。 These includes the managed and native files.
这些包括托管文件和本机文件。
The proper way to determine what platform you are on would be to check with the #define
directives that Unity uses. 确定您所处平台的正确方法是使用Unity使用的
#define
指令进行检查。 You can find more information about it in the docs here 您可以在这里的文档中找到有关它的更多信息
Here is an example of how you can check if you are on Android or IOS: 以下是如何检查您使用的是Android还是IOS的示例:
#if UNITY_ANRDOID
// Android code goes here
#elif UNITY_IOS
// IOS Code goes here
#endif
Hope this helps! 希望这可以帮助!
If you want a runtime check, then you can use the RuntimePLatform 如果要运行时检查,则可以使用RuntimePLatform
if (Application.platform == RuntimePlatform.Android)
value = 5;
else if(Application.platform == RuntimePlatform.IPhonePlayer)
value = 10;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.