Search Unity

Question ValueInput with a struct and a custom inspector

Discussion in 'Visual Scripting' started by jsm174, Feb 9, 2022.

  1. jsm174

    jsm174

    Joined:
    Jul 22, 2015
    Posts:
    16
    Hello.

    I am trying to make a ValueInput use a struct and show a custom inspector -- pretty much just like how a Vector3 is displayed:

    Screen Shot 2022-02-09 at 7.36.33 AM.png


    In ValueInput, there is a SupportsDefaultValue method that seems to control if an inspector will show. It matches against a set of types:

    Code (CSharp):
    1.  
    2.         private static readonly HashSet<Type> typesWithDefaultValues = new HashSet<Type>()
    3.         {
    4.             typeof(Vector2),
    5.             typeof(Vector3),
    6.             typeof(Vector4),
    7.             typeof(Color),
    8.             typeof(AnimationCurve),
    9.             typeof(Rect),
    10.             typeof(Ray),
    11.             typeof(Ray2D),
    12.             typeof(Type),
    13. #if PACKAGE_INPUT_SYSTEM_EXISTS
    14.             typeof(UnityEngine.InputSystem.InputAction),
    15. #endif
    16.         };
    17.  
    18.         public static bool SupportsDefaultValue(Type type)
    19.         {
    20.             return
    21.                 typesWithDefaultValues.Contains(type) ||
    22.                 typesWithDefaultValues.Contains(Nullable.GetUnderlyingType(type)) ||
    23.                 type.IsBasic() ||
    24.                 typeof(UnityObject).IsAssignableFrom(type);
    25.         }
    26.  
    If I use a struct, then the inspector will not show:

    Code (CSharp):
    1.  
    2.     public struct LampIdValue
    3.     {
    4.         public string id;
    5.         public int value;
    6.  
    7.         public static readonly LampIdValue Empty = new LampIdValue { id = string.Empty, value = 0 };
    8.     }
    9. .
    10. .
    11. .
    12. var lampIdValue = ValueInput<LampIdValue>("Lamp ID " + (i + 1), LampIdValue.Empty);
    13.  
    Screen Shot 2022-02-09 at 7.47.40 AM.png

    So, the only way I can get the inspector to show is to make the ValueInput use a UnityObject:

    Code (CSharp):
    1.  
    2.     [Serializable]
    3.     public class LampIdValue : UnityObject
    4.     {
    5.         public string id;
    6.         public int value;
    7.  
    8.         public static readonly LampIdValue Empty = new LampIdValue { id = string.Empty, value = 0 };
    9.     }
    10. .
    11. .
    12. .
    13. var lampIdValue = ValueInput<LampIdValue>("Lamp ID " + (i + 1), LampIdValue.Empty);
    14.  

    Now the problem is, whenever I restart Unity, (or make a code change), the data is lost:

    Screen Shot 2022-02-08 at 4.59.01 PM.png


    Does anyone know why the data is not being preserved?
     
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,079
    I'm poorly versed in the land of custom nodes, but did you try decorating your struct with [Serializable] attribute? I recall that being a requirement for structs to display correctly within UnityVS Variables inspector. Might also help here.
     
  3. jsm174

    jsm174

    Joined:
    Jul 22, 2015
    Posts:
    16
    @PanthenEye - thanks, but we tried that. Unfortunately, the inspector wont show because of how they lock it down to just a few types (Vector2, Vector3, Vector4, Color, AnimationCurve, Rect, Ray, Ray2D, primitives, etc).