private void JsonUtilityStuff() { var savePath = CreateSaveDataPath("SerializablePlayerData.json"); if (GUILayout.Button("Try Save JSON")) { var scriptableObject = CreateTestScriptableObject(); var jsonString = JsonUtility.ToJson(scriptableObject); using (var streamWriter = new StreamWriter(savePath)) { streamWriter.Write(jsonString); } } if (GUILayout.Button("Try Load JSON")) { if (!File.Exists(savePath)) { EditorUtility.DisplayDialog("No serialized file found", "Please make sure the file exists", "OK"); } else { ScriptablePlayerData scriptableObjectSaveData = ScriptableObject.CreateInstance <ScriptablePlayerData>(); using (var textReader = new StreamReader(savePath)) { JsonUtility.FromJsonOverwrite(textReader.ReadToEnd(), scriptableObjectSaveData); } Debug.Log(scriptableObjectSaveData.name); } } }
private void AssetStuff() { var fileName = "ScriptablePlayerData.asset"; var savePath = "Assets/" + fileName; if (GUILayout.Button("Try save asset")) { var scriptableObject = CreateTestScriptableObject(); AssetDatabase.CreateAsset(scriptableObject, savePath); } if (GUILayout.Button("Try load asset")) { _currPlayerData = AssetDatabase.LoadAssetAtPath <ScriptablePlayerData>(savePath); if (_currPlayerData == null) { EditorUtility.DisplayDialog("No scriptable object", "Please make sure the file exists", "OK"); } } if (_currPlayerData != null) { EditorGUILayout.Space(); EditorGUILayout.TextField("Scriptable object is not null:"); EditorGUILayout.LabelField("Health: " + _currPlayerData.health); EditorGUILayout.LabelField("Name: " + _currPlayerData.name); int indent = EditorGUI.indentLevel; EditorGUI.indentLevel = 1; foreach (var keyValuePair in _currPlayerData.inventory) { EditorGUILayout.LabelField(keyValuePair.Key + " : " + keyValuePair.Value); } EditorGUI.indentLevel = indent; } }
private void showDictionary(SerializedProperty keys, SerializedProperty values, ScriptablePlayerData data) { if (keys == null || values == null) { Debug.Log("Dictionary is empty"); return; } for (int i = 0, l = keys.arraySize; i < l; i++) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(keys.GetArrayElementAtIndex(i), GUIContent.none, true, GUILayout.MinWidth(60)); GUILayout.Space(8); EditorGUILayout.PropertyField(values.GetArrayElementAtIndex(i), GUIContent.none, true, GUILayout.MinWidth(60)); GUILayout.Space(8); if (GUILayout.Button("-", GUILayout.MaxWidth(30))) { Debug.Log("Remove key: " + keys.GetArrayElementAtIndex(i).stringValue); data.inventory.Remove(keys.GetArrayElementAtIndex(i).stringValue); EditorUtility.SetDirty(target); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); } }