Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Assets [WIP] Odin Inspector & Serializer Looking for Feedback

Discussion in 'Works In Progress' started by jorisshh, Feb 22, 2017.

  1. bjarkeck

    bjarkeck

    Joined:
    Oct 26, 2014
    Posts:
    272
  2. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    919
  3. Kripll

    Kripll

    Joined:
    Jan 20, 2017
    Posts:
    7
    Hello. How to serialize Dictionary when I put it in class that is used to display Toggle?

    Code (CSharp):
    1. public class ActionPerson : SerializedMonoBehaviour
    2. {
    3.     [Toggle("Enabled")]
    4.     public MovePosition Move = new MovePosition();
    5.  
    6.     [Serializable]
    7.     public class MovePosition
    8.     {
    9.         public bool Enabled;
    10.  
    11.         [HideInInspector]
    12.         public string Title;
    13.  
    14.         public enum Chouse
    15.         {
    16.             Transform,
    17.             Vector
    18.         }
    19.  
    20.         [EnumToggleButtons]
    21.         public Chouse Parametrs;
    22.  
    23.         [ShowInInspector]
    24.         [DictionaryDrawerSettings(DisplayMode = DictionaryDisplayOptions.OneLine)]
    25.         [SerializeField]
    26.         public Dictionary<GameObject, int> objectsPosition = new Dictionary<GameObject, int>();
    27.  
    28.  
    29.         [LabelText("startPosition")]
    30.         [ShowIf("Parametrs", Chouse.Vector)]
    31.         public Vector3 startPosition;
    32.         [LabelText("endtPosition")]
    33.         [ShowIf("Parametrs", Chouse.Vector)]
    34.         public Vector3 endPosition;
    35.  
    36.         [LabelText("startPosition")]
    37.         [ShowIf("Parametrs", Chouse.Transform)]
    38.         public Transform startPosTran;
    39.         [LabelText("endtPosition")]
    40.         [ShowIf("Parametrs", Chouse.Transform)]
    41.         public Transform endPosTran;
    42.     }
    43. }
     
  4. Sirenix

    Sirenix

    Joined:
    Feb 13, 2017
    Posts:
    1
    Your MovePosition class is currently serialized by Unity, not Odin - remove [Serializable] from the class, and then Odin will serialize it. See this FAQ answer for more info.
     
  5. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,071
    Hello, I have a question about your serializer.

    I'm using it to save the game, and saving/loading it from a file. I also save a version number in the beginning of the file.

    This version number is so in the future when I release the game, if I change something post release (like the name of a variable, or deleting a variable), I can change the current version number - then upon loading that old save, it can detect that it's out of date...

    Problem is, if a variable in serialized format isn't in the class upon deserialization, wouldn't that make the variable not be created?
     
  6. Tor-Vestergaard

    Tor-Vestergaard

    Joined:
    Mar 20, 2013
    Posts:
    175
    Indeed it would - if we have data for a field with a given name, but there is no serialized field (or field marked with [FormerlySerializedAs]) that matches the name, then the data is discarded and skipped over. I think there are a variety of possible solutions here, none of them entirely simple. If you'd like a more detailed discussion about this, feel free to join us on our discord.
     
    Last edited: May 30, 2018
    joshcamas likes this.
  7. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,071
    The discord link is invalid :(
     
  8. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    785
    I have some fields marked with NonSerialized (some) and FoldoutGroup (a lot). Can i tell odin somehow to not include the nonserialized ones in the inspector?
     
  9. Tor-Vestergaard

    Tor-Vestergaard

    Joined:
    Mar 20, 2013
    Posts:
    175
    Remove FoldoutGroup from the fields that are non-serialized? Then they will not show up in the inspector. Otherwise, as a general rule you can add [HideInInspector] to fields to make them never show up in the inspector.
     
  10. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    785
    The code was autogenerated, for about a hundred classes and hand-edited afterwards (cannot be easily regenerated). I only find&replace-d a similar attribute. Looks like i have to handle that differently then.
     
  11. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
  12. Tor-Vestergaard

    Tor-Vestergaard

    Joined:
    Mar 20, 2013
    Posts:
    175
    rakkarage likes this.
  13. Razziel

    Razziel

    Joined:
    Nov 1, 2016
    Posts:
    5
    BUG on Unity version 2018.2.0f2:
    Variables of type unity events are shown in the inspector as such:



    I will now copy paste the logs:

    Encountered the following exception when trying to instantiate a drawer of type UnityEventDrawer<UnityEvent>
    UnityEngine.Debug:Log(Object)
    Sirenix.OdinInspector.Editor.<>c__DisplayClass35_0:<GetAllDrawers>b__2(DrawerInfo)
    System.Linq.<CreateWhereIterator>c__Iterator1D`1:MoveNext()
    Sirenix.Utilities.<Append>d__19`1:MoveNext() (at F:/Sirenix/Sirenix Solution/Sirenix.Utilities/Extensions/LinqExtensions.cs:343)
    System.Linq.Enumerable:ToArray(IEnumerable`1)
    Sirenix.OdinInspector.Editor.DrawerLocator:GetAllDrawers(Type, Type, List`1, Boolean)
    Sirenix.OdinInspector.Editor.DrawerLocator:GetValueDrawers(Type) (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Infos/InspectorPropertyInfo.cs:710)
    Sirenix.OdinInspector.Editor.DrawerLocator:GetDrawersForMemberInfo(MemberInfo, Type, Boolean) (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Infos/InspectorPropertyInfo.cs:524)
    Sirenix.OdinInspector.Editor.DrawerLocator:GetDrawersForProperty(InspectorProperty) (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Infos/InspectorPropertyInfo.cs:535)
    Sirenix.OdinInspector.Editor.InspectorUtilities:DrawProperty(InspectorProperty, GUIContent)
    Sirenix.OdinInspector.Editor.InspectorUtilities:DrawProperty(InspectorProperty)
    Sirenix.OdinInspector.Editor.InspectorUtilities:DrawPropertiesInTree(PropertyTree)
    Sirenix.OdinInspector.Editor.PropertyTree:Draw(Boolean)
    Sirenix.OdinInspector.Editor.OdinEditor:DrawTree() (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Children/PropertyValueCollection.cs:541)
    Sirenix.OdinInspector.Editor.OdinEditor:DrawOdinInspector() (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Children/PropertyValueCollection.cs:533)
    Sirenix.OdinInspector.Editor.OdinEditor:OnInspectorGUI() (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Children/PropertyValueCollection.cs:516)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)




    AmbiguousMatchException: Ambiguous matching in method resolution
    System.Reflection.Binder.FindMostDerivedMatch (System.Reflection.MethodBase[] match) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/Binder.cs:106)
    System.MonoType.GetMethodImpl (System.String name, BindingFlags bindingAttr, System.Reflection.Binder binder, CallingConventions callConvention, System.Type[] types, System.Reflection.ParameterModifier[] modifiers) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/MonoType.cs:245)
    System.Type.GetMethod (System.String name, BindingFlags bindingAttr) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Type.cs:787)
    Sirenix.OdinInspector.Editor.UnityPropertyHandlerUtility..cctor () (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Context/PropertyContext.cs:60)
    Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Sirenix.OdinInspector.Editor.UnityPropertyHandlerUtility
    Sirenix.OdinInspector.Editor.UnityPropertyDrawer`2[UnityEditorInternal.UnityEventDrawer,UnityEngine.Events.UnityEvent]..ctor ()
    Sirenix.OdinInspector.Editor.Drawers.UnityEventDrawer`1[UnityEngine.Events.UnityEvent]..ctor () (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/Value Drawers/Vector4Drawer.cs:66)
    System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:513)
    Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:519)
    System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:528)
    System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:77)
    System.Activator.CreateInstance (System.Type type, Boolean nonPublic) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:372)
    System.Activator.CreateInstance (System.Type type) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:254)
    Sirenix.OdinInspector.Editor.DrawerLocator.GetDrawer (System.Type drawerType, Boolean forceUniqueDrawerInstance)
    Sirenix.OdinInspector.Editor.DrawerLocator+<>c__DisplayClass35_0.<GetAllDrawers>b__2 (Sirenix.OdinInspector.Editor.DrawerInfo info)
    UnityEngine.Debug:LogException(Exception)
    Sirenix.OdinInspector.Editor.<>c__DisplayClass35_0:<GetAllDrawers>b__2(DrawerInfo)
    System.Linq.<CreateWhereIterator>c__Iterator1D`1:MoveNext()
    Sirenix.Utilities.<Append>d__19`1:MoveNext() (at F:/Sirenix/Sirenix Solution/Sirenix.Utilities/Extensions/LinqExtensions.cs:343)
    System.Linq.Enumerable:ToArray(IEnumerable`1)
    Sirenix.OdinInspector.Editor.DrawerLocator:GetAllDrawers(Type, Type, List`1, Boolean)
    Sirenix.OdinInspector.Editor.DrawerLocator:GetValueDrawers(Type) (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Infos/InspectorPropertyInfo.cs:710)
    Sirenix.OdinInspector.Editor.DrawerLocator:GetDrawersForMemberInfo(MemberInfo, Type, Boolean) (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Infos/InspectorPropertyInfo.cs:524)
    Sirenix.OdinInspector.Editor.DrawerLocator:GetDrawersForProperty(InspectorProperty) (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Infos/InspectorPropertyInfo.cs:535)
    Sirenix.OdinInspector.Editor.InspectorUtilities:DrawProperty(InspectorProperty, GUIContent)
    Sirenix.OdinInspector.Editor.InspectorUtilities:DrawProperty(InspectorProperty)
    Sirenix.OdinInspector.Editor.InspectorUtilities:DrawPropertiesInTree(PropertyTree)
    Sirenix.OdinInspector.Editor.PropertyTree:Draw(Boolean)
    Sirenix.OdinInspector.Editor.OdinEditor:DrawTree() (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Children/PropertyValueCollection.cs:541)
    Sirenix.OdinInspector.Editor.OdinEditor:DrawOdinInspector() (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Children/PropertyValueCollection.cs:533)
    Sirenix.OdinInspector.Editor.OdinEditor:OnInspectorGUI() (at F:/Sirenix/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/Children/PropertyValueCollection.cs:516)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)
     
  14. Tor-Vestergaard

    Tor-Vestergaard

    Joined:
    Mar 20, 2013
    Posts:
    175
    This is issue #365 (amongst others, it has been reported many times). Please see linked issue for more information.
     
  15. prduquesne

    prduquesne

    Joined:
    Mar 12, 2018
    Posts:
    14
    Absoluteley incredible! Well DONE!! I can see myself using this in the near future.
     
  16. jorisshh

    jorisshh

    Joined:
    Oct 6, 2009
    Posts:
    1,522
    Since Odin has been out for over a year now, we're closing this thread.
    Thanks for all the amazing feedback and comments over the past many months. Odin Inspector & Serializer will keep improving thanks to you fine folk!

    Please move to the [Released] thread or Discord for future support and updates :)
     
  17. RkSanders

    RkSanders

    Joined:
    Jun 28, 2011
    Posts:
    25
    Hi, loving the plugin. Is it possible to have something like the FoldoutGroup attribute use a static bool to know when it's expanded? I'd like to expand/collapse all FoldoutGroups with the same name. Exactly how expanding/collapsing a component in the inspector works.

    Thanks!
     
  18. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    785
    You can wrap the static bool with a property
     
  19. Razmot

    Razmot

    Joined:
    Apr 27, 2013
    Posts:
    303
    are the ECS functions / inspectors available somewhere ? I don't care if it's not completely stable - neither Ecs nor my own code are stable :p
     
  20. RkSanders

    RkSanders

    Joined:
    Jun 28, 2011
    Posts:
    25
    I am I missing something? This is what I had but it doesn't update the bool value.

    Code (CSharp):
    1.         public static bool GLOBAL_SETTINGS;
    2.         public bool GlobalSettings {
    3.             get { return GLOBAL_SETTINGS; }
    4.             set { GLOBAL_SETTINGS = value; }
    5.         }
    6.  
    7.         [FoldoutGroup("$GlobalSettings")]
    8.         public float variable;
     
  21. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    785
    It was just the first thing that came to my mind. Now that i had a look at it, it seems Foldoutgroup doesn't have an overload that accepts a member identifying string, your example would only change the title of the group.
    It's not as pretty, but you could decorate all those properties with the HideIf attribute instead that uses a member lookup.
     
  22. vizgl

    vizgl

    Joined:
    Nov 4, 2014
    Posts:
    38
    Hi,
    I have made generic class WeightedRandomElement<T> and made drawer
    class WeightedRandomElementDrawer<T> : OdinValueDrawer<WeightedRandomElement<T>>

    It's working, but if I create child class of WeightedRandomElement like:
    class WeightedRandomInt: WeightedRandomElement<int>

    custom value drawer stoped working on new class. Where I can find option to make drawer work on children?
     
  23. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    820
    what is the easiest way to add button/action to every element in the list? thanks! great asset
     
    joshcamas likes this.
unityunity