Getting started with the Unity plugin Odin

Store address: Odin
Odin is a plug-in to expand the editor, which can serialize various data and conveniently create various editor interfaces, as follows:
insert image description here

insert image description here
After importing the plug-in, as shown in Tool–Odin Inspector–Getting Started, you can view the overview interface provided by Odin.
insert image description here
Clicking Open Attributes Overview will display attributes and field editing related examples, allowing us to easily edit the content of the Inspector interface. Clicking Leran More will display some window-related examples, which is convenient for customizing some pop-up interfaces.
insert image description here
Some Scene samples are provided below the overview for further study.
insert image description here
First, view the field-related examples, as shown in the figure above, the left side is the category, the upper right side is the content drawn on the Inspector interface, and the lower right side is the corresponding code, which can be copied and used directly.
Odin provides more than 100 attributes (Attribute), you only need to add the attribute to the field to display the above style. Attributes are simply used to mark elements, such as fields, methods, and classes. It's hard to remember so many features, and it's usually necessary to find a suitable style.

	//AssetsOnly表示只能拖拽Assets目录下的资源,场景中的资源是无法拖动
	[AssetsOnly]
	public GameObject SomePrefab;
	
	//SceneObjectsOnly相反,只能拖拽Scene场景中的资源
	[SceneObjectsOnly]
	public GameObject SomeSceneObject;

The following summarizes some of the more commonly used features

1. Limit the value range, slider, progress bar

insert image description here

    [Range(0, 100)]
    public int Field = 2;
    
    [MinValue(0)]
    public int IntMinValue0;

    [MaxValue(0)]
    public int IntMaxValue0;

    [ProgressBar(0, 100)]
    public float ProgressBar = 50;

2. Trigger a specific method when the value changes

	[OnValueChanged("OnValueChanged")]
    public int DelayedField;
    
    //ShowInInspector用于将属性显示到界面上
    [ShowInInspector]
    [OnValueChanged("OnValueChanged")]
    public string DelayedProperty {
    
     get; set; }

    private void OnValueChanged()
    {
    
    
        Debug.Log("Value changed!");
    }

3. Color

insert image description here

	//字段添加颜色
    [GUIColor(0.3f, 0.8f, 0.8f, 1f)]
    public int ColoredInt1;
    
    //调色板
    [ColorPalette("Fall")]
    public Color Color1;
    
    //按钮添加颜色
    [GUIColor(0, 1, 0)]
    [Button("ButtonName", ButtonSizes.Small)]
    private void ButtonMethod()
    {
    
    
    }

4. Prompt information

insert image description here

    //HideLabel用于隐藏字段名
    [Title("Vector3标题")]
    [HideLabel]
    public Vector3 WideVector1;
    
    //Space用于添加一行空隙
    [Space]
    [InfoBox("提示1")]
    public int Int1;
    
    //MyGameObject为空时才会提示
    [Required]
    public GameObject MyGameObject;

5. Input verification

insert image description here

    [ValidateInput("HasMeshRenderer")]
    public GameObject DynamicMessage;
    
    private bool HasMeshRenderer(GameObject gameObject, ref string errorMessage)
    {
    
    
        if (gameObject == null) return true;

        if (gameObject.GetComponentInChildren<MeshRenderer>() == null)
        {
    
    
            errorMessage = "\"" + gameObject.name + "\" 必须包含MeshRenderer组件";
            return false;
        }

        return true;
    }
    
    
    [ValidateInput("CheckSpace", "字符串不能有空格", InfoMessageType.Warning)]
    public string Message = "Dynamic";

    private bool CheckSpace(string value)
    {
    
    
        return value.IndexOf(' ') < 0;
    }

6. Dropdown list

insert image description here

    [ValueDropdown("TextureSizes")]
    public int SomeSize1;
    
    private static int[] TextureSizes = new int[] {
    
     256, 512, 1024 };
    
    
    [ValueDropdown("FriendlyTextureSizes")]
    public int SomeSize2;
    
    private static IEnumerable FriendlyTextureSizes = new ValueDropdownList<int>()
    {
    
    
        {
    
     "Small", 256 },
        {
    
     "Medium", 512 },
        {
    
     "Large", 1024 },
    };
    

    [ValueDropdown("TreeViewOfInts", ExpandAllMenuItems = true)]
    public List<int> IntTreview = new List<int>() {
    
     1, 2, 7 };
    
    private IEnumerable TreeViewOfInts = new ValueDropdownList<int>()
    {
    
    
        {
    
     "Node 1/Node 1.1", 1 },
        {
    
     "Node 1/Node 1.2", 2 },
        {
    
     "Node 2/Node 2.1", 3 },
        {
    
     "Node 3/Node 3.1", 4 },
        {
    
     "Node 3/Node 3.2", 5 },
        {
    
     "Node 1/Node 3.1/Node 3.1.1", 6 },
        {
    
     "Node 1/Node 3.1/Node 3.1.2", 7 },
    };

7. Grouping

insert image description here

    //水平分组
    [HorizontalGroup] 
    public float num;
    
    [HorizontalGroup, Button(ButtonStyle.Box)]
    private void Full(float a, float b, out float c)
    {
    
    
        c = a + b;
    }
    
    //Box分组
    [BoxGroup("Titles")]
    public int A;

    [BoxGroup("Titles")]
    public int B;
    
    //按钮分组
    [ButtonGroup]
    private void C() {
    
     }

    [ButtonGroup]
    private void D() {
    
     }

8. Collection

insert image description here
1. Note that serialized dictionaries must inherit SerializedMonoBehaviour, but List does not

public class Odin学习 : SerializedMonoBehaviour
{
    
    
    public Dictionary<int, Material> IntMaterialLookup;
    
    [OnInspectorInit]
    private void CreateData()
    {
    
    
        IntMaterialLookup = new Dictionary<int, Material>()
        {
    
    
            {
    
     1, ExampleHelper.GetMaterial() },
            {
    
     7, ExampleHelper.GetMaterial() },
        };
    }
}

insert image description here
2. List can also be used without adding features. Drag the slider on the left to adjust the order of elements. TableList can convert List to table form. Click the button to the left of the plus sign to switch to the original list form.

    public List<float> FloatList;
    
    [Range(0, 1)]
    public float[] FloatRangeArray;
    
    [TableList(ShowIndexLabels = true, AlwaysExpanded = true)]
    public List<SomeCustomClass> TableListWithIndexLabels = new List<SomeCustomClass>()
    {
    
    
        new SomeCustomClass(),
        new SomeCustomClass(),
    };
    
    [Serializable]
    public class SomeCustomClass
    {
    
    
        [TableColumnWidth(57)]
        [PreviewField(Alignment = ObjectFieldAlignment.Center)]
        public Texture Icon;

        [TextArea]
        public string A, B;
    }

9. Conditions

insert image description here

    public bool IsToggled;

    [DisableIf("IsToggled")]
    public int DisableIfToggled;
    
    [EnableIf("IsToggled")]
    public int EnableIfToggled;
    
    [DisableInEditorMode]
    public GameObject A;

    [DisableInPlayMode]
    public Material B;

    [HideIf("IsToggled")]
    public Vector3 HiddenWhenToggled;

    [ShowIf("IsToggled")]
    public Vector2 VisibleWhenToggled;

10. Resource list

insert image description here

	//显示该路径下的材质,路径前面的Assets不用写
    [AssetList(Path = "Materials/")]
    public List<Material> AssetList;
    
    [AssetList(AssetNamePrefix = "Line")]
    public List<Material> MaterialsStartingWithLine;

11. window

insert image description here

public class Odin窗口 : OdinEditorWindow
{
    
    
    [MenuItem("Tools/简单窗口")]
    private static void OpenWindow()
    {
    
    
        var window = GetWindow<Odin窗口>();
        window.position = GUIHelper.GetEditorWindowRect().AlignCenter(500, 500);
    }

    [EnumToggleButtons]
    public ViewTool SomeField;
}

Guess you like

Origin blog.csdn.net/sinat_34014668/article/details/125957699