Search Unity

Question Display Scriptable object in inspector using generic/template variables

Discussion in 'Scripting' started by fouratj, Jul 15, 2020.

  1. fouratj

    fouratj

    Joined:
    Sep 20, 2019
    Posts:
    9
    Hi there,

    I'm struggling to display a generic variable in the inspector using the scriptable objects
    Here's what my code look like

    Code (CSharp):
    1. [System.Serializable]
    2. public class VariableTypes<T> : ScriptableObject
    3. {
    4.     [SerializeField]
    5.     public T Value;
    6.  
    7.     public virtual T GetValue() { return Value; }
    8. }
    9.  
    10. [System.Serializable]
    11. [CreateAssetMenu(menuName = "SOExtention/Variables/Int")]
    12. public class VariableInt : VariableTypes<int> { }
    13.  
    Code (CSharp):
    1. [System.Serializable]
    2. public abstract class VariableReferenceTypes<T1>
    3. {
    4.     public bool UseConstant = true;
    5.  
    6.     [SerializeField]
    7.     public T1 ConstantValue;
    8.  
    9.     [SerializeField]
    10.     public VariableTypes<T1> Variable;
    11.  
    12.     public T1 Value
    13.     {
    14.         get { return UseConstant ? ConstantValue : Variable.GetValue(); }
    15.     }
    16. }
    17.  
    18. [Serializable]
    19. public class VariableReferenceInt : VariableReferenceTypes<int> { }
    20.  
    21.  
    But here's what the final result look like

    The test string is what is supposed to display, and the String Type is my result using the generic

    upload_2020-7-15_13-47-4.png

    Did someone already faced this issue ?

    Any help would be appreciated
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I'm not sure there's a true generic mapper that would work, and you might have to query the type and write your own custom field editor. I think this might be your friend for such an endeavor:

    https://docs.unity3d.com/ScriptReference/EditorGUILayout.PropertyField.html

    But I am speculating on that last bit so if someone knows that is NOT it, please tell me and I will edit this post.
     
  3. fouratj

    fouratj

    Joined:
    Sep 20, 2019
    Posts:
    9
    Unfortunately i tried that with the editor but doesn't show up

    Code (CSharp):
    1. [CustomEditor(typeof(VariableReferenceString))]
    2. [CanEditMultipleObjects]
    3. public class VariableTypeCustomEditor : Editor
    4. {
    5.      SerializedProperty m_GameObjectProp;
    6.  
    7.     void OnEnable()
    8.     {
    9.         // Fetch the objects from the GameObject script to display in the inspector
    10.         m_GameObjectProp = serializedObject.FindProperty("Variable");
    11.     }
    12.  
    13.     public override void OnInspectorGUI()
    14.     {
    15.         base.OnInspectorGUI();
    16.  
    17.         //The variables and GameObject from the MyGameObject script are displayed in the Inspector with appropriate labels
    18.         EditorGUILayout.PropertyField(m_GameObjectProp, new GUIContent("Game Object"));
    19.  
    20.         // Apply changes to the serializedProperty - always do this at the end of OnInspectorGUI.
    21.         serializedObject.ApplyModifiedProperties();
    22.     }
    23. }
    I think that the only way left for me is to do all that manually without using generics variable which is quite annoying but... i don't think i have other choices
     
  4. Elango

    Elango

    Joined:
    Jan 27, 2016
    Posts:
    107
    I'm afraid you have to use concrete(non-generic) class at this point
    public VariableTypes<T1> Variable;

    like
    public VariableInt Variable;

    but this will ruin the whole point of the generic class.
    Unity 2020 support this even without concreting generic class, so you don't need VariableReferenceInt and can do right away
    public VariableReferenceTypes<int> myInt;
     
    SolidAlloy likes this.