1.CanEditMultipleObjects属性
添加到类上面, 在自定义编辑器时,允许多对象编辑,就是同时选中多个物体,统一修改共同的值
这是没有加该属性
2.CustomEditor属性
添加到类上面,为一个组件或者脚本自定义属性面板,定义类要继承editor类,CustomEditor(typeof(你自己的类名))
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EditorAttribute : MonoBehaviour
{
//属性必须是public的,并且和你自定义编辑器中的类型是一致的,比如这里是int
类型,你在编辑器中就用intslider
public int damage;
public int armor;
public GameObject gun;
}
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(EditorAttribute))]
public class EditorAttributes : Editor
{
SerializedProperty damageProp;
SerializedProperty armorProp;
SerializedProperty gunProp;
void OnEnable()
{
// Setup the SerializedProperties.
damageProp = serializedObject.FindProperty("damage");
armorProp = serializedObject.FindProperty("armor");
gunProp = serializedObject.FindProperty("gun");
}
public override void OnInspectorGUI()
{
// Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
serializedObject.Update();
// Show the custom GUI controls.
EditorGUILayout.IntSlider(damageProp, 0, 100, new GUIContent("Damage"));
// Only show the damage progress bar if all the objects have the same damage value:
if (!damageProp.hasMultipleDifferentValues)
ProgressBar(damageProp.intValue / 100.0f, "Damage");
EditorGUILayout.IntSlider(armorProp, 0, 100, new GUIContent("Armor"));
// Only show the armor progress bar if all the objects have the same armor value:
if (!armorProp.hasMultipleDifferentValues)
ProgressBar(armorProp.intValue / 100.0f, "Armor");
EditorGUILayout.PropertyField(gunProp, new GUIContent("Gun Object"));
// Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
serializedObject.ApplyModifiedProperties();
}
// Custom GUILayout progress bar.
void ProgressBar(float value, string label)
{
// Get a rect for the progress bar using the same margins as a textfield:
Rect rect = GUILayoutUtility.GetRect(18, 18, "TextField");
EditorGUI.ProgressBar(rect, value, label);
EditorGUILayout.Space();
}
}
3.InitializeOnLoad属性
添加到一个类上面,当unity 加载的时候初始化一个Editor类,或者当脚本重新编译的时候
当重新编译项目中的脚本时,将调用具有此属性的静态构造函数. 这发生在Unity第一次加载你的项目的时候,也发生在Unity检测到脚本的修改的时候,就是执行该类的静态构造函数,把该属性放在类上
4.InitializeOnLoadMethod属性
写到类上面,这个类是放在Editor文件夹下的,也就是一个editor类,初始化其中的静态static方法,在unity加载的时候,多用于在编辑器下调试
using UnityEngine;
using UnityEditor;
class MyClass
{
[InitializeOnLoadMethod]
static void OnProjectLoadedInEditor()
{
Debug.Log("Project loaded in Unity Editor");
}
}
5.MenuItem
添加到static方法上面,MenuItem 添加一个菜单项,
该菜单项可以有快捷键: % (ctrl on Windows, cmd on macOS), # (shift), & (alt).如果不需要这三个键组合,直接下划线后面个键名就可以了. 比如 shift+alt+g use "MyMenu/Do Something #&g"
. 或者 g "MyMenu/Do Something _g"
.
一些特殊的键盘键被支持为热键, for example "#LEFT" shift+left箭头. : LEFT, RIGHT, UP, DOWN, F1 .. F12, HOME, END, PGUP, PGDN.小键盘上的键都是大写的
menuitem路径与快捷键之间要有空格空出 ("MyMenu/Do_g"
这种方式不行, while "MyMenu/Do _g"
要有空格键隔开才行).
当添加菜单项到"GameObject/"菜单创建自定义游戏对象时,一定要调用 GameObjectUtility.SetParentAndAlign 确保新的游戏物体的层级和位置关系正确.或者使用 Undo.RegisterCreatedObjectUndo 撤销操作,使用 Selection.activeObject 选中创建的物体. 并且创建的菜单可以根据优先级分层级,
MenuItem有三个参数,第一个是菜单栏的路径,第二个表示如果有相同名字的菜单栏,优先执行哪一个方法,为true则执行该方法,为false则表示不执行该方法,第三个参数为优先级,表示上下排列的顺序,小的在上,不写则默认为1000。
另外,如果相邻的两个的priority参数值相差>=11,则认为是不同组的,中间会有线分割显示:
[MenuItem("EamTools/T1",false,1)]
static void T1()
{
}
[MenuItem("EamTools/T2",false,12)]
static void T2()
{
}
[MenuItem("EamTools/T3",false,0)]
static void T3()
{
}
using UnityEditor;
using UnityEngine;
public class MenuTest : MonoBehaviour
{
[MenuItem("MyMenu/Do Something")]
static void DoSomething()
{
Debug.Log("Doing Something...");
}
[MenuItem("MyMenu/Log Selected Transform Name")]
static void LogSelectedTransformName()
{
Debug.Log("Selected Transform is on " + Selection.activeTransform.gameObject.name + ".");
}
[MenuItem("MyMenu/Log Selected Transform Name", true)]
static bool ValidateLogSelectedTransformName()
{
// Return false if no transform is selected.
return Selection.activeTransform != null;
}
[MenuItem("MyMenu/Log Selected Transform Name", false)]
static void Show()
{
// Return false if no transform is selected.
Debug.Log(Selection.activeTransform != null);
}
// Add a menu item named "Do Something with a Shortcut Key" to MyMenu in the menu bar
// and give it a shortcut (ctrl-g on Windows, cmd-g on macOS).
[MenuItem("MyMenu/Do Something with a Shortcut Key %g")]
static void DoSomethingWithAShortcutKey()
{
Debug.Log("Doing something with a Shortcut Key...");
}
// Add a menu item called "Double Mass" to a Rigidbody's context menu.
//给指定组件右键添加一个属性,contextMenu,只是给脚本组件右键添加一个属性
//MenuCommand有两个属性,context表示右键点击的组件
//"CONTEXT/Rigidbody/Double Mass",CONTEXT固定写法,Rigidbody 目标添加属性的组件名字,可以是自定义脚本,这样就和 contextMenu一样了
[MenuItem("CONTEXT/Rigidbody/Double Mass")]
static void DoubleMass(MenuCommand command)
{
Rigidbody body = (Rigidbody)command.context;
body.mass = body.mass * 2;
Debug.Log("Doubled Rigidbody's Mass to " + body.mass + " from Context Menu.");
}
// Add a menu item to create custom GameObjects.
// Priority 1 ensures it is grouped with the other menu items of the same kind
// and propagated to the hierarchy dropdown and hierarchy context menus.
[MenuItem("GameObject/MyCategory/Custom Game Object", false, 10)]
static void CreateCustomGameObject(MenuCommand menuCommand)
{
// Create a custom game object
GameObject go = new GameObject("Custom Game Object");
// Ensure it gets reparented if this was a context click (otherwise does nothing)
GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
// Register the creation in the undo system
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
Selection.activeObject = go;
}
}
undo撤销操作,要先注册撤销,然后操作
类似: