C# 当点击按钮序列化数据并更改场景时,游戏停止,但Unity没有错误日志
正如您在标题中所看到的,当我在Unity编辑器中运行游戏时,我在输入字段中输入用户名后单击一个按钮,该字段序列化名称并加载下一个场景。问题是,当我单击按钮时,游戏冻结,Unity停止响应,但当我删除序列化名称的代码时,它会加载下一个级别,没有问题。我附加了从输入字段获取名称的代码、序列化名称的代码以及按该顺序加载下一个场景的代码。问题很可能出在序列化代码中,但我不完全确定问题出在哪里。我编辑了代码,并按照尤里·加利扎的要求替换了下面的代码。若要直接查看问题并获得有关代码所做工作的更好上下文,请使用以下链接:C# 当点击按钮序列化数据并更改场景时,游戏停止,但Unity没有错误日志,c#,serialization,unity5,C#,Serialization,Unity5,正如您在标题中所看到的,当我在Unity编辑器中运行游戏时,我在输入字段中输入用户名后单击一个按钮,该字段序列化名称并加载下一个场景。问题是,当我单击按钮时,游戏冻结,Unity停止响应,但当我删除序列化名称的代码时,它会加载下一个级别,没有问题。我附加了从输入字段获取名称的代码、序列化名称的代码以及按该顺序加载下一个场景的代码。问题很可能出在序列化代码中,但我不完全确定问题出在哪里。我编辑了代码,并按照尤里·加利扎的要求替换了下面的代码。若要直接查看问题并获得有关代码所做工作的更好上下文,请使
使用系统集合;
使用System.Collections.Generic;
使用System.IO;
使用UnityEngine.UI;
使用UnityEngine;
[系统可序列化]
公共类getUserName:MonoBehavior
{
公共输入字段mainputfield;
公共字符串用户名;
公共静态电流;
public void SetName()
{
用户名=mainputfield.text;
Debug.Log(用户名);
GameObject NameSaver=GameObject.Find(“NameSaver”);
SaveName SaveName=NameSaver.GetComponent();
SaveName.nameSave();
}
}
使用系统集合;
使用System.Collections.Generic;
使用System.Runtime.Serialization.Formatters.Binary;
使用System.IO;
使用UnityEngine.UI;
使用UnityEngine;
公共类存储名:MonoBehavior
{
公共静态字符串播放器名称;
公共静态列表名称=新列表();
公共静态void nameSave()
{
Debug.Log(“保存”);
GameObject InputField=GameObject.Find(“InputField”);
getUserName getUserName=InputField.GetComponent();
GetUserName.SetName();
PlayerName=GetUserName.userName;
Log(“PlayerName”);
name.Add(getUserName.current);
BinaryFormatter bf=新的BinaryFormatter();
FileStream file=file.Create(Application.persistentDataPath+“/savedGames.torlon”);
序列化(文件,SaveName.names);
file.Close();
Debug.Log(“已保存”);
}
公共静态void nameLoad()
{
Debug.Log(“加载”);
if(File.Exists(Application.persistentDataPath+“/savedGames.torlon”))
{
BinaryFormatter bf=新的BinaryFormatter();
FileStream file=file.Open(Application.persistentDataPath+“/savedGames.torlon”,FileMode.Open);
SaveName.names=(列表)bf.Deserialize(文件);
file.Close();
}
其他的
{
Log(“加载失败”);
}
}
}
使用系统集合;
使用UnityEngine;
使用UnityEngine.SceneManagement;
公共类ButtonManager:MonoBehavior{
公共int NextScene;
公共void OnClick()
{
Log(“加载级别”);
场景管理器。加载场景(NextScene);
}
}
嗯,在调用日志之前,应用程序可能已经死亡。尝试在代码中的不同位置放置日志。然后您可能会找到问题所在。我还没有尝试过,所以我可能错了,但很可能是在序列化数据的代码中。这可能是因为我的脚本编写错误,也可能是因为游戏试图序列化和加载另一个场景,同时中断。我不完全确定。我有一些好消息和一些坏消息。出于某种原因,它不再困扰我,但当我按下按钮时,我得到了一个错误:NullReferenceException:对象引用未设置为对象SaveName.nameSave()的实例(位于Assets/scripts/SaveName.cs:22)getUserName.SetName()(位于Assets/scripts/getUserName.cs:23)不应该SaveName.nameSave()是SaveName.nameSave()是的,看起来你把它们放错顺序了,只是注意到了。谢谢你提出来。除了修复这个问题之外,我还更改了代码以异步加载场景,但是我没有看到控制台中出现任何序列化数据的代码,并且输入用户名的场景不会消失,而是停留在新场景的顶部。到目前为止,我将在整个游戏中添加一个指向谷歌硬盘的链接。检查问题以找到它。
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.UI;
using UnityEngine;
[System.Serializable]
public class getUserName : MonoBehaviour
{
public InputField mainInputField;
public string userName;
public static getUserName current;
public void SetName()
{
userName = mainInputField.text;
Debug.Log(userName);
GameObject NameSaver = GameObject.Find("NameSaver");
SaveName saveName = NameSaver.GetComponent<SaveName>();
SaveName.nameSave();
}
}
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.UI;
using UnityEngine;
public class SaveName : MonoBehaviour
{
public static string PlayerName;
public static List<getUserName> names = new List<getUserName>();
public static void nameSave()
{
Debug.Log("Saving");
GameObject InputField = GameObject.Find("InputField");
getUserName GetUserName = InputField.GetComponent<getUserName>();
GetUserName.SetName();
PlayerName = GetUserName.userName;
Debug.Log("PlayerName");
names.Add(getUserName.current);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/savedGames.torlon");
bf.Serialize(file, SaveName.names);
file.Close();
Debug.Log("Saved");
}
public static void nameLoad()
{
Debug.Log("Loading");
if (File.Exists(Application.persistentDataPath + "/savedGames.torlon"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/savedGames.torlon", FileMode.Open);
SaveName.names = (List<getUserName>)bf.Deserialize(file);
file.Close();
}
else
{
Debug.Log("Load failed");
}
}
}
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ButtonManager : MonoBehaviour {
public int NextScene;
public void OnClick()
{
Debug.Log("Loading level");
SceneManager.LoadScene(NextScene);
}
}