0
public class GameDataEditor : EditorWindow{      
public GameData gameData;
private string gameDataProjectFilePath = "/StreamingAssets/data.json";

//[MenuItem("Window/Game Data Editor")]
static void Init()
{
    EditorWindow.GetWindow(typeof(GameDataEditor)).Show();
}

void OnGUI()
{
    if (gameData != null)
    {
        GameDataEditor obj = ScriptableObject.CreateInstance<GameDataEditor>();

        SerializedObject serializedObject = new SerializedObject(obj);
        SerializedProperty serializedProperty = serializedObject.FindProperty("gameData");
        Debug.Log("myInt " + gameData.allRoundData.Length);
        EditorGUILayout.PropertyField(serializedProperty, true);

        serializedObject.ApplyModifiedProperties();

        if(GUILayout.Button("Save data"))
        {
            SaveGameData();
        }
    }

    if (GUILayout.Button("Load Data"))
    {
        LoadGameData();
    }
}

private void LoadGameData()
{
    string filePath = Application.dataPath + gameDataProjectFilePath;
    if (File.Exists(filePath))
    {
        string dataAsJson = File.ReadAllText(filePath);
        gameData = JsonUtility.FromJson<GameData>(dataAsJson);
        Debug.Log("File Exists " + gameData.allRoundData[0].name);
    }
    else
    {
        gameData = new GameData();
        Debug.Log("File not found");
    }
}

private void SaveGameData()
{
    string dataAsJson = JsonUtility.ToJson(gameData);
    string filePath = Application.dataPath + gameDataProjectFilePath;
    File.WriteAllText(filePath,dataAsJson);
}

}

得到一个错误。NullReferenceException:对象引用未设置为对象 UnityEditor.PropertyHandler.OnGUILayout 的实例(UnityEditor.SerializedProperty 属性,UnityEngine.GUIContent 标签,布尔 includeChildren,

EditorGUILayout.PropertyField(serializedProperty, true);
4

0 回答 0