新的C#/ Unity(溫柔) - 我試圖創建我自己的腳本,添加一個公共變量到檢查器,這樣做可以讓我有一個腳本適用於多個GameObjects(或者我認爲)。Unity場景管理和Android - 無法加載場景
該腳本非常簡單。
它適用於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);
}
}
我將此腳本作爲組件附加到了驅動場景加載的所有按鈕(在整個應用程序中)。同樣,這裏的價值是,我可以將資產文件夾中的「場景」拖放到檢查器/組件中(因此,我不必顯式管理「整數」或「字符串」),它將全部通過對象引用)。
再次,
這會作用於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 <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(),就像我認爲的那樣。
任何人都可以建議我做錯了什麼?這是不可能的或一個愚蠢的想法?
預先感謝您。
您必須添加構建遊戲時將加載到遊戲中的所有場景。如果你已經這麼做了,不要在調試器上使用pc,而應該在獨立版本上使用。我不知道是什麼原因造成的,但你也可以在Unity論壇上提問。 – Snak
@Snak,是的,這是我檢查的第一件事情之一;在原始問題中提供的屏幕截圖中,我展示了在構建中加載的場景以及在inspector/component/script中引用的場景對象。如果我不能在這裏尋求解決方案,我會去嘗試團結論壇。謝謝。 –
然後很酷。無論如何,我會說問題是SceneObj對象爲null,因此當代碼嘗試訪問它時,它會拋出異常。 – Snak