I have three scripts: Singleton, MapManager and MapManagerEditor
When I make changes in the MapManager script component, after I click to go to other scene and then come back, the values are reset!
Here is a small example:
Singleton
using UnityEngine;
using System.Collections;
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour {
public static T instance { get; private set; }
protected virtual void Awake()
{
if (instance == null)
instance = this as T;
else
Destroy(gameObject);
}
protected virtual void OnDisable()
{
instance = null;
}
}
}
MapManager
using UnityEngine;
using System.Collections;
[System.Serializable]
public class MapManager : Singleton<MapManager> {
[System.NonSerialized]
public int lol = 0;
}
MapManagerEditor
using UnityEditor;
using System.Collections;
using UnityEditor.SceneManagement;
[CustomEditor(typeof(MapManager))]
public class MapManagerEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
MapManager mapManager = (MapManager)target;
mapManager.lol = EditorGUILayout.IntField("lol", mapManager.lol);
}
}
I wanted to add to this. This helped me track down my problem however, the only thing of your solutions I needed to do was add the if(GUI.changed) snippet to my OnInspectorGUI method. However I also had the variables in my class set with default accesors for example
public float boo { get; set; }
however I needed to remove those so it was just
public float boo;
for some reason they hindered the process. So with those changes alone it solved my problem in case anyone tried the above and still have the issue it may be accesors.
For future generations⌠this is not a good way to get your editors to serialize. I struggled with custom editors for a long time too and did a bunch of hacky garbage just to get things kind of ok. But its not this much work once you know what to focus on.
The key is to only manipulate SerializedProperties, donât ever operate on the targetâs data! Hereâs a simple exampleâŚ
public override void OnInspectorGUI()
{
//do this first to make sure you have the latest version
serializedObject.Update();
//for each property you want to draw ....
EditorGUILayout.PropertyField(serializedObject.FindProperty("boo"));
//if you need to do something cute like use a different input type you can do this kind of thing...
SerializedProperty specialProp = serializedObject.FindProperty("myFloatThatPretendsItsAnInt");
specialProp.floatValue = EditorGUILayout.IntField(specialProp.floatValue as int) as float;
//do this last! it will loop over the properties on your object and apply any it needs to, no if necessary!
serializedObject.ApplyModifiedProperties();
}
Your question doesnât have much to do with topic and it is also bit of a necro post.
I bet that because you canât serialize a 2D array in standard Inspector, you wonât see it in serialized object for that class either. I think you will just see null if you try to do FindProperty(âmyTwoDeeArrayâ).
You could try to do a typical Serializable class instead that looks like two dimensional array (class with a list of arrays) and then draw that with your Custom Inspector + serializable object.
Hey guys, Iâm having some difficulty with this.
I managed to get everything working, but after writing something in the Editor Window and hitting Enter or Tab, the value Iâve just tiped is erased.
My functions for loading as saving are working fine.
I think the problem is on the OnGUI function
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class LocalizedTextEditor : EditorWindow
{
public LocalizationData localizationData;
Vector2 scrollPosition = Vector2.zero;
[MenuItem("Window/Localized Text Editor")]
static void Init()
{
EditorWindow.GetWindow(typeof(LocalizedTextEditor)).Show();
}
private void OnGUI()
{
scrollPosition = GUILayout.BeginScrollView(scrollPosition, true, true);
if (localizationData != null)
{
SerializedObject serializedObject = new SerializedObject(this);
serializedObject.Update();
SerializedProperty serializedProperty = serializedObject.FindProperty("localizationData");
EditorGUILayout.PropertyField(serializedProperty);
serializedObject.ApplyModifiedProperties();
if (GUILayout.Button("Save data"))
{
SaveGameData();
}
}
if (GUILayout.Button("Load data"))
{
LoadGameData();
}
if (GUILayout.Button("Create new data"))
{
CreateNewData();
}
GUILayout.EndScrollView();
}
private void LoadGameData()
{
string filePath = EditorUtility.OpenFilePanel("Select localization data file", Application.streamingAssetsPath, "json");
if (!string.IsNullOrEmpty(filePath))
{
string dataAsJson = File.ReadAllText(filePath);
localizationData = JsonUtility.FromJson<LocalizationData>(dataAsJson);
}
}
private void SaveGameData()
{
string filePath = EditorUtility.SaveFilePanel("Save localization data file", Application.streamingAssetsPath, "", "json");
if (!string.IsNullOrEmpty(filePath))
{
string dataAsJson = JsonUtility.ToJson(localizationData);
File.WriteAllText(filePath, dataAsJson);
}
}
private void CreateNewData()
{
localizationData = new LocalizationData();
}
}
[System.Serializable]
public class LocalizationData
{
public LocalizationItem[] items;
}
[System.Serializable]
public class LocalizationItem
{
public string key;
public string value;
}
THANK YOU!!! I tried for hours and didnât get it to work the right way. That was my last attempt and after I changed my Code to your Soloution it worked flawlessly.
Maybe this is a bad idea, but I managed to get this to work:
public static void CopyFromObject(this SerializedObject serializedObject, T target)
where T : notnull, UnityEngine.Object
{
var it = new SerializedObject(target).GetIterator();
it.Next(true);
while (it.Next(false))
{
serializedObject.CopyFromSerializedProperty(it);
}
serializedObject.ApplyModifiedProperties();
}
The nice thing about this is that it lets me edit the target object directly, then save those changes back into the serializedObject without having to go through all the standard SerializedObject hoops.
I needed the GameObject to be Serialized and I needed it to be hidden in the inspector for organizational sake.
Itâs simple but itâs what worked for me.
My particular issue is that i was maintaining a reference for âbaseClassSerializedObjectâ and calling
serializedObject.Update()
DrawPropertiesExcluding(âbaseClassSerializedObjectâ, âŚ)
serializedObject.ApplyModifiedProperties()
I shouldâve either been passed serializedObjectr into the draw method OR called update and apply on the baseclassSO.