【发布时间】:2021-11-07 20:25:55
【问题描述】:
我正在构建一个需要将用户创建的数据保存并加载到持久数据路径的应用。当我在 Unity 编辑器中播放它时,它运行良好,但是当我在 iOS 上尝试它时,我得到了这个错误:
Uploading Crash Report
NullReferenceException: Object reference not set to an instance of an object.
at CustomModeScript.LoadSavedModes () [0x00000] in <00000000000000000000000000000000>:0
at CustomModeScript.Initialize (System.String fromPanel) [0x00000] in <00000000000000000000000000000000>:0
at MainSettingsPanelScript.OnControlModes () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.Events.UnityAction.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1].Invoke (T1 handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchPress (UnityEngine.EventSystems.PointerEventData pointerEvent, System.Boolean pressed, System.Boolean released) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchEvents () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.StandaloneInputModule.Process () [0x00000] in <00000000000000000000000000000000>:0
UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents()
UnityEngine.EventSystems.StandaloneInputModule:Process()
以前是ioexception:Sharing violation on path 错误,现在是我重构Load函数后出现上面的错误。当我尝试加载数据时,当前正在发生此问题(我无法确定它是否正在保存)
这里是保存和加载(检索)的函数
public void SaveModes()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/UserCustomModes.jl");
List<Mode> data = new List<Mode>();
data = savedModes;
bf.Serialize(file , data);
file.Close();
}
// Retrieve saved modes if they exist
public List<Mode> RetrieveSavedModes()
{
if(File.Exists(Application.persistentDataPath + "/UserCustomModes.jl"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/UserCustomModes.jl", FileMode.Open);
List<Mode> storedModesFile = bf.Deserialize(file) as List<Mode>;
savedModes = storedModesFile;
file.Close();
}
return savedModes;
}
下面是调用加载函数的代码:
// Populate mode view with saved modes
private void PopulateModeView()
{
ClearCustomModes();
Debug.Log("Loading Modes: " + Manager.savedModes.Count);
if (Manager.savedModes.Count > 0)
{
//Debug.Log("More than 0 modes present");
var index = 0;
foreach (var savedMode in Manager.savedModes)
{
var modeItem = Instantiate(customModeItemPrefab, customModeSectionTransform);
modeItem.GetComponent<CustomModeItemScript>().Initialize(savedMode, index, this, mainSettingsPanelScript);
generatedModeButtons.Add(modeItem);
}
}
}
这里是 LoadSavedModes 函数:
private void LoadSavedModes()
{
RemoveAllModeButtons();
Manager.savedModes = Manager.RetrieveSavedModes();
Debug.Log("[UNITY] Retrieve Modes Count: " + Manager.savedModes.Count);
PopulateModeView();
}
提前谢谢你!
【问题讨论】:
-
我不确定为什么代码还不能工作,但是
not使用BinaryFormatter因为它不安全。另一种选择是ReadAllBytes和WriteAllBytes。 -
错误信息很清楚。
PersistentPath应该没有问题,因为这看起来是正确的。错误是说在Initialize调用之一中尝试调用方法LoadSavedModes时存在null引用。可以分享一下这个方法吗?如果用户没有保存数据,你会默认你的数据吗?你在保存/加载什么?是原始数据类型还是您尝试保存/加载更复杂的类型,例如GameObjects? -
刚刚意识到我为我的原始评论链接了错误的文档,
here是避免使用BinaryFormatter的原因。我之前已经链接了原始用例文档 - 我的错。 -
感谢您查看@TEEBQNE。我已经用 LoadSavedModes 函数更新了我的问题。在声明变量时以及在调用 RetrieveSavedModes 函数之前,我还将 savedModes 对象设置为新的 List
。数据是原始数据,而不是游戏对象 -
您是否能够在连接到
XCode时运行您的应用程序?这将使您的调试变得更加容易,因为XCode将显示来自您的应用程序的实时打印输出。查找打印输出,您可以根据在null参考之前和/或之后打印出的内容准确找到它中断的位置。我仍然建议将使用BinaryFormatter换成替代方案。我看到那里有几个地方可能是null参考,但很难说准确。
标签: c# ios unity3d persistent-storage