I’m currently making a custom inspector script for one of my scripts that allows user input to set a inspector read only variable on the script, with background checking. The script works and sets this variable but after saving the scene, project and restarting unity the variable is now empty.
I’ve tried force saving the scene and project via script but also this does not work.
The variable script snippet:
public class Object : OWSGBehaviour
{
[ShowOnly, SerializeField] protected string _databaseID = "null";
// Details
/// <summary>
/// Item Database ID (SET ONLY FOR OBJECT DATABASE USE)
/// </summary>
public string DatabaseID
{
get
{
return _databaseID;
}
set
{
_databaseID = value;
}
}
The custom inspector code:
[CustomEditor(typeof(Object), true)]
public class EditorObjectInspectorAdd : Editor {
private static EditorObjectInspectorAddPopup popup;
private static Object myTarget;
static EditorObjectInspectorAdd()
{
EditorObjectInspectorAddPopup.WindowClosed += ReceiveId;
}
public override void OnInspectorGUI()
{
myTarget = (Object)target;
if (GUILayout.Button("Set Database ID"))
{
popup = ScriptableObject.CreateInstance<EditorObjectInspectorAddPopup>();
popup.position = new Rect(Screen.width / 2, Screen.height / 2, 250, 75);
popup.ShowUtility();
}
DrawDefaultInspector();
}
static void ReceiveId(string id)
{
if(myTarget.DatabaseID == "null")
{
myTarget.DatabaseID = id;
}
else
{
Debug.Log("Object Updated from " + myTarget.DatabaseID + " to: " + id);
ObjectDatabaseEditor.UpdateID(myTarget.DatabaseID, id);
myTarget.DatabaseID = id;
}
// Trying to force save changes - no dice
EditorApplication.SaveAssets();
EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
}
}
public class EditorObjectInspectorAddPopup : EditorWindow
{
public delegate void OnWindowClose(string id);
public static event OnWindowClose WindowClosed;
bool allowed = false;
string input = "";
string lastInput = "";
void OnGUI()
{
EditorGUILayout.LabelField("Input ID (10 Char max):");
input = EditorGUILayout.TextField(input, EditorStyles.textField);
input = input.Replace(" ", "");
if (input != lastInput)
{
lastInput = input;
allowed = ObjectDatabaseEditor.IsIDavaliable(input);
}
if (allowed)
{
if (GUILayout.Button("Agree!"))
{
if (WindowClosed != null)
WindowClosed(input);
this.Close();
}
} else
{
EditorGUILayout.LabelField("ID is currently taken or not avaliable");
}
}
}