我是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); } }
我将此脚本作为组件附加到驱动场景加载的所有按钮(在整个应用程序中).同样,这里的值是我可以将我的"场景"从assets文件夹拖到检查器/组件中(因此我不必显式管理"整数"或"字符串" - 它将全部通过对象引用).
参见例如:
再次,这适用于PC UPDATE:根据评论中的建议,我在独立播放器中尝试了这个并且它失败了.这仅适用于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:0 12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) [0x00000] in :0 12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) [0x00000] in :0 12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) [0x00000] in :0 12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in :0 12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.UI.Button.Press () [0x00000] in :0 12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00000] in :0 12-23 15:37:27.028 14816 14847 I Unity : at UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in :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 :0 12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) [0x00000] in :0 12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) [0x00000] in :0 12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) [0x00000] in :0 12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.Events.UnityEvent`1[T0].Invoke (.T0 arg0) [0x00000] in :0 12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.EventSystems.EventTrigger.Execute (EventTriggerType id, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in :0 12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.EventSystems.EventTrigger.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00000] in :0 12-23 15:37:27.036 14816 14847 I Unity : at UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in 如果我输入一个硬编码值(字符串或场景int),它在Android和PC中都能正常工作.但是,由于某些我无法看到的原因,我在Inspector中引用的Scene对象,当我为Android构建时,似乎没有捕获String名称并将其传递给LoadScene(),就像我想的那样.
任何人都可以建议我做错了什么?这是不可能的还是愚蠢的想法?
先感谢您.