9
\$\begingroup\$

While using ScriptableObjects, how can I make some variables conditional?

Example Code:

[System.Serializable]
public class Test : ScriptableObject
{
      public bool testbool;
      public string teststring;
      public int testint;
}

Goal: When testbool == true then teststring is available to edit, when testbool == false then testint is available to edit while the other one is "grayed out".

\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

The Editor-friendly path is a "custom inspector". In Unity API terms, this means extending the Editor class.

Here is a working example, but the doc link above will walk you through a lot of the details and additional options:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
    private Test targetObject;

    void OnEnable()
    {
        targetObject = (Test) this.target;
    }

    // Implement this function to make a custom inspector.
    public override void OnInspectorGUI()
    {
        // Using Begin/End ChangeCheck is a good practice to avoid changing assets on disk that weren't edited.
        EditorGUI.BeginChangeCheck();

        // Use the editor auto-layout system to make your life easy
        EditorGUILayout.BeginVertical();
        targetObject.testBool = EditorGUILayout.Toggle("Bool", targetObject.testBool);

        // GUI.enabled enables or disables all controls until it is called again
        GUI.enabled = targetObject.testBool;
        targetObject.testString = EditorGUILayout.TextField("String", targetObject.testString);

        // Re-enable further controls
        GUI.enabled = true;

        targetObject.testInt = EditorGUILayout.IntField("Int", targetObject.testInt);

        EditorGUILayout.EndVertical();

        // If anything has changed, mark the object dirty so it's saved to disk
        if(EditorGUI.EndChangeCheck())
            EditorUtility.SetDirty(target);
    }
}

Keep in mind that this script uses Editor-only APIs, so it must be placed in a folder named Editor. The above code will turn your inspector into the following:

enter image description here

That should get you rolling until you are more comfortable with Editor scripting.

\$\endgroup\$
3
\$\begingroup\$
[System.Serializable]
public class Test : ScriptableObject
{
    private bool testbool;
    public string teststring;
    public int testint;

    public string TestString 
    {
        get 
        {    
            return teststring; 
        }
        set 
        {
            if (testbool)
                teststring = value; 
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ It looks precise! I will test and report back! \$\endgroup\$
    – Valamorde
    Commented Jun 8, 2016 at 13:18
  • \$\begingroup\$ It appears that this will only prevent a wrong value and not make it unavailable to edit while a condition is true. \$\endgroup\$
    – Valamorde
    Commented Jun 8, 2016 at 23:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .