Using ChangeCheckScope

Unity_s9D7ALeIkE

Using ChangeCheckScope to Check if any control was changed inside a block of code and VerticalScope to organize the nodes together.

EditorGUILayout.LabelField(dialogue.name);

using EditorGUI.ChangeCheckScope changeCheckScope = new();
foreach (DialogueNode node in dialogue.GetAllNodes())
{
    using (new EditorGUILayout.VerticalScope("Box"))
    {
        node.uniqueID = EditorGUILayout.TextField("Unique ID:", node.uniqueID);
        EditorGUILayout.LabelField("Text");
        node.dialogueText = EditorGUILayout.TextArea(node.dialogueText);
    }
}
    
if (changeCheckScope.changed) EditorUtility.SetDirty(_dialogue);

A little cleaner way of doing it then having

string newText = EditorGUILayout.TextField("Text:", node.text);
if (newText != node.text)
{
    node.text = newText;
    EditorUtility.SetDirty(dialouge);
}

As now I do not have to check every variable that I want to change by caching it first and checking if it is not the same as the original. Unity just says “hey a control in your GUI has changed” and respond with “ok I you need to save the Scriptable Object”.

The ‘using EditorGUI.ChangeCheckScope changeCheckScope = new();’ is the same as ‘using (new EditorGUILayout.VerticalScope(“Box”)) {…}’ which is the same as EditorGUI.BeginChangeCheck(); other code if (EditorGUI.EndChangeCheck() EditorUtility.SetDirty(_dialogue); which is a way of saying if anything changes after this code set the changed variable to true. If the changed variable is true then set the scriptable object to dirty.

The using (new EditorGUILayout.VerticalScope("Box")) {..} create the box around each node, It is the same as doing EditorGUILayout.BeginVertical("Box"); things you want in the box EditorGUILayout.EndVertical(); with the added benefit that if there is errors in my code I do not get get extra errors from Unity GUI.

Looks like I got ahead of the videos again.

Privacy & Terms