本文介绍了Application.platform == RuntimePlatform.Android无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像我在另一个问题中所说的那样,没有人回答:

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

在我的Android设备上无法在Unity上运行 a>

我发现我的应用程序无法正常工作的问题是此行:

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.

推荐答案

在您发布帖子之后,我做了一个实验.

Follow you posts, I did an experiment.

首先,我尝试在Remote 5上运行以下代码,并将其分别作为单独的应用程序运行.

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 ());
}

在Remote 5应用程序上,控制台将打印:

On Remote 5 app, the console prints:

Application.platform: OSXEditor

在单个应用程序上,控制台将打印:

On individual app, the console prints:

Application.platform: Android

显然,Application.platform在Unity Remote 5上无法正常工作,因此将它们放在if语句中时,代码将无法工作.实际上,您的代码无需使用Remote 5即可完美运行.

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.

有两种解决方法:

  1. 请勿使用Unity Remote 5测试您的应用程序.尽管这是一种测试游戏的简便方法,但有时在将应用程序部署到真实设备上时却无法按预期工作.

  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.

使用依赖于平台的编译.我建议您使用它在不同平台上开发游戏.正如 RuntimePlatform 文档所建议的:

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

这里是一个仅在具有平台相关标志的Android平台上运行代码的示例.

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
    }

这篇关于Application.platform == RuntimePlatform.Android无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:54