タイトルでわかるように、ユニティエディタでゲームを実行すると、名前をシリアライズして次のシーンをロードする入力フィールド。問題は、ボタンをクリックすると、ゲームがフリーズして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);
}
}
私はまだそれを試していないので間違っているかもしれませんが、データをシリアライズするコードの中にある可能性が非常に高いです。それは私が間違ってスクリプトを書いていることが原因かもしれませんし、ゲームが別のシーンを同時にシリアル化して読み込みしようとしていることが原因かもしれません。私は完全にはわからない。 –
私は良い知らせと悪い知らせがあります。何らかの理由で、もはや私を壊すことはなくなりましたが、ボタンを押すとエラーになります。NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません。 SaveName.nameSave()(Assets/scripts/SaveName) –
はSaveName.nameSave()をsaveName.nameSave()にしないでください。 –