https://odininspector.com/tutorials
https://blog.csdn.net/su9257/article/details/103159984
https://odininspector.com/attributes
總結:
1.標簽化管理
2.自定義繪制。比如 OnInspectorGUI和CustomDraw等 可以自定義變量的繪制。
3.Group可以進行區域屬性的划分,子屬性不需要寫N多標簽。
具體想要什么 用的時候再查閱吧。
1.Assets Only Attribute
https://www.jianshu.com/p/194081e24e65
- AssetsOnly: 點擊需要序列化的資源字段時,在出現的彈窗中只有Project中的資源文件,不會出現Hierachy(場景)的資源
- SceneObjectsOnly: 點擊需要序列化的資源字段時,在出現的彈窗中只有Hierachy中的資源文件,不會出現Project中的資源
注意:例如:預制體等資源在Scene或者Project中都含有,出現的彈窗中也都會含有對應的資源
2.Custom Value Drawer Attribute
https://www.jianshu.com/p/1c4e85a83e3c
Custom Value Drawer Attribute 特性,允許用戶自定義一個繪制方法 (這個好,可以散列的對變量進行自定義顯示操作。和數據邏輯和EditorGUILayout關聯)
public float Max = 100, Min = 0; [CustomValueDrawer("MyStaticCustomDrawerStatic")] public float CustomDrawerStatic; private static float MyStaticCustomDrawerStatic(float value, GUIContent label) { return EditorGUILayout.Slider(label, value, 0f, 10f); } [CustomValueDrawer("MyStaticCustomDrawerInstance")] public float CustomDrawerInstance; private float MyStaticCustomDrawerInstance(float value, GUIContent label) { return EditorGUILayout.Slider(label, value, this.Min, this.Max); } [CustomValueDrawer("MyStaticCustomDrawerArray")] public float[] CustomDrawerArray = new float[] { 3f, 5f, 6f }; private float MyStaticCustomDrawerArray(float value, GUIContent label) { return EditorGUILayout.Slider(value, this.Min, this.Max); }
3.Delayed Property Attribute
1.變量更改的的回調 OnValueChanged 2.Delay方式讓回調在鼠標離開選項框在callback 3.Delayed DelayedProperty 后者可用於屬性值
// Delayed and DelayedProperty attributes are virtually identical... [Delayed] [OnValueChanged("OnValueChanged")] public int DelayedField; // ... but the DelayedProperty can, as the name suggests, also be applied to properties. [ShowInInspector, DelayedProperty] [OnValueChanged("OnValueChanged")] public string DelayedProperty { get; set; } private void OnValueChanged() { Debug.Log("Value changed!"); }
4.Detailed Info Box Attribute
在Inspector面板中繪制一個信息面板,添加對應標題和詳細信息描述,點擊時可以顯示出對應填寫的詳細信息 https://www.jianshu.com/p/ee83f0c151e9
// // 摘要: // Displays a message box with hideable details. // // 參數: // message: // The message for the message box. // // details: // The hideable details of the message box. // // infoMessageType: // Type of the message box. 哪種類型的圖標提示 // // visibleIf: // Optional name of a member to hide or show the message box. 自定義是否顯示這個Box信息 public DetailedInfoBoxAttribute(string message, string details, InfoMessageType infoMessageType = InfoMessageType.Info, string visibleIf = null);
5.Enable GUIAttribute
把顯示灰色的不置灰,感覺用不到啊
6.GUIColor Attribute
1.RGB 2.自定義方式 3.@字符串方式實現2的方式
7.Hide Label Attribute
用於任何屬性。使用此選項可隱藏Inspector中的屬性標簽。
8.Property Order Attribute
PropertyOrder特性:用於任何屬性,並允許對屬性進行排序。使用此選項可以定義屬性的顯示順序。
9.Property Space Attribute
PropertySpace特性:與Unity的現有Space屬性具有相同的功能,但可以應用於任何屬性而不僅僅是字段;而且還可以控制與前后字段的間距
10.Read Only Attribute
就是以灰態的形式展示
11.Required Attribute
用於任何對象屬性,如果對應屬性沒有賦值,則在檢查器中出現對應的提示消息。
12.Show In Inspector Attribute
如果需要序列化,需要配合SerializeField特性使用
[ShowInInspector]
private int myPrivateInt; [ShowInInspector] //沒與序列化的屬性也可以顯示 public int MyPropertyInt { get; set; } [ShowInInspector] public int ReadOnlyProperty { get { return this.myPrivateInt; } } [ShowInInspector] //static成員的顯示 public static bool StaticProperty { get; set; }
[SerializeField, HideInInspector]
private int evenNumber; [ShowInInspector] public int EvenNumber { get { return this.evenNumber; } set { this.evenNumber = value - (value % 2); } }
13.Searchable Attribute
搜索功能, 作用域比較靈活, 可以在獨立的屬性或者整體類上。
14.Title Attribute
用於在屬性上方生成粗體標題。 主標題、副標題、是否有分割線、是否粗體
[Title("Non bold title", "With no line seperator", horizontalLine: false, bold: false)]
15.Type Filter Attribute
https://www.jianshu.com/p/6b829180daef
對輸入的value 進行自定義過濾,只顯示需要的類型
16.Type Info Box Attribute
17.Validate Input Attribute
無效的輸入,進行提示。(可自定義什么是有效。比如10.Required Attribute 要求有輸入才有效)
[ValidateInput("HasMeshRendererDynamicMessageAndType", "對應的函數中已經有消息和類型,所以這個默認消息和類型已經沒用")] public GameObject DynamicMessageAndType; [InfoBox("Change GameObject value to update message type", InfoMessageType.Info)] public InfoMessageType MessageType; private bool HasMeshRendererDynamicMessageAndType(GameObject gameObject, ref string errorMessage, ref InfoMessageType? messageType) { if (gameObject == null) return true; if (gameObject.GetComponentInChildren<MeshRenderer>() == null) { errorMessage = "\"" + gameObject.name + "\" 要有一個 MeshRenderer 組件";//如果設置消息,則默認消息會被覆蓋 messageType = this.MessageType;//如果設置消息類型,則默認消息類型會被覆蓋 return false; } return true; }
18.Value Dropdown Attribute
https://www.jianshu.com/p/72bbece66f5d
1.MemberName,也是唯一一個有參構造函數需要的屬性,有兩種形式的Drop下拉條,一種是直接數值的,另一種是Key-Value形式的
2.【SortDropdownItems】默認為false 開啟后為下拉列表為根據Key升序排序
3.【DropdownTitle】給下來條提供一個標題
4.【DropdownHeight】下拉條高度 【DropdownWidth】下拉條的寬度
5.【FlattenTreeView】是否使用平鋪的樹形視圖
6.【DoubleClickToConfirm】需要雙擊才能確地選中的內容
7.【HideChildProperties】是否隱藏此類型所含有的屬性信息
8.【AppendNextDrawer】下拉條變成一個小的選擇器,代替原有的寬型下拉條
9.【DisableGUIInAppendedDrawer】配合AppendNextDrawer使用,顯示的數值為灰度狀態,達到不可更改數值的目的
10.【ExpandAllMenuItems】下拉條里面的條目是否全部展開
11.【IsUniqueList】在添加的列表Item前面添加勾選框,可以一次性勾選多個Item並添加
12.【ExcludeExistingValuesInList】添加列中不會顯示已經選中的Item
13.【DisableListAddButtonBehaviour】禁用下拉列表,以彈窗的形式彈出
14.【DrawDropdownForListElements】已經添加的Item不會再出現Item下拉表(和12有什么區別?)
15.【NumberOfItemsBeforeEnablingSearch】查過指定數量的Item則出現搜索框。默認是10。
19.Asset List Attribute
針對一次性多個List和Array選擇
AssetList:創建一個指定類型的列表
【Path】根據指定篩選資源的路徑
【AutoPopulate】符合規則的自動添加到List中 ,設置為true則自動填充符合規則的資源,false為只顯示不填充
【LayerNames】指定layer層過濾
【AssetNamePrefix】以資源名稱的前綴作為篩選條件
【Tags】以資源的tag作為篩選條件,可用,符號分隔多個Tag
【CustomFilterMethod】自定義過濾函數
20.Asset Selector Attribute
在對象字段旁邊添加一個小按鈕,該按鈕將向用戶顯示資產下拉列表,以便從屬性中進行選擇。(IsUniqueList可進行多選)
【AssetSelector】添加到對應的字段上即可
【FlattenTreeView】是否開啟樹狀圖
【Paths】單路徑或多路徑查找
【Filter】自定義過濾條件
【DisableListAddButtonBehaviour】開啟后加號不會出現樹形下拉條以彈窗形式出現
【DrawDropdownForListElements】控制已經添加的Item是否含有下拉列表
【ExcludeExistingValuesInList】去除已經含有的Item
【IsUniqueList】開啟列表勾選模式(這個關鍵字在Odin的很多特性中都有)
【ExpandAllMenuItems】下拉列表是否強制展開(這個關鍵字在Odin的很多特性中都有)
【DropdownTitle】下拉列表標題
21.Child Game Objects Only Attribute
用於Components和GameObject字段,並將在對象字段旁邊添加一個小按鈕,該按鈕將在所有子游戲對象中搜索可分配對象,並將其顯示在下拉列表中供用戶選擇。
【ChildGameObjectsOnly】獲取包括自己在內以的可用節點
【IncludeSelf】是否包含自己的節點
22.Color Palette Attribute
調色板相關
23.Display As String Attribute
用於任何屬性,對應的值在檢查器中以文本形式顯示字符串。如果屬性的值要在檢查器中顯示字符串,但不允許進行任何編輯,請使用此選項。
24.File Path Attribute
字符串屬性
[FilePath(ParentFolder = "Assets/Resources")] public string ResourcePath; //ParentFolder默認打開到Assets/Resources相對路徑下
[FilePath(Extensions = "cs")] [BoxGroup("Conditions")] public string ScriptFiles; //Extensions默認選擇的是cs文件
// By setting AbsolutePath to true, the FilePath will provide an absolute path instead. [FilePath(AbsolutePath = true)]//是否絕對路徑 [BoxGroup("Conditions")] public string AbsolutePath; // FilePath can also be configured to show an error, if the provided path is invalid. [FilePath(RequireExistingPath = true)]//沒值就顯示大大的提醒 [BoxGroup("Conditions")] public string ExistingPath; // By default, FilePath will enforce the use of forward slashes. It can also be configured to use backslashes instead. [FilePath(UseBackslashes = true)]//反斜杠 [BoxGroup("Conditions")] public string Backslashes; // FilePath also supports member references with the $ symbol. [FilePath(ParentFolder = "$DynamicParent", Extensions = "$DynamicExtensions")]//動態定義父節點和擴展文件類型 [BoxGroup("Member referencing")] public string DynamicFilePath; [BoxGroup("Member referencing")] public string DynamicParent = "Assets/Plugins/Sirenix"; [BoxGroup("Member referencing")] public string DynamicExtensions = "cs, unity, jpg"; // FilePath also supports lists and arrays. [FilePath(ParentFolder = "Assets/Plugins/Sirenix/Demos/Odin Inspector")] [BoxGroup("Lists")] public string[] ListOfFiles;
25.Folder Path Attribute
用於字符串字段,並為目錄路徑提供接口。
26.Inline Editor Attribute
InlineAttribute用於任何屬性或字段,其類型繼承自UnityEngine.Object。這包括組件和資產等。
unity自帶的詳細編輯
27.Hide In Inline Editors Attribute
在屬性對象的Inline編輯模式中隱藏不需要繪制的屬性
27.Enum Paging Attribute
Draws an enum selector in the inspector with next and previous buttons to let you cycle through the available values for the enum property.
[EnumPaging, OnValueChanged("SetCurrentTool")] [InfoBox("Changing this property will change the current selected tool in the Unity editor.")] public UnityEditor.Tool sceneTool; private void SetCurrentTool() { UnityEditor.Tools.current = this.sceneTool; }
28.Table List Attribute
https://odininspector.com/attributes/table-list-attribute
Renders lists and arrays in the inspector as tables.
[TableList(ShowIndexLabels = true)]
[TableList(DrawScrollView = true, MaxScrollViewHeight = 200, MinScrollViewHeight = 100)]
[TableList(AlwaysExpanded = true, DrawScrollView = false)]
[TableList(ShowPaging = true)]
29.Table Matrix Attribute
繪制二維數組
30.Hide In Tables Attribute
TableList特性中隱藏對應的屬性繪制的Inline
31.Hide Mono Script Attribute
隱藏系統Script屬性的顯示
32.Multi Line Property Attribute
允許用戶在多行文本框中編輯字符串。
[Multiline(10)]
[MultiLineProperty(10)]
33.Preview Field Attribute
繪制一個方形ObjectField,它呈現UnityEngine.Object類型的預覽。此對象字段還添加了對拖放的支持
[PreviewField(150, ObjectFieldAlignment.Right)]
34.Toggle Attribute
控制屬性是否可編輯
Toggle is used on any field or property, and allows to enable or disable the property in the inspector. Use this to create a property that can be turned off or on.
35.Toggle Left Attribute
Draws the checkbox before the label instead of after.
[InfoBox("Draws the toggle button before the label for a bool property.")] [ToggleLeft] public bool LeftToggled; [EnableIf("LeftToggled")] public int A; [EnableIf("LeftToggled")] public bool B; [EnableIf("LeftToggled")] public bool C;
36.Button Attribute
用於為一個方法在檢查器中繪制一個觸發該方法的功能按鈕
37.Enum Paging Attribute
使用下一個和上一個按鈕繪制枚舉選擇器,以便循環訪問枚舉屬性的可用值
38.Enum Toggle Buttons Attribute
在水平按鈕組中繪制枚舉而不是下拉列表。
39.Inline Button Attribute
用於將一個按鈕(函數)添加到屬性的末尾。
40.Box Group Attribute
可用於任何屬性,並將該屬性組織在一個Box中。使用它可以在檢查器中清晰地組織相關值。
41.Title Group Attribute
標題Group,將對應的成員划分
可設置 副標題、標題對齊方式、子節點是否相對縮進、是否隱藏橫線
42.Foldout Group Attribute
可用於任何屬性,並將屬性組織為折疊。使用它來組織屬性,並允許用戶隱藏當前與他們不相關的屬性。
expanded 標記為初始是否展開的。
43.Horizontal Group Attribute
1.層級關系是耦合的,可以避免在每個屬性上都要重新寫一遍。 2.MarginLeft、MarginRight(左右邊距)、PaddingLeft(內邊距)、PaddingRight(組與組的邊距)、MinWidth與MaxWidth間距,設置Title標題
[Button(ButtonSizes.Large)] [FoldoutGroup("Buttons in Boxes")] [HorizontalGroup("Buttons in Boxes/Horizontal")] [BoxGroup("Buttons in Boxes/Horizontal/One")] public void Button1() { } [Button(ButtonSizes.Large)] [BoxGroup("Buttons in Boxes/Horizontal/Two")] public void Button2() { } [Button] [HorizontalGroup("Buttons in Boxes/Horizontal", Width = 60)] [BoxGroup("Buttons in Boxes/Horizontal/Double")] public void Accept() { } [Button] [BoxGroup("Buttons in Boxes/Horizontal/Double")] public void Cancel() { }
44.Tab Group Attribute
可用於任何屬性,並將屬性組織到不同的選項卡中。使用它來組織不同的值,以使清潔檢查器變得易於使用。
也可以為選項卡指定對應的組
https://www.jianshu.com/p/997e58be508a
45.Vertical Group Attribute
結合
Horizontal 可以達到二維的效果。
46.Toggle Group Attribute
ToggleGroup用於任何字段,並創建一組可切換的選項。使用此選項可以創建可以啟用或禁用的選項。
也可以以制定toggle group的標題,或者通過$特殊標識符引用一個成員的值作為標題
如果制定的toggle為class結構,需要添加Serializable特性,toggle標題默認為此類的名稱,且繼承關系的父類結構同樣可以繪制在檢查器面板上
47.Button Group Attribute
用於可用於任何實例函數,並將按鈕添加到組織為水平組的檢查器中。使用此按鈕可以將多個按鈕組織在一個整齊的水平組中。
48.Responsive Button Group Attribute
將按鈕分組到一個組中,該組將根據可用布局空間的大小來定位和調整按鈕的大小。
以上,通過Group的概念,歸類、通過名稱划分父子層級
49.Dictionary Drawer Settings Attribute
【KeyLabel和ValueLabel】自定義標簽
【DictionaryDisplayOptions】控制value默認以折疊還是展開形式顯示
50.List Drawer Settings Attribute
將[Range]屬性應用於此列表,代替傳統的float形式
不同方式的只讀方式
數據結構的數組或列表
自定義page每頁的個數
[ListDrawerSettings(NumberOfItemsPerPage = 5)]
顯示對應元素的索引和指定其元素的標簽
[ListDrawerSettings(ShowIndexLabels = true, ListElementLabelName = "SomeString")]
禁止拖拽item,禁止翻頁,禁止顯示item個數
[ListDrawerSettings(DraggableItems = false, Expanded = false, ShowIndexLabels = true, ShowPaging = false, ShowItemCount = false, HideRemoveButton = true)]
【OnBeginListElementGUI】【OnEndListElementGUI】在每個列表元素的前后調用一個函數,並傳入對應元素的索引
使用它將自定義GUI行為注入到列表的標題欄中。
[ListDrawerSettings(OnTitleBarGUI = "DrawRefreshButton")] public List<int> CustomButtons; private void DrawRefreshButton() { if (SirenixEditorGUI.ToolbarButton(EditorIcons.Refresh)) { Debug.Log(this.CustomButtons.Count.ToString()); } }
[ListDrawerSettings(CustomAddFunction = "CustomAddFunction")] public List<int> CustomAddBehaviour; private int CustomAddFunction() { return this.CustomAddBehaviour.Count+100; }
51.Table List Attribute
將列表和數組呈現為表。
【ShowIndexLabels】設置為True,則為每個元素繪制一個標簽,其中顯示元素的索引。
【DrawScrollView 】為True,為table添加一個滾動條,並設置滾動條最大高度(MaxScrollViewHeight )和最小高度(MinScrollViewHeight )
【ShowPaging】設置為True,則繪制一個翻頁的選項 【NumberOfItemsPerPage】則設置每個分頁含有的Item數量,默認15個
- 【IsReadOnly】在檢查器中不可修改
- 【HideToolbar】隱藏翻頁等工具
- 【CellPadding】每個Item及屬性的間隔
- 【ScrollViewHeight】固定滾動條高度
- 【MinScrollViewHeight】最小滾動條高度
- 【MaxScrollViewHeight】最大滾動條高度
52.Table Matrix Attribute
https://www.jianshu.com/p/9ef30570309e
用於進一步指定Odin應如何繪制二維數組。
【IsReadOnly 】為true,則不能調整矩陣的順序
【Transpose】作用起到一個順序調到的效果
其他輔助效果 ResizableColumns:是否可以通過鼠標調整列的寬度 RowHeight:固定行高度
[PropertySpace(40)] [ShowInInspector, DoNotDrawAsReference] [TableMatrix(HorizontalTitle = "Transposed Custom Cell Drawing", DrawElementMethod = "DrawColoredEnumElement", ResizableColumns = true, RowHeight = 16, Transpose = true)]//Transpose順序顛倒 public bool[,] Transposed { get { return CustomCellDrawing; } set { CustomCellDrawing = value; } } private static bool DrawColoredEnumElement(Rect rect, bool value) { if (Event.current.type == EventType.MouseDown && rect.Contains(Event.current.mousePosition)) { value = !value; GUI.changed = true; Event.current.Use(); } UnityEditor.EditorGUI.DrawRect(rect.Padding(1), value ? new Color(0.1f, 0.8f, 0.2f) : new Color(0, 0, 0, 0.5f)); return value; }
53.Table Column Width Attribute
TableColumnWidth屬性用於進一步自定義使用“ TableListAttribute” 繪制的表中的列的寬度。
55.Value Dropdown Attribute
自定義Dropdown的屬性 , 還可以自定義下拉list的數據來源
https://odininspector.com/attributes/value-dropdown-attribute
[ValueDropdown("GetAllSceneObjects", IsUniqueList = true, DropdownTitle = "Select Scene Object", DrawDropdownForListElements = false, ExcludeExistingValuesInList = true)] public List<GameObject> UniqueGameobjectListMode2; private static IEnumerable GetAllSceneObjects() { Func<Transform, string> getPath = null; getPath = x => (x ? getPath(x.parent) + "/" + x.gameObject.name : ""); return GameObject.FindObjectsOfType<GameObject>().Select(x => new ValueDropdownItem(getPath(x.transform), x)); } private static IEnumerable GetAllScriptableObjects() { return UnityEditor.AssetDatabase.FindAssets("t:ScriptableObject") .Select(x => UnityEditor.AssetDatabase.GUIDToAssetPath(x)) .Select(x => new ValueDropdownItem(x, UnityEditor.AssetDatabase.LoadAssetAtPath<ScriptableObject>(x))); }
Custom Context Menu Attribute
Custom Context Menu Attribute:可用於任何屬性,並將自定義選項添加到屬性的上下文菜單。當您要將自定義操作添加到屬性的上下文菜單時,請使用此選項。
[CustomContextMenu("Say Hello/菜鳥海瀾", "SayHelloFunction")]
Disable Context Menu Attribute
用於任何屬性,並禁用該屬性的上下文菜單。如果您不希望屬性使用上下文菜單,請使用此選項。
Draw With Unity Attribute
[InfoBox("如果你曾經遇到過Odin屬性的問題,那么使用DrawWithUnity")]
Indent Attribute
用於縮進可用於任何屬性,並將屬性的標簽向右移動。使用它可以清楚地組織檢查器中的屬性。
[Indent(3)]
Info Box Attribute
【InfoBox】添加不同提示類型的文本框
[InfoBox("Warning info box.", InfoMessageType.Warning)]
Inline Property Attribute
用於將類型的內容放置在標簽旁邊,而不是呈現在折疊中。
https://www.jianshu.com/p/17e19ba03065
On Inspector GUIAttribute
[OnInspectorGUI("DrawPreview", append: true)]
只要檢查器代碼正在運行,它將調用指定的函數。使用它為對象創建自定義InspectorGUI。(這個常用)
On Value Changed Attribute
[OnValueChanged("CreateMaterial")] public Shader Shader;
Property Tooltip Attribute
將屬性懸停在檢查器中時創建工具提示。用它來解釋目的或如何使用屬性。
[PropertyTooltip("放在屬性上顯示對應的懸停提示.")]
Suffix Label Attribute
屬性在屬性的末尾繪制一個標簽。用它來傳達有關屬性的意圖
[SuffixLabel("Prefab")]
Enable If Attribute
這個特性的效果主要是當指定條件滿足時,啟用對應的屬性,默認傳入的參數為對應屬性的名稱,如果為True或者不為null時,啟用對應屬性
[EnableIf("SomeObject")]
EnumToggleButtons 可以用來制作選擇一個的button
Hide If Attribute
Show If Group Attribute
EnableIf的集合模式。
Show In Inline Editors Attribute
Show In Inline Editors Attribute:用於在Inline中顯示對應的屬性
[ShowInInlineEditors] public string ShownInInlineEditor; [HideInInlineEditors]//在繪制的里面不顯示 public string HiddenInInlineEditor; [DisableInInlineEditors]//顯示但是是灰態 public string DisabledInInlineEditor;
Hide In Play Mode Attribute
在Play模式下隱藏對應屬性
[HideInPlayMode]
Disable In Non Prefabs Attribute
用於當屬性所在的組件在非預制體上面時,禁用對應的屬性
Max Value Attribute
它將字段的值限制為最大值。使用此定義字段的最大值。
[MaxValue(0)]
Min Max Slider Attribute
用於繪制一個特殊的滑塊,用戶可以用來指定最小值和最大值之間的范圍。使用Vector2,其中x為最小值,y為最大值。
[MinMaxSlider(-10, 10)]
Progress Bar Attribute
https://www.jianshu.com/p/4c1a57b13479
// 最小和最大屬性也支持帶有$符號的屬性表達式. [BoxGroup("Dynamic Range")] [ProgressBar("Min", "Max")] public float DynamicProgressBar = 50; [BoxGroup("Dynamic Range")] public float Min; [BoxGroup("Dynamic Range")] public float Max = 100;
Property Range Attribute
屬性創建一個滑塊控件,以將屬性的值設置在指定范圍之間。這等效於Unity的Range屬性,但是此屬性可以同時應用於字段和屬性。
[InfoBox("Odin的PropertyRange屬性類似於Unity的Range屬性,但也適用於屬性.")] [ShowInInspector, PropertyRange(0, 10)] public int Property { get; set; }
Wrap Attribute
類似: Mathf
.PingPong
[Wrap(0f, 100f)] public int IntWrapFrom0To100;