Skip to content

Instantly share code, notes, and snippets.

@LuizMoratelli
Created March 12, 2024 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuizMoratelli/dbde3a87697ea095cf56516ac4d6cd33 to your computer and use it in GitHub Desktop.
Save LuizMoratelli/dbde3a87697ea095cf56516ac4d6cd33 to your computer and use it in GitHub Desktop.
[Odin] Create a button to create and add reference to new SO

Simple and easy way to add a new button for create and attach an SO to another, this make easier to create abilities for cards in my game (removing the need of create the SO, find the card, and attach)

Result: image

In Editor.cs

public class MyDataProcessor : OdinAttributeProcessor<MyData>
{
    public override void ProcessChildMemberAttributes(InspectorProperty parentProperty, MemberInfo member, List<Attribute> attributes)
    {
        base.ProcessChildMemberAttributes(parentProperty, member, attributes);


        if (member.Name == "abilities")
        {
            var attribute = attributes.GetAttribute<ListDrawerSettingsAttribute>();
            
            attribute.OnTitleBarGUI = "@MyDataProcessor.CreateNewAbility($root)"; // could use $value to get Abilities[], or $root to get MyData
        }
    }

    static private void CreateNewAbility(CardData value)
    {
        if (SirenixEditorGUI.ToolbarButton(SdfIconType.PlusCircleDotted))
        {
            ScriptableObjectCreator.ShowDialog<MyData>("Assets\\Resources\\abilities", obj =>
            {
                Array.Resize(ref value.abilities, value.abilities.Length + 1); // If you could, use a List instead
                value.abilities[^1] = obj; // Append do not work well
            });
        }
    }
}

In MyData.cs

[ListDrawerSettings()]
public MyData[] abilities;

Special thanks for Spiney on Odin's discord for all help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment