Odin Inspector 系列教程 — On Inspector Init Attribute

在编译后的第一次绘制进行初始化设置。

using Sirenix.OdinInspector;
using Sirenix.Utilities.Editor;
using System;
using UnityEngine;

public class OnInspectorInitAttributeExample : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }
    // Display current time for reference.
    [ShowInInspector, DisplayAsString, PropertyOrder(-1)]
    public string CurrentTime
    {
        get
        {
            GUIHelper.RequestRepaint();
            return DateTime.Now.ToString();
        }
    }

    //当这个字符串第一次被绘制到检查器中时,OnInspectorInit执行。
    //当实例被重新编译时,会再次执行一次。
    [OnInspectorInit("@TimeWhenExampleWasOpened = DateTime.Now.ToString()")]
    public string TimeWhenExampleWasOpened;

    // OnInspectorInit在属性在检查器中被实际解析之前不会执行。
    //Odin的属性系统是延迟计算的,所以直到折叠展开才会进行OnInspectorInit初始化。
    [FoldoutGroup("Delayed Initialization", Expanded = false, HideWhenChildrenAreInvisible = false)]
    [OnInspectorInit("@TimeFoldoutWasOpened = DateTime.Now.ToString()")]
    public string TimeFoldoutWasOpened;
}

更多教程内容详见:革命性Unity 编辑器扩展工具 — Odin Inspector 系列教程

发表评论