Odin Inspector 系列教程 — SearchableAttribute「建议收藏」

Odin Inspector 系列教程 — SearchableAttribute「建议收藏」通过添加SearchableAttribute特性为其添加一个搜索框,可用于搜索对应的类或其子类的成员,但目前不可用于字典类型。imageusingSirenix.OdinInspector;usingSystem;usingSystem.Collections.Generic;usingUnityEngine;publicclassSearchableExam…

大家好,又见面了,我是你们的朋友全栈君。

通过添加SearchableAttribute特性为其添加一个搜索框,可用于搜索对应的类或其子类的成员,但目前不可用于字典类型。
Odin Inspector 系列教程 --- SearchableAttribute「建议收藏」

image

using Sirenix.OdinInspector;
using System;
using System.Collections.Generic;
using UnityEngine;

public class SearchableExample_0 : MonoBehaviour
{
    [Searchable]
    public List<Perk> Perks = new List<Perk>()
{
    new Perk()
    {
        Name = "Old Sage",
        Effects = new List<Effect>()
        {
            new Effect() { Skill = Skill.Wisdom, Value = 2, },
            new Effect() { Skill = Skill.Intelligence, Value = 1, },
            new Effect() { Skill = Skill.Strength, Value = -2 },
        },
    },
    new Perk()
    {
        Name = "Hardened Criminal",
        Effects = new List<Effect>()
        {
            new Effect() { Skill = Skill.Dexterity, Value = 2, },
            new Effect() { Skill = Skill.Strength, Value = 1, },
            new Effect() { Skill = Skill.Charisma, Value = -2 },
        },
    },
    new Perk()
    {
        Name = "Born Leader",
        Effects = new List<Effect>()
        {
            new Effect() { Skill = Skill.Charisma, Value = 2, },
            new Effect() { Skill = Skill.Intelligence, Value = -3 },
        },
    },
    new Perk()
    {
        Name = "Village Idiot",
        Effects = new List<Effect>()
        {
            new Effect() { Skill = Skill.Charisma, Value = 4, },
            new Effect() { Skill = Skill.Constitution, Value = 2, },
            new Effect() { Skill = Skill.Intelligence, Value = -3 },
            new Effect() { Skill = Skill.Wisdom, Value = -3 },
        },
    },
};

    [Serializable]
    public class Perk
    {
        public string Name;

        [TableList]
        public List<Effect> Effects;
    }

    [Serializable]
    public class Effect
    {
        public Skill Skill;
        public float Value;
    }

    public enum Skill
    {
        Strength,
        Dexterity,
        Constitution,
        Intelligence,
        Wisdom,
        Charisma,
    }
}

放置在类顶进行全局搜索

Odin Inspector 系列教程 --- SearchableAttribute「建议收藏」

image

using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor.Examples;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

[Searchable]
public class SearchableExample_1 : MonoBehaviour
{
    public List<string> strings = new List<string>(Enumerable.Range(1, 10).Select(i => "Str Element " + i));

    public List<ExampleStruct> searchableList = new List<ExampleStruct>(Enumerable.Range(1, 10).Select(i => new ExampleStruct(i)));

    [Serializable]
    public struct ExampleStruct
    {
        public string Name;
        public int Number;
        public ExampleEnum Enum;

        public ExampleStruct(int nr) : this()
        {
            this.Name = "Element " + nr;
            this.Number = nr;
            this.Enum = (ExampleEnum)ExampleHelper.RandomInt(0, 5);
        }
    }

    public enum ExampleEnum
    {
        One, Two, Three, Four, Five
    }
}

Searchable应用到对应的类型上后,使用对应的类型都会出现搜索栏,也可以通过 ISearchFilterable接口自定义搜索规则

Odin Inspector 系列教程 --- SearchableAttribute「建议收藏」

image

using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor.Examples;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class SearchableExample_2 : MonoBehaviour
{
    [Searchable]
    public ExampleClass searchableClass = new ExampleClass();

    [Searchable]
    public List<ExampleStruct> searchableList = new List<ExampleStruct>(Enumerable.Range(1, 10).Select(i => new ExampleStruct(i)));

    [Searchable(FilterOptions = SearchFilterOptions.ISearchFilterableInterface)]
    public List<FilterableBySquareStruct> customFiltering = new List<FilterableBySquareStruct>(Enumerable.Range(1, 10).Select(i => new FilterableBySquareStruct(i)));

    [Serializable]
    public class ExampleClass
    {
        public string SomeString = "Saehrimnir is a tasty delicacy";
        public int SomeInt = 13579;

        public DataContainer DataContainerOne = new DataContainer() { Name = "Example Data Set One" };
        public DataContainer DataContainerTwo = new DataContainer() { Name = "Example Data Set Two" };
    }

    [Serializable, Searchable] // You can also apply it on a type like this, and it will become searchable wherever it appears
    public class DataContainer
    {
        public string Name;
        public List<ExampleStruct> Data = new List<ExampleStruct>(Enumerable.Range(1, 10).Select(i => new ExampleStruct(i)));
    }

    [Serializable]
    public struct FilterableBySquareStruct : ISearchFilterable
    {
        public int Number;

        [ShowInInspector, DisplayAsString, EnableGUI]
        public int Square { get { return this.Number * this.Number; } }

        public FilterableBySquareStruct(int nr)
        {
            this.Number = nr;
        }

        public bool IsMatch(string searchString)
        {
            return searchString.Contains(Square.ToString());
        }
    }

    [Serializable]
    public struct ExampleStruct
    {
        public string Name;
        public int Number;
        public ExampleEnum Enum;

        public ExampleStruct(int nr) : this()
        {
            this.Name = "Element " + nr;
            this.Number = nr;

            this.Enum = (ExampleEnum)ExampleHelper.RandomInt(0, 5);
        }
    }

    public enum ExampleEnum
    {
        One, Two, Three, Four, Five
    }
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/162779.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • handlerInvocation的invoke方法

    handlerInvocation的invoke方法java.lang.reflect.Proxy类的newProxyInstance(),是用于创建动态代理类和实例的静态方法.返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序。java.lang.reflect.InvocationHandler接口中的invoke(),在代理实例上处理方法调用并返回结果。当与方法关联的代理实例上调用方法时,将在调用处理程序上调用此方法…

    2022年5月12日
    45
  • 快捷方式图标变白完美解决方法_应用程序图标显示空白

    快捷方式图标变白完美解决方法_应用程序图标显示空白annotation:explorer.exe是Windows程序管理器或者文件资源管理器,它用于管理Windows图形壳,包括桌面和文件管理,删除该程序会导致Windows图形界面无法使用解决方式:把图标缓存的数据库删除第一步:win+rcmd进入命令行第二步:一行一行键入如下命令taskkill/imexplorer.exe/fcd/d%u…

    2022年10月18日
    0
  • JAVA reference_Java instanceof

    JAVA reference_Java instanceof本文介绍对象的强、软、弱和虚引用的概念、应用。1.对象的强、软、弱和虚引用  在JDK1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象。也就是说,只有对象处于可触及(reachable)状态,程序才能使用它。从JDK1.2版本开始,把对象的引用分为4种级别,从而使程序能更加灵活地控制对象的生命周期。这4种级别由高到低依次为:强引用、软引用、弱引用和虚引用。

    2022年9月8日
    0
  • Android控件自定义属性(declare-styleable属性详解)

    Android控件自定义属性(declare-styleable属性详解)我们在做项目的时候,由于android自带的属性不能满足需求,android提供了自定义属性的方法,其中的format是做什么用的?以及如何使用它?下面列出一些常用的。1.reference:参考某一资源ID。   (1)属性定义:                                           (2)属性使用:

    2022年7月13日
    13
  • noip宝藏_24日拼图寻宝

    noip宝藏_24日拼图寻宝题目描述:点击打开链接/*思路:可以看成是一个多层的约瑟夫环问题*/#include#defineN10005#defineM105intstairs[N][M];//是否有楼梯口(0/1)intsign[N][M];//房间指示牌(走的步数)intf[N];//每层楼梯口总数(记录每层楼梯口数

    2022年8月22日
    3
  • 网页制作105个问答

    网页制作105个问答

    2021年8月21日
    58

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号