Unity场景管理和Android - 无法加载场景

时间:2015-12-23 20:55:43

标签: c# unity3d unity5

我是C#/ Unity的新手(温柔) - 我试图创建自己的脚本,向Inspector添加一个公共变量,这样我就可以让一个脚本适用于多个GameObjects(左右)我想)。

脚本非常简单。

它适用于PC,但它在Android上无效。

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;

public class BtnLoadScene : MonoBehaviour {
    public Object SceneObj;
    private string SceneName;

    public void LoadLevel()
    {
        SceneName = SceneObj.name;
        Debug.Log (SceneName);
        SceneManager.LoadScene(SceneName);
    }
}

我将此脚本作为组件附加到驱动场景加载的所有按钮(在整个应用程序中)。再次,这里的价值是我可以拖动我的"场景"从资产文件夹到检查员/组件(因此我不必明确管理" ints"或"字符串" - 它将全部通过对象完成参考文献)。

参见例如: enter image description here

再次,这适用于PC 更新:根据评论中的建议,我在独立播放器中尝试了这个并且它失败了。这仅适用于Unity编辑器。当我为Android构建时(是的它编译并且我可以安装APK)它不起作用 - 第一个场景加载,按钮响应我的触摸,我看到它"压下",但是亚行抛出:

12-23 15:37:27.028 14816 14847 I Unity   : NullReferenceException: Object reference not set to an instance of an object
12-23 15:37:27.028 14816 14847 I Unity   :   at BtnLoadScene.LoadLevel () [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity   :   at UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity   :   at UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity   :   at UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity   :   at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity   :   at UnityEngine.UI.Button.Press () [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity   :   at UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity   :   at UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <filename unknown>:0
12-23 15:37:27.028 14816 14847 I Unity   :   at UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target
12-23 15:37:27.036 14816 14847 I Unity   : NullReferenceException: Object reference not set to an instance of an object
12-23 15:37:27.036 14816 14847 I Unity   :   at BtnLoadScene.LoadLevel () [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity   :   at UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity   :   at UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity   :   at UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity   :   at UnityEngine.Events.UnityEvent`1[T0].Invoke (.T0 arg0) [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity   :   at UnityEngine.EventSystems.EventTrigger.Execute (EventTriggerType id, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity   :   at UnityEngine.EventSystems.EventTrigger.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00000] in <filename unknown>:0
12-23 15:37:27.036 14816 14847 I Unity   :   at UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <filename

如果我输入一个硬编码值(字符串或场景int),它在Android和PC中都能正常工作。但是,由于某些我无法看到的原因,我在Inspector中引用的Scene对象,当我为Android构建时,似乎没有捕获String名称并将其传递给像我想的那样LoadScene()。

有人可以告诉我做错了什么吗?这是不可能的还是愚蠢的想法?

提前谢谢。

1 个答案:

答案 0 :(得分:2)

我担心UnityEngine.SceneAsset只能在编辑器中使用,并且在任何版本中最终都会为null。 但是,Unity3D只需要BuildSettings列表中的字符串或索引,因此它很简单:只需在检查器中传递场景名称即可。在整个开发过程中,它将是一个常量,并且如果您更改场景名称将会表现相同,就像您将实际的SceneAsset对象放在那里一样(在两种情况下都会在重命名时失败)。