Editor

变量说明
serializedObjectA SerializedObject representing the object or objects being inspected.
targetThe object being inspected.
targetsAn array of all the object being inspected.

公有函数说明
DrawDefaultInspectorDraw the built-in inspector.
DrawHeaderCall this function to draw the header of the editor.
DrawPreviewThe first entry point for Preview Drawing.
GetInfoStringImplement this method to show asset information on top of the asset preview.
GetPreviewTitleOverride this method if you want to change the label of the Preview area.
HasPreviewGUIOverride this method in subclasses if you implement OnPreviewGUI.
OnInspectorGUIImplement this function to make a custom inspector.
OnInteractivePreviewGUIImplement to create your own interactive custom preview. Interactive custom previews are used in the preview area of the inspector and the object selector.
OnPreviewGUIImplement to create your own custom preview for the preview area of the inspector, primary editor headers and the object selector.
OnPreviewSettingsOverride this method if you want to show custom controls in the preview header.
RenderStaticPreviewOverride this method if you want to render a static preview that shows.
RepaintRepaint any inspectors that shows this editor.
RequiresConstantRepaintDoes this edit require to be repainted constantly in its current state?
UseDefaultMarginsOverride this method in subclasses to return false if you don't want default margins.

静态方法说明
CreateCachedEditorOn return previousEditor is an editor for targetObject or targetObjects. The function either returns if the editor is already tracking the objects, or Destroys the previous editor and creates a new one.
CreateEditorMake a custom editor for targetObject or targetObjects.

Messages说明
OnSceneGUIEnables the Editor to handle an event in the scene view.

示例:

  1. using UnityEngine;
  2. using System.Collections;
  3. // This is not an editor script.
  4. public class MyPlayer : MonoBehaviour {
  5. public int armor = 75;
  6. public int damage = 25;
  7. public GameObject gun;
  8. void Update () {
  9. // Update logic here...
  10. }
  11. }

 2.40 Editor  - 图1

  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Collections;
  4. // Custom Editor using SerializedProperties.
  5. // Automatic handling of multi-object editing, undo, and prefab overrides.
  6. [CustomEditor(typeof(MyPlayer))]
  7. [CanEditMultipleObjects]
  8. public class MyPlayerEditor : Editor {
  9. SerializedProperty damageProp;
  10. SerializedProperty armorProp;
  11. SerializedProperty gunProp;
  12. void OnEnable () {
  13. // Setup the SerializedProperties.
  14. damageProp = serializedObject.FindProperty ("damage");
  15. armorProp = serializedObject.FindProperty ("armor");
  16. gunProp = serializedObject.FindProperty ("gun");
  17. }
  18. public override void OnInspectorGUI() {
  19. // Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
  20. serializedObject.Update ();
  21. // Show the custom GUI controls.
  22. EditorGUILayout.IntSlider (damageProp, 0, 100, new GUIContent ("Damage"));
  23. // Only show the damage progress bar if all the objects have the same damage value:
  24. if (!damageProp.hasMultipleDifferentValues)
  25. ProgressBar (damageProp.intValue / 100.0f, "Damage");
  26. EditorGUILayout.IntSlider (armorProp, 0, 100, new GUIContent ("Armor"));
  27. // Only show the armor progress bar if all the objects have the same armor value:
  28. if (!armorProp.hasMultipleDifferentValues)
  29. ProgressBar (armorProp.intValue / 100.0f, "Armor");
  30. EditorGUILayout.PropertyField (gunProp, new GUIContent ("Gun Object"));
  31. // Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
  32. serializedObject.ApplyModifiedProperties ();
  33. }
  34. // Custom GUILayout progress bar.
  35. void ProgressBar (float value, string label) {
  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. }

另外,如果自动处理多对象编辑、撤销、和预制重写是不需要的,脚本变量可以直接修改由编辑器不使用serializedobject和serializedproperty系统,如下面的例子。

  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Collections;
  4. // Example script with properties.
  5. public class MyPlayer : MonoBehaviour {
  6. public int damage;
  7. public int armor;
  8. public GameObject gun;
  9. // ...other code...
  10. }
  11. // Custom Editor the "old" way by modifying the script variables directly.
  12. // No handling of multi-object editing, undo, and prefab overrides!
  13. [CustomEditor (typeof(MyPlayer))]
  14. public class MyPlayerEditor : Editor {
  15. public override void OnInspectorGUI () {
  16. MyPlayer mp = (MyPlayer)target;
  17. mp.damage = EditorGUILayout.IntSlider ("Damage", mp.damage, 0, 100);
  18. ProgressBar (mp.damage / 100.0f, "Damage");
  19. mp.armor = EditorGUILayout.IntSlider ("Armor", mp.armor, 0, 100);
  20. ProgressBar (mp.armor / 100.0f, "Armor");
  21. bool allowSceneObjects = !EditorUtility.IsPersistent (target);
  22. mp.gun = (GameObject)EditorGUILayout.ObjectField ("Gun Object", mp.gun, typeof(GameObject), allowSceneObjects);
  23. }
  24. // Custom GUILayout progress bar.
  25. void ProgressBar (float value, string label) {
  26. // Get a rect for the progress bar using the same margins as a textfield:
  27. Rect rect = GUILayoutUtility.GetRect (18, 18, "TextField");
  28. EditorGUI.ProgressBar (rect, value, label);
  29. EditorGUILayout.Space ();
  30. }
  31. }
  1. void OnSceneGUI()
  2. {
  3. m_pEvent = Event.current;
  4. if (m_pEvent.type == EventType.mouseDown && m_pEvent.button == 1) {
  5. Ray ray = HandleUtility.GUIPointToWorldRay(m_pEvent.mousePosition);
  6. if(isAddNode && Physics.Raycast(ray, out hit))
  7. {
  8. GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
  9. go.transform.position = hit.point;
  10. go.transform.SetParent(emiter.transform);
  11. }
  12. }
  13. GUILayout.BeginArea (new Rect (Screen.width - 200, Screen.height - 100, 100, 50));
  14. isAddNode = GUILayout.Toggle (isAddNode, "isAddNode");
  15. GUILayout.EndArea ();
  16. }

?