关于Unity Code的问题(可以为PC而不是Mobile构建)

问题描述 投票:1回答:1

我正在Unity上设计一个2D平台,我想部署到我的手机上。

但是,当我尝试构建并运行到我的Android设备时,我不断收到错误。

这是我的代码:

public class PlayerControl : MonoBehaviour
{
    private Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        rb.velocity = new Vector2(5, rb.velocity.y);
        if (Input.GetMouseButtonDown(0)){
            rb.velocity = new Vector2(5, 8);
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name == "Win")
        {
            UnityEditor.EditorApplication.isPlaying = false;
            Application.Quit();
        }
        if (collision.gameObject.name == "Lose")
        {
            SceneManager.LoadScene(0);
        }
    }
}

在我添加OnTriggerEnter2D()之前,游戏可以在我的Android上构建并运行。添加后,有错误。

我怀疑它可能与EditorApplication.isPlaying = false有关?也许还有SceneManager.LoadScene()?

我可以使用替代代码吗?

我希望比赛在我赢球时关闭,并在我输球时“重新开始”。

unity3d mobile
1个回答
2
投票

您的问题来自此行:

    UnityEditor.EditorApplication.isPlaying = false;

您不能在构建中包含UnityEditor.dll。您还应确保不在命名空间中使用它。

或者,您可以使用预处理器封装它

#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#endif
© www.soinside.com 2019 - 2024. All rights reserved.