ContextMenu/ContextMenuItem
using UnityEngine;
using UnityEditor;
public class ContextTest : MonoBehaviour
{
//在该附加脚本的 Inspector 中,当用户选择该上下文菜单时, 将执行此函数。
[ContextMenu("Do Something")]
void DoSomething()
{
Debug.Log("Perform operation");
}
//使用该属性可将上下文菜单添加到调用命名方法的字段。
[ContextMenuItem("Reset", "ResetBiography")]
public string playerBiography = "abcd";
void ResetBiography()
{
playerBiography = "";
}
#if UNITY_EDITOR
//附加到Camera的菜单
[MenuItem("CONTEXT/Camera/Log")]
static void Log(MenuCommand command)
{
Debug.Log(command.context);
}
#endif
}
Editor自定义Inspector面板
1.MyPlayer .cs
using UnityEngine;
using System.Collections;
public class MyPlayer : MonoBehaviour
{
public int armor = 75;
public int damage = 25;
public GameObject gun;
}
2.MyPlayerEditor
using UnityEditor;
using UnityEngine;
using System.Collections;
//编辑器可序列化
[CustomEditor(typeof(MyPlayer))]
[CanEditMultipleObjects]
public class MyPlayerEditor : Editor
{
SerializedProperty damageProp;
SerializedProperty armorProp;
SerializedProperty gunProp;
void OnEnable()
{
//赋值
damageProp = serializedObject.FindProperty("damage");
armorProp = serializedObject.FindProperty("armor");
gunProp = serializedObject.FindProperty("gun");
}
public override void OnInspectorGUI()
{
// 更新值
serializedObject.Update();
EditorGUILayout.IntSlider(damageProp, 0, 100, new GUIContent("Damage"));
//当同时编辑多个时,只有值一样才展示ProgressBar
if (!damageProp.hasMultipleDifferentValues)
ProgressBar(damageProp.intValue / 100.0f, "Damage");
EditorGUILayout.IntSlider(armorProp, 0, 100, new GUIContent("Armor"));
if (!armorProp.hasMultipleDifferentValues)
ProgressBar(armorProp.intValue / 100.0f, "Armor");
EditorGUILayout.PropertyField(gunProp, new GUIContent("Gun Object"));
// 保存值
serializedObject.ApplyModifiedProperties();
}
// 自定义ProgressBar
void ProgressBar(float value, string label)
{
Rect rect = GUILayoutUtility.GetRect(18, 18, "TextField");
EditorGUI.ProgressBar(rect, value, label);
EditorGUILayout.Space();
}
}
Unity常用内置特性
自定义特性
1.Attribute 继承 PropertyAttribute
2.渲染器继承 PropertyDrawer
3.使用 CustomPropertyDrawerAttribute 标记
4.在 OnGUI 渲染
5.使用 GetPropertyHeight 重新计算高度
MyDrawer.cs
using UnityEditor;
using UnityEngine;
using System.Collections;
using System;
[CustomPropertyDrawer(typeof(MyAttribute))]
public class MyDrawer : PropertyDrawer
{
//绘制
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
MyAttribute range = attribute as MyAttribute;
EditorGUI.LabelField(new Rect(position.x, position.y, position.width, 20), "自定义特性");
if (property.propertyType == SerializedPropertyType.Float)
EditorGUI.Slider(new Rect(position.x, position.y + 20, position.width, 20), property, range.min, range.max,"");
else if (property.propertyType == SerializedPropertyType.Integer)
EditorGUI.IntSlider(new Rect(position.x, position.y + 20, position.width, 20), property, Convert.ToInt32(range.min), Convert.ToInt32(range.max), "");
else
EditorGUI.LabelField(new Rect(position.x, position.y + 20, position.width, 20),"","Use Range with float or int.");
}
//重写属性高度
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return base.GetPropertyHeight(property, label) + 25;
}
}
MyAttr.cs
using UnityEngine;
public class MyAttribute : PropertyAttribute
{
public float min;
public float max;
public MyAttribute(float min, float max)
{
this.min = min;
this.max = max;
}
}
ObjectPreview
Object 的预览编辑器
1.继承 ObjectPreview
2.HasPreviewGUI 返回 true
3.OnPreviewGUI 渲染
4.CustomPreviewAttribute 标记
using UnityEngine;
using UnityEditor;
//所有的GameObject预览
[CustomPreview(typeof(GameObject))]
public class MyPreview : ObjectPreview
{
public override bool HasPreviewGUI()
{
return true;
}
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
GUI.Label(r, target.name + " is being previewed");
}
}
免费课程链接:
独立游戏《Unity打造关卡编辑器》Unity 独立游戏 关卡编辑器https://bycwedu.vipwan.cn/promotion_channels/72762192