Odin Attributes 大全之 Essentials 系列 | (37)Searchable 特性

📂 Unity常用插件 | 总目录

📂 Unity 3D资源 | 目录索引

📂Odin Attributes 大全 | 目录索引

Odin Attributes 为Unity开发者提供了更多的自定义选项,使得开发过程更加高效和愉悦。通过使用这些特性,开发者可以创建更加专业和用户友好的编辑器界面,从而提升整个开发团队的工作效率。

在这里插入图片描述

【Odin Inspector and Serializer 最新版 (0积分)免费下载


Searchable 特性:添加一个搜索框,用于搜索对应的类或其子类的成员,目前不可用于字典类型。

1. 修饰字段

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

public class SearchableExample_0 : MonoBehaviour
{
    [Searchable]
    public List Perks = new List()
	{
	    new Perk()
	    {
	        Name = "Old Sage",
	        Effects = new List()
	        {
	            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()
	        {
	            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()
	        {
	            new Effect() { Skill = Skill.Charisma, Value = 2, },
	            new Effect() { Skill = Skill.Intelligence, Value = -3 },
	        },
	    },
	    new Perk()
	    {
	        Name = "Village Idiot",
	        Effects = new List()
	        {
	            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 Effects;
    }

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

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

在这里插入图片描述

2. 修饰类,进行全局搜索

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 strings = new List(Enumerable.Range(1, 10).Select(i => "Str Element " + i));

    public List searchableList = new List(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
    }
}

在这里插入图片描述

3. Searchable 修饰对应的类型后,都会出现搜索栏,也可以通过 ISearchFilterable接口自定义搜索规则

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 searchableList = new List(Enumerable.Range(1, 10).Select(i => new ExampleStruct(i)));

    [Searchable(FilterOptions = SearchFilterOptions.ISearchFilterableInterface)]
    public List customFiltering = new List(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 Data = new List(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
    }
}

在这里插入图片描述

📂Odin Attributes 大全 | 目录索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity打怪升级

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值