Search Unity

[RELEASED] Odin Inspector & Serializer - The Ultimate Workflow Tool ★★★★★

Discussion in 'Assets and Asset Store' started by jorisshh, Jun 15, 2017.

  1. Schwapo

    Schwapo

    Joined:
    Jul 29, 2019
    Posts:
    10
    You should be able to access the property's value entry and get a reference to your parent class that way:
    Code (CSharp):
    1. public class SomeMonoBehaviour : MonoBehaviour
    2. {
    3.     // [...]
    4. }
    5.  
    6. public class SomeMonoBehaviourProcessor : OdinAttributeProcessor<SomeMonoBehaviour>
    7. {
    8.     public override void ProcessChildMemberAttributes(InspectorProperty parentProperty, MemberInfo member, List<Attribute> attributes)
    9.     {
    10.         var someMonoBehaviour = (SomeMonoBehaviour)parentProperty.ValueEntry.WeakSmartValue;
    11.      
    12.         // [...]
    13.     }
    14. }
     
    Neohun likes this.
  2. Neohun

    Neohun

    Joined:
    Oct 30, 2018
    Posts:
    77
    Thank you so much!
    May I ask a few quick questions for the custom editor window that I create with Odin:
    1 - Custom context menu is awesome but I don't want to allow copy paste, copy to clipboard and set null, which breaks the whole system if it is used accidently which comes as default I have to disable them because I have a complex editor system which has lots of nested objects, list inside table list inside another list etc. and I need to allow only my custom context menu which has delete and rename only for some sections.. [DisableContextMenu] attribute is disabling all context menu, is there any option to disable it for some properties without losing my custom context menu? also applying for every property in a class is really annoying is there a disable option for the whole class but allow only for some field/properties..

    2 - How can I refresh the editor to update the changes immediately because till I move my mouse it doesn't update with some attributes such as [OnValueChanged] I have a workaround though I call Editor.Forcus() func when I complete my run in the func is there any better option, am I missing something?

    3 - Is there any chance that you can implement a horizontal list like table list but rather than vertical, it draws the elements horizontal next to each. I was making a translator window and since there was a limited amount of languages (only the major languages) I have created a field for each other, that'll do for me for now so I ask for future implementations whether you can achieve something like this or not..

    4 - This one is more of a suggestion actually, can you implement a horizontal line (draws a colorful line only) and a [SingleEnumSelect] attribute which allows only one selection for flags enums rather than multiple.. I have implemented my own attribute which works quite well but I guess since it is very easy to implement you can add two more useful attribute to your system which I'm a huge fan by the way I have started a few days ago and it is a must have when we consider how terrible unity's custom editor system, it makes it much more fun and organized.. I'm always against the 3rd party assets which I never use to not have a dependency on them because I don't trust for stability and long term support but since this is an editor asset not runtime I have tried for the first time and this is very well documented and well organized although it needs some improvements for some advanced customizations but still very very good.

    The first question what matters the most actually the others just nice to have :)
     
    Last edited: Oct 30, 2022
  3. ununion

    ununion

    Joined:
    Dec 2, 2018
    Posts:
    275
    hello eveyone,a stupid question here,
    Can I use odin without attributes workflow?because I don't want to add editor attributes to my runtime code.
    I mean is there anyway that i can write a editorwindow class and call odin's ui component?
     
  4. Schwapo

    Schwapo

    Joined:
    Jul 29, 2019
    Posts:
    10
    A lot of Odin's more complicated drawers do not have simple methods that you can just call to draw them, so at some point in the process you're always going to use attributes. Odin has something called AttributeProcessors that allows you to apply attributes to your members programatically which makes it possible to separate your runtime code from your editor code while still using attributes.
     
  5. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    519
    Hi there, Didn't find the answer in thread.
    Is there a way to display an animation / clip preview in an Object Preview (or anything) ?
    This is what I get so far :
    upload_2022-11-18_15-34-15.png
     
  6. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    519
  7. epare025

    epare025

    Joined:
    Oct 9, 2017
    Posts:
    10
    Saw that this asset has issues working with nested prefabs, what are the limitations of odin inspector's compatibility with nested prefabs? What can't I do and how is the nested prefab workflow affected by Odin's lack of support?

    I got Odin before knowing this and am wondering if this asset is of any use since I very much like to use nested prefabs.
     
  8. Schwapo

    Schwapo

    Joined:
    Jul 29, 2019
    Posts:
    10
    Odin Inspector works perfectly fine with nested prefabs, it's Odin Serializer that does not.
    This means that if you use one of Odin's serialized classes like SerializedMonoBehaviour or SerializedScriptableObject you may get weird and unexpected behaviour with prefabs. If you use Unity's default serialisation you should be fine. We're also currently working on a solution to this that could reintroduce full support for prefabs, there is no guarantee since we tried it a few times without success, but it seems to be promising so far.
     
  9. wechat_os_Qy07oG6LBuLwxrt0ao4ZkEwPc

    wechat_os_Qy07oG6LBuLwxrt0ao4ZkEwPc

    Joined:
    Jun 28, 2021
    Posts:
    17
    hi, I have some class Use unity CustomEditor but want to Drawn by odin. How should I Do?
    And I not want to change the class Editor Becouse It's from third party Plug.
     
    Nintendo-Silence likes this.
  10. Torukun_404

    Torukun_404

    Joined:
    Dec 14, 2021
    Posts:
    12
    sorry for my poor English if i make any mistake :)

    Hi, I just get started with Odin inspector, I've tried some attribute in the documentation, But I just encounter a problem.
    None of any attribute work as expected within non monobehavior class at all. But work just fine in when I use them within mono class. Here are some pictures.

    within non mono:
    Unity_1xFdnUaTKR.png

    within mono:
    Unity_vl3nkmTXCo.png

    The code:
    rider64_EJRS6vMq0Y.png
     
  11. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    Try removing the
    [Serializable]
    attribute from the class. From my experience (with an older Odin version), Odin ignores classes with that attribute.
     
    Torukun_404 likes this.
  12. Torukun_404

    Torukun_404

    Joined:
    Dec 14, 2021
    Posts:
    12
    I'm currently using Odin v3.1.9, will this bug gone if I try update the package? Also do the class still serializable without
    [Serializable] attribute or there are other ways around?
     
  13. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    I think (but don't know) Odin sees the Serializable attribute and decides that Unity should be responsible to serialize this class. At the same time, it is part of a SerializedMonoBehaviour which is drawn by Odin. I am not sure if the class is actually serialized by Unity then. At least it is not correctly drawn by Odin.

    Odin Serializer will be able to serialize the class without that attribute. Other serializers might ignore it.

    Anyway, I just tell you my observations and my best guesses about the technical details. I guess @Schwapo or an Odin developer can give you a better answer here (I'm also curious).
     
    Torukun_404 likes this.
  14. asqewfcq2egf

    asqewfcq2egf

    Joined:
    Nov 16, 2018
    Posts:
    15
    how do i make [Button] stop dirtying the scene? even a simple Log() call is dirtying it.
     
  15. RedZAndroidDev

    RedZAndroidDev

    Joined:
    Dec 2, 2016
    Posts:
    7
    Here is a useful video for someone who is new to Odin:
     
    ledshok likes this.
  16. rioneye

    rioneye

    Joined:
    Aug 10, 2015
    Posts:
    12
    Hi,

    I'm getting a few errors while building my app on Android that I have no idea how to debug:

    Code (CSharp):
    1. ArgumentNullException: Value cannot be null.
    2. Parameter name: unityObject
    3. Sirenix.Serialization.UnitySerializationUtility.SerializeUnityObject (UnityEngine.Object unityObject, Sirenix.Serialization.SerializationData& data, System.Boolean serializeUnityFields, Sirenix.Serialization.SerializationContext context) (at C:/Sirenix/OdinSerializer/OdinSerializer/Unity Integration/UnitySerializationUtility.cs:592)
    4. Sirenix.OdinInspector.SerializedMonoBehaviour.UnityEngine.ISerializationCallbackReceiver.OnBeforeSerialize () (at C:/Sirenix/OdinSerializer/OdinSerializer/Unity Integration/SerializedUnityObjects/SerializedMonoBehaviour.cs:44)
    5. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
    Code (CSharp):
    1. ArgumentNullException: Value cannot be null.
    2. Parameter name: unityObject
    3. Sirenix.Serialization.UnitySerializationUtility.DeserializeUnityObject (UnityEngine.Object unityObject, Sirenix.Serialization.SerializationData& data, Sirenix.Serialization.DeserializationContext context, System.Boolean isPrefabData, System.Collections.Generic.List`1[T] prefabInstanceUnityObjects) (at C:/Sirenix/OdinSerializer/OdinSerializer/Unity Integration/UnitySerializationUtility.cs:1371)
    4. Sirenix.Serialization.UnitySerializationUtility.DeserializeUnityObject (UnityEngine.Object unityObject, Sirenix.Serialization.SerializationData& data, Sirenix.Serialization.DeserializationContext context) (at C:/Sirenix/OdinSerializer/OdinSerializer/Unity Integration/UnitySerializationUtility.cs:1360)
    5. Sirenix.OdinInspector.SerializedMonoBehaviour.UnityEngine.ISerializationCallbackReceiver.OnAfterDeserialize () (at C:/Sirenix/OdinSerializer/OdinSerializer/Unity Integration/SerializedUnityObjects/SerializedMonoBehaviour.cs:37)
    6. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
    I have several scripts that use
    SerializedMonoBehaviour
    but I don't know which is the offending script or object. Has anyone run into these before?
     
  17. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    Odin is using [CustomEditor( typeof( Object ), true )] to handle a number of custom things, I think including the Button attribute which is conflicting with some custom editors of our own. Is there any way to get them to co-exist? We could even create a base class with events and have Odin subscribe to it or something similar
     
  18. Tor-Vestergaard

    Tor-Vestergaard

    Joined:
    Mar 20, 2013
    Posts:
    188
    Odin does not in fact use that approach - though in practical effect it does do something similar. Odin decides itself which types it wants to draw, and then it directly injects itself into Unity's internal editor mappings to set itself as the editor for all types it wants to target. It just so happens that the setup of things Odin will choose to inspect by default looks a lot like a [CustomEditor(typeof(Object), true)] setup - if Odin detects that a type already has a custom editor, Odin will respect that and leave the type alone so as not to interfere with its custom drawing.

    What you can do to implement Odin is inherit from OdinEditor instead of Editor if Odin is installed, checked via for example a preprocessor directive like #if ODIN_INSPECTOR - then all the base editor drawing methods such as OnInspectorGUI will draw Odin's inspector instead of Unity's default.
     
    Nihil688 likes this.
  19. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    Odin lives inside the plugins folder where my dll/framework is inside packages so it will always work above mine. I guess your suggestion might work but since the default result is that it does actually not recognize my class currently unless there's a setting somewhere?
     
  20. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    It didn't work unfortunately:


    Code (CSharp):
    1.  [CustomEditor( typeof( Object ), true )]
    2. #if ODIN_INSPECTOR
    3.     internal class CustomObjectEditor : Sirenix.OdinInspector.Editor.OdinEditor
    4. #else
    5.     internal class CustomObjectEditor : UnityEditor.Editor
    6. #endif
    7. {
    8.     public static event Action<object> OnInspectorGUIAction;
    9.     public override void OnInspectorGUI()
    10.     {
    11.        base.OnInspectorGUI();
    12.  
    13.        if( OnInspectorGUIAction != null )
    14.        {
    15.           OnInspectorGUIAction( target );
    16.        }
    17.     }
    18. }
     
  21. Grumpy-Dot

    Grumpy-Dot

    Joined:
    Feb 29, 2016
    Posts:
    93
    Hello, one item in the initial todo list was the ability to have serialized Object snapshots. Would be a cool feature to have these snapshots exposed directly in inspector, without additional files in the project, and be able to apply them to existing components at editor and runtime. Any updates on this or ideas on how implement this?
     
  22. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    I'm not a hundred percent sure what your intentions are. But I have an Editor Extension in the making you might be interested in: It allows you to bind (almost) any value of a component or game object (or any variable in your code) to something I call "Brush". You can then have multiple sets of values (I call them "Themes") to apply a value (a "Paint") to it.
    If this is what you need, write me a PM. I may send you a beta version if this is something useful for you (you already beta tested my asset Beter UI for me some years ago, hope you still find it useful) :)
     
    Grumpy-Dot likes this.
  23. growingmaster

    growingmaster

    Joined:
    Dec 20, 2020
    Posts:
    3
    Hello, I met some problems regarding data saving. I have a list of classes with polymorphism, and I want to save different categories of classes into different format(not DataFormat).
    For instance, I have an abstract class A. Now there are two classes, B and C, inherit A. B has two fields to save while C has three fields. I can use a field to identify the type. And I was wondering if there's any approach to make odin serializer recognize that and save it, just like the type filter attribute does?
    The only way I have for now is have a data class with some field named "field1", "field2" and I need to parse them according to type after deserialization, which might lead to a great amount of unnecessary memory usage.
     
  24. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,951
    Can you elaborate on what you mean by this?

    If polymorphic serialisation is in effect, then Odin will serialise and draw the correct type and all its respective serialisable members.
     
  25. growingmaster

    growingmaster

    Joined:
    Dec 20, 2020
    Posts:
    3
    My concern is about saving and loading. Odin can definitely serialize all the child classes correctly. But when it comes to the process of deserialization, how can it recognize what kind of child class it should be when facing a string of binary? We only have "List<A>" for the deserialization utility.

    I admit I haven't tried it yet. But I guess the basic SerializationUtility.DeserializeValue<T> as written in the sample is not enough to deal with that.
     
  26. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,951
    It handles polymorphic and by-reference serialisation fine. There's nothing extra you need to do.
     
  27. growingmaster

    growingmaster

    Joined:
    Dec 20, 2020
    Posts:
    3
    Cool, thank you. I would try it then.
     
  28. HajiyevEl

    HajiyevEl

    Joined:
    Feb 19, 2020
    Posts:
    47
    Hi. How can i make [ListDrawerSettings(CustomAddFunction)] work with [TableList] ?
    They work separately, but not together.
    I mean, if list is drawn as a table and i add element, than 'CustomAddFunction' will not fire.
    And if i disable table view and add element, 'CustomAddFunction' will fire.

    Also, about HideInheritedChildProperties. Is there any way for same logic to be applies to fields?
    Also, ListDrawerSettings(OnTitleBarGUI = "DrawRefreshButton") does not work when list is drawn as table [TableList] (refresh button doesnt show up), but works if table view disabled (refresh button shows up).
     
    Last edited: Mar 19, 2024
  29. frank231love

    frank231love

    Joined:
    Jun 2, 2019
    Posts:
    23
    "Odin Serializer ArchitectureInfo initialization with defaults (all unaligned read/writes disabled). Odin Serializer detected non-white-listed runtime platform IPhonePlayer; disabling all unaligned memory read/writes. libc++abi: terminating due to uncaught exception of type Il2CppExceptionWrapper

    I am currently using: Odin version: 3.1.14.3 Unity version: 2022.3.10 Encountering this issue, how should I handle it? Now encountering this issue when publishing to Xcode."
     
  30. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    Try to change the Api Compatibility Level in the player settings (from .Net Standard to .Net Framework). This solved a similar issue I encountered.
     
  31. frank231love

    frank231love

    Joined:
    Jun 2, 2019
    Posts:
    23
    thank you
     
  32. HajiyevEl

    HajiyevEl

    Joined:
    Feb 19, 2020
    Posts:
    47
    Hi! How can i enable scrollbar when list is TableList with ReadOnly attribute added? Scrollview bar gets locked and impossible to scroll.
    Screenshot 2024-03-26 113303.png

    P.S. I don't want Add or Delete buttons to be shown.

    EDIT: Solved.
    Removed [ReadOnly] attribute from parent list that had [TableList] attribute.
    Added 'isReadOnly' param to parent list that had [TableList] attribute.
    Added [ReadOnly] attribute to variables of parent list's members.
    Changed [ReadOnly] attribute to 'isReadOnly' param on child list that had [TableList] attribute.
     
    Last edited: Mar 26, 2024
  33. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,194
    Hi there! I am the creator of S.A.M. - The Streamable Assets Manager. I do not own Odin inspector but have several users that are having compatibility issues between my asset and yours. I'm hoping you can help me figure out what's going on.

    The issues:
    For some components, when viewed in the inspector the editor become very laggy and sometimes even crashes. The inspector just shows the first Header Attribute (if there is one present) repeated multiple times.

    Odin_repeated_header.png

    Users also get this exception in some instances:
    odin_error_1.png

    I have narrowed the issue down to my use of a custom PropertyAttribute and DecoratorDrawer:

    Code (CSharp):
    1.  
    2. public class SAMGuideButton : PropertyAttribute
    3. {
    4.     public string chapterName, sectionName;
    5.     public SAMGuideButton(string chapterName, string sectionName)
    6.     {
    7.         this.chapterName = chapterName;
    8.         this.sectionName = sectionName;
    9.     }
    10. }
    11.  
    Code (CSharp):
    1. [CustomPropertyDrawer(typeof(SAMGuideButton))]
    2. class SAMGuideButtonDrawer : DecoratorDrawer
    3. {
    4.     SAMGuideButton button
    5.     {
    6.         get { return ((SAMGuideButton)attribute); }
    7.     }
    8.  
    9.     public override void OnGUI(Rect position)
    10.     {
    11.         var position2 = new Rect(position.x + position.width - 22f, position.y, 22f, position.height);
    12.  
    13.         if (GUI.Button(position2, guideButtonLabel))
    14.             Application.OpenURL($"https://deepspacelabs.net/html/sam/docs/documentation_main.html?section={button.chapterName.RemoveWhitespace()}_{button.sectionName.RemoveWhitespace()}");
    15.     }
    16.  
    17.     readonly GUIContent guideButtonLabel = new GUIContent("?", "Hold Shift and Click to open new guide window; otherwise, Chapter/Section will open in existing guide if there is one.");
    18. }
    Code (CSharp):
    1. [SAMGuideButton("Hierarchy Organizers", "Overview")]
    2. [Header("Basic Organizer Settings")]
    3. [SerializeField]
    4. int minChunksToAddPerFrame = 10;
    Removing the Header attribute does not resolve the issue, so it doesn't appear to be an issue with using multiple attributes. There are no issues if Odin Inspector is not installed in the project. Any help would be greatly appreciated, thanks!
     
    Last edited: Apr 10, 2024
  34. thangnh111

    thangnh111

    Joined:
    Aug 4, 2020
    Posts:
    21
    i'm currently using property tree to draw a class editor, as default it will be draw at bottom of the window, is there any way that i can move it to top right area
     
  35. Tor-Vestergaard

    Tor-Vestergaard

    Joined:
    Mar 20, 2013
    Posts:
    188
    We've gotten a lot of similar reports, and it appears related to a change we made in 3.2 to fix another issue with UI Toolkit. We are investigating it and should hopefully have a fix ready and released within the next week or so :)
     
    gilley033 and spiney199 like this.
  36. jesta

    jesta

    Joined:
    Jun 19, 2010
    Posts:
    294
    I chose the worst possible time to show Odin to our team. Because of the StackOverflowException caused by Header attributes, the rest of the team quickly dismissed the tool as buggy.

    I've used Odin in all my previous teams since 2017 without any issue, so it's extra sad and frustrating.
     
  37. Tor-Vestergaard

    Tor-Vestergaard

    Joined:
    Mar 20, 2013
    Posts:
    188
    I can report that we have a definite fix for this issue, and will be releasing it in a beta patch on our website this week, and likely on the Asset Store the week after, when the full 3.3 beta goes into stable release. Until then, please feel free to PM me or contact me on our support Discord, and I will be happy to provide an advance build with the fix included.
     
    gilley033 likes this.
  38. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    81
    Hi, if I already have my own custom UnityEditor.Editor for my scripts, does that mean I have to re-do all of them? Can I maybe change my base classes from UnityEditor.Editor to some OdinEditor and keep everything else the same?
     
    Last edited: Apr 20, 2024
  39. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,951
    UnityEditor.Editor
    's bypass the way Odin's works so Odin's attribute system won't work for types they are drawing. Even OdinEditor's that are doing manual drawing bypass this too.

    Generally with Odin you will never actually need a traditional editor. The built in attributes cover nearly all common use cases, then other tools such as Value or Attribute Drawers can draw things the 'Odin way', along with utilities such as Attribute Processor or Property Processors to modify the inspector without having to draw fields manually.
     
    jakesee likes this.
  40. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    81
    Hi, anyone else getting an an annoying warning with Odin Inspector and Serializer 3.2.1.0 ? It appears everytime code change and recompiles.

    Code (CSharp):
    1. An Odin PropertyTree instance is being garbage collected without first having been disposed. PropertyTree instances must be disposed once they are no longer needed. This instance was allocated at the following location:
    2.  
    3.   at Sirenix.OdinInspector.Editor.PropertyTree..ctor () [0x0005e] in Y:\Repos\Sirenix-Development-Framework\Sirenix Solution\Sirenix.OdinInspector.Editor\Core\PropertyTree.cs:293
     
  41. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,951
    Not myself. Are you manually creating and drawing a PropertyTree anywhere?
     
  42. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    81
    @spiney199 !!! It's you again (not complaining), seems you are on the forums all day. Are you a moderator or working for Unity? Just curious.

    I am not drawing PropertyTree on my own, no idea what they are, but the only other recent change is the use of XNode, so together they may be creating some problems?
     
  43. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,951
    They shouldn't create problems for one another. Not the base one at least. Are you using the Odin compatible fork? https://github.com/KAJed82/xNode
     
  44. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    81
    Ah... I wasn't aware there is a different fork. When you say "not the base one at least" are you referring to the Siccity/xNode:master, that's what I am using.
     
  45. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,951
    Yes. Otherwise I can't think of what would be causing it. It could be anything that's drawing an Odin powered inspector and not disposing of the PropertyTree afterwards (which is Odin's substitute for Unity's SerializedObject).
     
    jakesee likes this.
  46. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    81
    Sorry, another question, may I ask what is Odin's equivalent of CanEditMultipleObjects and UnityEditor.Editor.targets? I need to generate values for more than a hundred scriptable objects, where some of the values are related to other scriptable objects in the selection, so I need reference to the selected targets.

    I've look at the OdinEditorWindow, but I don't really want to redo all my editor windows... :(
     
    Last edited: Apr 22, 2024 at 4:33 AM
  47. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,951
    In what context? A OdinEditor? I already mentioned you don't need to write traditional editors with Odin.
     
  48. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    81
    In the inspector, say for example, I have 100 ScriptableObjects, with UnityEditor.Editor and [CanEditMultipleObjects] and Editor.targets, I can generate GUID for all 100 at once. With Odin, I can use a [Button] attribute, but how do I get the target without searching the the AssetDatabase?

    Also, it's not an issue of whether I need to write traditional UnityEditor.Editor, it is that I already have them and Odin doesn't want to work with them.
     
  49. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,951
    With Odin stuff, you always, in some form or another, have access to the PropertyTree. Again, this is Odin's replacement for Unity's SerializedObject, and can be used to do similar things (but better). It's comprised of InspectorProperty's, which replace Unity's SerializedObject.

    Here's two examples of how you can do it:
    Code (CSharp):
    1. public sealed class ExampleObject : ScriptableObject
    2. {
    3.     [SerializeField]
    4.     private string _guid;
    5.  
    6. #if UNITY_EDITOR
    7.     [Button(DisplayParameters = false)] // Odin supplies the InspectorProperty automatically
    8.     private void GenerateGUID_01(Sirenix.OdinInspector.Editor.InspectorProperty property)
    9.     {
    10.         var root = property.SerializationRoot; // this is the property for the scriptable itself
    11.         var weakTargets = root.ValueEntry.WeakValues; // this is akin to Editor.targets, weakly typed
    12.         foreach (var weakTarget in weakTargets)
    13.         {
    14.             var exampleObject = (ExampleObject)weakTarget;
    15.             exampleObject._guid = System.Guid.NewGuid().ToString();
    16.         }
    17.         root.RecordForUndo();
    18.     }
    19.  
    20.     // OR
    21.     [Button(DisplayParameters = false)]
    22.     private void GenerateGUID_02(Sirenix.OdinInspector.Editor.InspectorProperty property)
    23.     {
    24.         var root = property.SerializationRoot; // get the root
    25.         var guidProperty = root.FindChild(x => x.Name == "_guid", false); // find the property for the guid field
    26.         if (guidProperty != null)
    27.         {
    28.             var weakValues = guidProperty.ValueEntry.WeakValues;
    29.             int count = weakValues.Count;
    30.  
    31.             // loop through the property's weak values and assign them
    32.             for (int i = 0; i < count; i++)
    33.             {
    34.                 weakValues[i] = System.Guid.NewGuid().ToString();
    35.             }
    36.         }
    37.         root.RecordForUndo();
    38.     }
    39. #endif
    40. }
    Though I would just make the guid just automatically initialise with a field-initialiser, or perhaps an Asset Post Processor, rather than manually through a button. If I had some scriptable objects already that needed a GUID generated, I'd just write a once-off bit of MenuItem code to do that.

    It's more they take control away from Odin. Odin can't magically inject itself into your GUI code. It needs to be the one in charge of drawing the inspector for a given scriptable object or component.
     
  50. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    81
    Appreciate the very detailed and complete examples, although "Odin supplies the InspectorProperty automatically" would suffice. There's not a lot of info when I google, mostly flooded with the Swedish god.

    =========

    As a side note:

    From the Asset Store page:

    Effortless Integration:
    Odin is extremely easy to use and won't break your existing workflow. In fact, you don't even need to inherit from anything, which means your existing editors will continue to work even with Odin.

    But in fact they don't, so to me that's quite misleading, should have warned that we need abandon the existing and re-write all the editors for most of the features.