Application.platform == RuntimePlatform.Android no

2019-08-23 08:41发布

Like I said in this other question that no one has answered:

Touch not working on Unity on my Android device

I discovered that the problem of my app not working was this lines:

if (Application.platform == RuntimePlatform.Android) {

} 

If i remove that condition the app works like it should but the moment i add it back it just doensn't work.

1条回答
我只想做你的唯一
2楼-- · 2019-08-23 09:22

Follow you posts, I did an experiment.

First, I try run below code on Remote 5 and run it as an individual app respectively.

void Update () {
    Debug.LogFormat ("Application.platform: {0}", Application.platform.ToString ());
}

On Remote 5 app, the console prints:

Application.platform: OSXEditor

On individual app, the console prints:

Application.platform: Android

Apparently, the Application.platform is not working as you expected with Unity Remote 5, so your code doesn't work when you put them inside the if statement. Actually, your code would work perfectly without using Remote 5.

There are two ways to solve this:

  1. Don't use Unity Remote 5 to test your app. Though it's a pretty handy way to test your game, sometimes it's not working as you expected when you deploy your app to the real device.

  2. Use Platform dependent compilation. I recommend you to use this for develop your game on different platforms. As the RuntimePlatform document suggests:

The difference between using RuntimePlatform and Platform dependent Compilation is that using RuntimePlatform is evaluated at runtime while Platform dependent Compilation is evaluated at compile time. So if you can use platform dependent compilation, don't hesitate to use it. In most cases, you can get the same functionality using both, and using the defines will produce smaller and faster code, as you don't need to check at runtime. There are some cases where RuntimePlatform is needed at runtime.

Here is an example to run your code on only Android platform with platform dependent flag.

    void Update () {
#if UNITY_ANDROID && !UNITY_EDITOR
        // Run your Android only code here.
#endif
    }
查看更多
登录 后发表回答