kac26
kac26

Reputation: 391

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.

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. Im wondering, how does Unity know if we're running the game on Android or iOS? I have checked UnityEngine.dll file and I did not find any if-else statement between the operating systems.

Upvotes: 2

Views: 4442

Answers (2)

Programmer
Programmer

Reputation: 125275

There basically two sides of Unity which are the managed and native side. 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.

The managed part extension is usually .dll and the native side for Android are usually .so, .aar, .jar or just a .dex.

The managed part extension for iOS native side are usually .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.

This code works on Andoid and on iOS. Im wondering, how does Unity know if we're running the game on Android or 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.

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

enter image description here

To avoid using some preprocessor directives, Application.platform or mising different platforms codes, this is done in the Editor during build time. 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. It will do the-same for Android but in the <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer path.

There are different UnityEngine.dll for each platform. 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. Note that UnityEngine.dll is not the only file included in that build. Most files in these paths, with the extensions I mentioned above are included. These includes the managed and native files.

Upvotes: 4

BenjaFriend
BenjaFriend

Reputation: 664

The proper way to determine what platform you are on would be to check with the #define directives that Unity uses. 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:

#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

if (Application.platform == RuntimePlatform.Android)
     value = 5;
 else if(Application.platform == RuntimePlatform.IPhonePlayer)
    value = 10;

Upvotes: 1

Related Questions