Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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".

share|improve this question
up vote 3 down vote accepted

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()
    {
        // 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();

        // Mark the object dirty so it's saved to disk
        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.

share|improve this answer
[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; 
        }
    }
}
share|improve this answer
    
It looks precise! I will test and report back! – Valamorde Jun 8 at 13:18
    
It appears that this will only prevent a wrong value and not make it unavailable to edit while a condition is true. – Valamorde Jun 8 at 23:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.