Unity Odin (实现如图所示效果)(二)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/494d9a6c6481eb136f7b782241a0dde8.png)
代码一
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
[CreateAssetMenu(fileName = "EnemyData",menuName = "My Game/Enemy Data")]
public class MyEnemyData : ScriptableObject
{
[BoxGroup("Basic Info")]
[LabelWidth(100)]
public string enemyName;
[BoxGroup("Basic Info")]
[LabelWidth(100)]
[TextArea]
public string enemyInfo;
[HideLabel]
[PreviewField(75)]
[HorizontalGroup("Game Data",75)]
public GameObject enemyModel;
[VerticalGroup("Game Data/Ver1")]
[LabelWidth(100)]
[Range(0,100)]
[GUIColor(1f,0,0)]
public int hp = 20;
[VerticalGroup("Game Data/Ver1")]
[LabelWidth(100)]
[Range(1f,10f)]
[GUIColor(0,1,0)]
public float speed = 2;
[VerticalGroup("Game Data/Ver1")]
[LabelWidth(100)]
[Range(1f,20f)]
[GUIColor(0,0,1)]
public float detectRange = 10f;
[VerticalGroup("Game Data/Ver1")]
[LabelWidth(100)]
[Range(1,10)]
[GUIColor(0.5f,0.5f,0.5f)]
public int damage = 1;
}
代码二
using System;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine;
public class MyEnemyDataWindow : OdinMenuEditorWindow
{
[MenuItem("MyTool/MyEnemyDataWindow")]
private static void OpenWindow()
{
GetWindow<MyEnemyDataWindow>().Show();
}
protected override void OnDestroy()
{
base.OnDestroy();
if(_createNewEnemyData!=null)
DestroyImmediate(_createNewEnemyData.myEnemyData);
}
private CreateNewEnemyData _createNewEnemyData;
protected override OdinMenuTree BuildMenuTree()
{
var tree = new OdinMenuTree();
_createNewEnemyData = new CreateNewEnemyData();
tree.Add("Enemy Data",_createNewEnemyData);
tree.AddAllAssetsAtPath("Enemy Data","Assets/Data",typeof(MyEnemyData));
return tree;
}
protected override void OnBeginDrawEditors()
{
var selection = this.MenuTree.Selection;
if (selection.SelectedValue is CreateNewEnemyData)
{
SirenixEditorGUI.BeginHorizontalToolbar();
{
GUILayout.FlexibleSpace();
if (SirenixEditorGUI.ToolbarButton("Create New Data"))
{
_createNewEnemyData.CreateNewData();
}
}
SirenixEditorGUI.EndHorizontalToolbar();
}
if (!(selection.SelectedValue is MyEnemyData asset)) return;
SirenixEditorGUI.BeginHorizontalToolbar();
{
GUILayout.FlexibleSpace();
if (SirenixEditorGUI.ToolbarButton("Delete CurrSelect"))
{
if (asset != null)
{
var path = AssetDatabase.GetAssetPath(asset);
AssetDatabase.DeleteAsset(path);
AssetDatabase.SaveAssets();
}
}
}
SirenixEditorGUI.EndHorizontalToolbar();
}
private class CreateNewEnemyData
{
public CreateNewEnemyData()
{
myEnemyData = ScriptableObject.CreateInstance<MyEnemyData>();
myEnemyData.enemyName = "New Enemy Data";
}
[BoxGroup("Create New Enemy Data",CenterLabel = true)]
[InlineEditor(InlineEditorObjectFieldModes.CompletelyHidden)]
public MyEnemyData myEnemyData;
[BoxGroup("Button")]
[PropertySpace(10)]
[Button(ButtonSizes.Medium)]
public void CreateNewData()
{
AssetDatabase.CreateAsset(myEnemyData,"Assets/Data/"+myEnemyData.enemyName+".asset");
AssetDatabase.SaveAssets();
myEnemyData = ScriptableObject.CreateInstance<MyEnemyData>();
myEnemyData.enemyName = "New Enemy Data";
}
}
}