当前位置:   article > 正文

unity 编辑器属性 之 Editor_caneditmultipleobjects

caneditmultipleobjects

1.CanEditMultipleObjects属性

 添加到类上面, 在自定义编辑器时,允许多对象编辑,就是同时选中多个物体,统一修改共同的值

这是没有加该属性

2.CustomEditor属性

添加到类上面,为一个组件或者脚本自定义属性面板,定义类要继承editor类,CustomEditor(typeof(你自己的类名))

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class EditorAttribute : MonoBehaviour
  5. {
  6. //属性必须是public的,并且和你自定义编辑器中的类型是一致的,比如这里是int
  7. 类型,你在编辑器中就用intslider
  8. public int damage;
  9. public int armor;
  10. public GameObject gun;
  11. }
  1. using UnityEngine;
  2. using UnityEditor;
  3. [CustomEditor(typeof(EditorAttribute))]
  4. public class EditorAttributes : Editor
  5. {
  6. SerializedProperty damageProp;
  7. SerializedProperty armorProp;
  8. SerializedProperty gunProp;
  9. void OnEnable()
  10. {
  11. // Setup the SerializedProperties.
  12. damageProp = serializedObject.FindProperty("damage");
  13. armorProp = serializedObject.FindProperty("armor");
  14. gunProp = serializedObject.FindProperty("gun");
  15. }
  16. public override void OnInspectorGUI()
  17. {
  18. // Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
  19. serializedObject.Update();
  20. // Show the custom GUI controls.
  21. EditorGUILayout.IntSlider(damageProp, 0, 100, new GUIContent("Damage"));
  22. // Only show the damage progress bar if all the objects have the same damage value:
  23. if (!damageProp.hasMultipleDifferentValues)
  24. ProgressBar(damageProp.intValue / 100.0f, "Damage");
  25. EditorGUILayout.IntSlider(armorProp, 0, 100, new GUIContent("Armor"));
  26. // Only show the armor progress bar if all the objects have the same armor value:
  27. if (!armorProp.hasMultipleDifferentValues)
  28. ProgressBar(armorProp.intValue / 100.0f, "Armor");
  29. EditorGUILayout.PropertyField(gunProp, new GUIContent("Gun Object"));
  30. // Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
  31. serializedObject.ApplyModifiedProperties();
  32. }
  33. // Custom GUILayout progress bar.
  34. void ProgressBar(float value, string label)
  35. {
  36. // Get a rect for the progress bar using the same margins as a textfield:
  37. Rect rect = GUILayoutUtility.GetRect(18, 18, "TextField");
  38. EditorGUI.ProgressBar(rect, value, label);
  39. EditorGUILayout.Space();
  40. }
  41. }

3.InitializeOnLoad属性

添加到一个类上面,当unity 加载的时候初始化一个Editor类,或者当脚本重新编译的时候

当重新编译项目中的脚本时,将调用具有此属性的静态构造函数. 这发生在Unity第一次加载你的项目的时候,也发生在Unity检测到脚本的修改的时候,就是执行该类的静态构造函数,把该属性放在类上

4.InitializeOnLoadMethod属性

写到类上面,这个类是放在Editor文件夹下的,也就是一个editor类,初始化其中的静态static方法,在unity加载的时候,多用于在编辑器下调试

  1. using UnityEngine;
  2. using UnityEditor;
  3. class MyClass
  4. {
  5. [InitializeOnLoadMethod]
  6. static void OnProjectLoadedInEditor()
  7. {
  8. Debug.Log("Project loaded in Unity Editor");
  9. }
  10. }

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,则认为是不同组的,中间会有线分割显示:

    

  1. [MenuItem("EamTools/T1",false,1)]
  2. static void T1()
  3. {
  4. }
  5. [MenuItem("EamTools/T2",false,12)]
  6. static void T2()
  7. {
  8. }
  9. [MenuItem("EamTools/T3",false,0)]
  10. static void T3()
  11. {
  12. }
  1. using UnityEditor;
  2. using UnityEngine;
  3. public class MenuTest : MonoBehaviour
  4. {
  5. [MenuItem("MyMenu/Do Something")]
  6. static void DoSomething()
  7. {
  8. Debug.Log("Doing Something...");
  9. }
  10. [MenuItem("MyMenu/Log Selected Transform Name")]
  11. static void LogSelectedTransformName()
  12. {
  13. Debug.Log("Selected Transform is on " + Selection.activeTransform.gameObject.name + ".");
  14. }
  15. [MenuItem("MyMenu/Log Selected Transform Name", true)]
  16. static bool ValidateLogSelectedTransformName()
  17. {
  18. // Return false if no transform is selected.
  19. return Selection.activeTransform != null;
  20. }
  21. [MenuItem("MyMenu/Log Selected Transform Name", false)]
  22. static void Show()
  23. {
  24. // Return false if no transform is selected.
  25. Debug.Log(Selection.activeTransform != null);
  26. }
  27. // Add a menu item named "Do Something with a Shortcut Key" to MyMenu in the menu bar
  28. // and give it a shortcut (ctrl-g on Windows, cmd-g on macOS).
  29. [MenuItem("MyMenu/Do Something with a Shortcut Key %g")]
  30. static void DoSomethingWithAShortcutKey()
  31. {
  32. Debug.Log("Doing something with a Shortcut Key...");
  33. }
  34. // Add a menu item called "Double Mass" to a Rigidbody's context menu.
  35. //给指定组件右键添加一个属性,contextMenu,只是给脚本组件右键添加一个属性
  36. //MenuCommand有两个属性,context表示右键点击的组件
  37. //"CONTEXT/Rigidbody/Double Mass",CONTEXT固定写法,Rigidbody 目标添加属性的组件名字,可以是自定义脚本,这样就和 contextMenu一样了
  38. [MenuItem("CONTEXT/Rigidbody/Double Mass")]
  39. static void DoubleMass(MenuCommand command)
  40. {
  41. Rigidbody body = (Rigidbody)command.context;
  42. body.mass = body.mass * 2;
  43. Debug.Log("Doubled Rigidbody's Mass to " + body.mass + " from Context Menu.");
  44. }
  45. // Add a menu item to create custom GameObjects.
  46. // Priority 1 ensures it is grouped with the other menu items of the same kind
  47. // and propagated to the hierarchy dropdown and hierarchy context menus.
  48. [MenuItem("GameObject/MyCategory/Custom Game Object", false, 10)]
  49. static void CreateCustomGameObject(MenuCommand menuCommand)
  50. {
  51. // Create a custom game object
  52. GameObject go = new GameObject("Custom Game Object");
  53. // Ensure it gets reparented if this was a context click (otherwise does nothing)
  54. GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
  55. // Register the creation in the undo system
  56. Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
  57. Selection.activeObject = go;
  58. }
  59. }

undo撤销操作,要先注册撤销,然后操作,相关链接:【Unity3d编辑器从入门到精通】深入简出聊Undo - 简书

类似:

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号