[英]Game stops when button is clicked to serialize data and change scene, but no error log from Unity
如标题所示,当我在Unity编辑器中运行游戏时,在将用户名输入序列名并输入下一个场景的输入字段中输入名称后,单击按钮。 问题是,当我单击该按钮时,游戏将冻结并且Unity停止响应,但是当我删除序列化名称的代码时,它将加载下一个级别,没有问题。 我已附上了从输入字段获取名称的代码,将名称序列化的代码以及按该顺序加载下一个场景的代码。 该问题很可能是序列化代码中的问题,但我不确定是什么问题。 我按照Yuri Galiza的要求编辑了代码并替换了下面的代码。 要直接查看问题并获得有关代码功能的更好的上下文,请使用此链接: https : //drive.google.com/drive/folders/0BynpT6Viy1xdR0IwWmV6T3RRU2s?usp=sharing
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);
}
}
好吧,可能是应用程序在调用Log之前就快死了。 尝试在整个代码中的不同位置放置日志。 然后您可能会发现问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.