美文网首页
Odin Inspector 系列教程 --- Required

Odin Inspector 系列教程 --- Required

作者: 忆中异 | 来源:发表于2021-11-29 10:09 被阅读0次

    Required Attribute特性:用于任何对象属性,如果对应属性没有赋值,则在检查器中出现对应的提示消息。

    image
    直接添加特性
    image
        [Required]
        public GameObject MyGameObject;
    
    
    也可以自定义消息
    image
        [Required("自定义错误消息.")]
        public Rigidbody MyRigidbody;
    
    
    当然可以使用$关键字使用动态消息
    image
        public string DynamicMessage = "Dynamic Message";
        [Required("$DynamicMessage")]
        public GameObject GameObject_DynamicMessage;
    
        [Required("$ReturnStringFunction")]
        public GameObject GameObject_DynamicMessage1;
        public string ReturnStringFunction()
        {
            return "菜鸟海澜";
        }
    
    
    也可以显示不同的消息类型,但是None和Info不显示,目前不知道就是这么设定的还是BUG?、
    image
        [Required("$DynamicMessage", InfoMessageType.None)]
        public GameObject GameObject_None;
        [Required("$DynamicMessage", InfoMessageType.Info)]
        public GameObject GameObject_Info;
        [Required("$DynamicMessage", InfoMessageType.Warning)]
        public GameObject GameObject_Warning;
        [Required("$DynamicMessage",InfoMessageType.Error)]
        public GameObject GameObject_Error;
    
    
    完整示例代码
    using Sirenix.OdinInspector;
    using UnityEngine;
    
    public class RequiredExample : MonoBehaviour
    {
    
        [Required]
        public GameObject MyGameObject;
    
        [Required("自定义错误消息.")]
        public Rigidbody MyRigidbody;
    
        public string DynamicMessage = "Dynamic Message";
        [Required("$DynamicMessage")]
        public GameObject GameObject_DynamicMessage;
    
        [Required("$ReturnStringFunction")]
        public GameObject GameObject_DynamicMessage1;
        public string ReturnStringFunction()
        {
            return "菜鸟海澜";
        }
    
        [Required("$DynamicMessage", InfoMessageType.None)]
        public GameObject GameObject_None;
        [Required("$DynamicMessage", InfoMessageType.Info)]
        public GameObject GameObject_Info;
        [Required("$DynamicMessage", InfoMessageType.Warning)]
        public GameObject GameObject_Warning;
        [Required("$DynamicMessage",InfoMessageType.Error)]
        public GameObject GameObject_Error;
    }
    
    

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

    链接:https://www.jianshu.com/p/c2590cf8ab89

    相关文章

      网友评论

          本文标题:Odin Inspector 系列教程 --- Required

          本文链接:https://www.haomeiwen.com/subject/fyqbtrtx.html