筆者以往在使用一些插件的時候經(jīng)常會看上面這種多炫酷的插件操作界面,常常羨慕不已,如今有了Odin,自己也可以簡簡單單的實現(xiàn)這些炫酷的效果了跃捣,下面筆者就帶大家初識OdinWindow。
創(chuàng)建一個Window
不積跬步無以至千里夺蛇,我們先從最簡單的創(chuàng)建一個Window開始,只需要自定義一類并
繼承OdinEditorWindow
即可
public class MySimpleEditorWindow : OdinEditorWindow
{
[MenuItem("My Game/My Simple Editor")]
private static void OpenWindow()
{
GetWindow<MySimpleEditorWindow>().Show();
}
public string Hello = "菜鳥海瀾";
}
混合Window
完成第一步后疚漆,我們可以根據(jù)筆者Odin主頁中的特性來填充這個Window了
public class MyHybridEditorWindowOne : OdinEditorWindow
{
[MenuItem("My Game/My Hybrid Editor")]
private static void OpenWindow()
{
GetWindow<MyHybridEditorWindowOne>().Show();
}
[EnumToggleButtons, BoxGroup("Settings")]
public ScaleMode ScaleMode;
[FolderPath(RequireExistingPath = true), BoxGroup("Settings")]
public string OutputPath;
[HorizontalGroup(0.5f)]//占比0.5
public List<Texture> InputTextures;
[HorizontalGroup, InlineEditor(InlineEditorModes.LargePreview)]
public Texture Preview;
[Button(ButtonSizes.Gigantic), GUIColor(0, 1, 0)]
public void PerformSomeAction()
{
}
}
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
/*
* 通過從OdinEditorWindow而不是EditorWindow繼承,您可以使Unity編輯器窗口的方式與創(chuàng)建檢查器的方式完全相同:僅使用屬性刁赦。
提示:您可以使用 OnInspectorGUI 屬性娶聘,如果您希望將自定義編輯器IMGUI代碼與Odin繪制的編輯器混合使用
*/
public class SomeWindow : OdinEditorWindow
{
[MenuItem("My Game/My Window")]
private static void OpenWindow()
{
GetWindow<SomeWindow>().Show();
}
[PropertyOrder(-10)]
[HorizontalGroup]
[Button(ButtonSizes.Large)]
public void SomeButton1() { }
[HorizontalGroup]
[Button(ButtonSizes.Large)]
public void SomeButton2() { }
[HorizontalGroup]
[Button(ButtonSizes.Large)]
public void SomeButton3() { }
[HorizontalGroup]
[Button(ButtonSizes.Large), GUIColor(0, 1, 0)]
public void SomeButton4() { }
[HorizontalGroup]
[Button(ButtonSizes.Large), GUIColor(1, 0.5f, 0)]
public void SomeButton5() { }
[TableList]
public List<SomeType> SomeTableData;
}
public class SomeType
{
[TableColumnWidth(50)]
public bool Toggle;
[AssetsOnly]
public GameObject SomePrefab;
public string Message;
[TableColumnWidth(160)]
[HorizontalGroup("Actions")]
public void Test1() { }
[HorizontalGroup("Actions")]
public void Test2() { }
}
使用OdinMenuEditorWindow保持井井有條
這個就厲害了,一個Window集成數(shù)個功能甚脉,讓每個功能以菜單的形式進行選擇并顯示丸升。
這里需要注意的是,這次并不是繼承OdinEditorWindow牺氨,而是繼承OdinMenuEditorWindow
狡耻,并使用OdinMenuTree中的Add
和AddAllAssetsAtPath
函數(shù)添加菜單
- Add: 設(shè)置菜單名稱并傳入對應(yīng)需要渲染的類
-
AddAllAssetsAtPath:設(shè)置菜單名稱,傳入路徑猴凹,示例中的第一個bool是指是否包含子路徑酝豪,第二bool表示是否子路徑的所有可用類都在一個層級中渲染。選擇false就是下面這種形式
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class MyMenuEditorWindow : OdinMenuEditorWindow
{
[MenuItem("My Game/My Menu Editor")]
private static void OpenWindow()
{
GetWindow<MyMenuEditorWindow>().Show();
}
protected override OdinMenuTree BuildMenuTree()
{
var tree = new OdinMenuTree();
tree.Selection.SupportsMultiSelect = false;
tree.Add("Settings", GeneralDrawerConfig.Instance);
tree.Add("Utilities", new TextureUtilityEditor());
tree.AddAllAssetsAtPath("Odin Settings", "Assets/Plugins/Sirenix", typeof(ScriptableObject), true, true);
return tree;
}
}
public class TextureUtilityEditor
{
[BoxGroup("Tool"), HideLabel, EnumToggleButtons]
public Tool Tool;
public List<Texture> Textures;
[Button(ButtonSizes.Large), HideIf("Tool", Tool.Rotate)]
public void SomeAction() { }
[Button(ButtonSizes.Large), ShowIf("Tool", Tool.Rotate)]
public void SomeOtherAction() { }
}
功能強大且可自定義的OdinMenuTrees
public class MyTreeEditorWindow : OdinMenuEditorWindow
{
[MenuItem("My Game/My Tree Editor")]
private static void OpenWindow()
{
GetWindow<MyTreeEditorWindow>().Show();
}
protected override OdinMenuTree BuildMenuTree()
{
var tree = new OdinMenuTree();
tree.DefaultMenuStyle = OdinMenuStyle.TreeViewStyle;
tree.Add("Menu Style", tree.DefaultMenuStyle);
var allAssets = AssetDatabase.GetAllAssetPaths()
.Where(x => x.StartsWith("Assets/"))
.OrderBy(x => x);
foreach (var path in allAssets)
{
tree.AddAssetAtPath(path.Substring("Assets/".Length), path);
}
tree.EnumerateTree().AddThumbnailIcons();
return tree;
}
}
在編輯器窗口中渲染實例對象
您可以覆蓋GetTarget方法精堕,并給它任何類型的實例來呈現(xiàn)孵淘。
public class MyTargetEditorWindow : OdinEditorWindow
{
[MenuItem("My Game/My Target Editor")]
private static void OpenWindow()
{
GetWindow<MyTargetEditorWindow>().Show();
}
protected override void Initialize()
{
this.WindowPadding = Vector4.zero;
}
protected override object GetTarget()
{
return Selection.activeObject;
}
}