第一个:
一:首先建立一个空白工程,创建一个空GameObject,在Assets中创建一个名为CustomEditor的文件夹,在该文件夹中创建一个LevelScript的c#文件。
LevelScript.cs如下:
using UnityEngine;
using System.Collections;
public class LevelScript : MonoBehaviour
{
public int experience;
public int Level
{
get { return experience / 750; }
}
}
二:再在Assets文件夹下创建一个Editor文件夹,在Editor文件夹中创建一个名为LevelScriptEditor的c#文件,内容如下:
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(LevelScript))]
public class LevelScriptEditor : Editor
{
public override void OnInspectorGUI()
{
LevelScript myTarget = (LevelScript)target;
myTarget.experience = EditorGUILayout.IntField("Experience", myTarget.experience);
EditorGUILayout.LabelField("Level", myTarget.Level.ToString());
}
}
第五行应该为C#特性的声明吧
第12,13行就是运用EditorGUILayout把两个属性Experience,Level刷新再UI上面
第二个:添加一个按钮增加对象
像第一个在CumstomEditor的文件夹中添加ObjectBuilderScript的C#文件
using UnityEngine;
using System.Collections;
public class ObjectBuilderScript : MonoBehaviour
{
public GameObject obj;
public Vector3 spawnPoint;
public void BuildObject()
{
Instantiate(obj, spawnPoint, Quaternion.identity);
}
}
然后再Editor的文件夹添加
ObjectBuilderScriptEditor的C#文件
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(ObjectBuilderScript))]
public class ObjectBuilderScriptEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
ObjectBuilderScript myScript = (ObjectBuilderScript)target;
if(GUILayout.Button("Build Object"))
{
myScript.BuildObject();
}
}
}
便可以在Inspector中看到属性
第三个:DrawDefaultInspector的功能(第一个程序的简化)
像上面同样创建创建SomeScript和SomeScriptEditor两个C#文件分别在ComstumEditor和Editor的文件夹中
SomeScript.cs:
using System.Collections;
using UnityEngine;
using UnityEditor;
public class SomeScript : MonoBehaviour {
public int level;
public int health;
public Vector3 target;
}
SomeScriptEditor.cs
using System.Collections;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(SomeScript))]
public class SomeScriptEditor : Editor {
public override void OnInspectorGUI()
{
DrawDefaultInspector ();
EditorGUILayout.HelpBox ("This is a help box", MessageType.Info);
}
}
第四个:添加菜单栏的更多功能
创建的文件要在文件夹Editor的目录下
using System.Collections;
using UnityEditor;
using UnityEngine;
public class MenuItems {
[MenuItem("Tools/Clear PlayerPrefs")]
private static void NewMenuOption()
{
PlayerPrefs.DeleteAll ();
}
}
另外:热键的对应值
-
% – CTRL on Windows / CMD on OSX
-
# – Shift
-
& – Alt
-
LEFT/RIGHT/UP/DOWN – Arrow keys
-
F1…F2 – F keys
-
HOME, END, PGUP, PGDN
更多的情况[MenuItem("Tools/New Option %#a")] private static void NewMenuOption() { } // Add a new menu item with hotkey CTRL-G [MenuItem("Tools/Item %g")] private static void NewNestedOption() { } // Add a new menu item with hotkey G [MenuItem("Tools/Item2 _g")] private static void NewOptionWithHotkey() { }
-
Assets – items will be available under the “Assets” menu, as well using right-click inside the project view.(Assets菜单下)
-
Assets/Create – items will be listed when clicking on the “Create” button in the project view (useful when adding new types that can be added to the project)(Assets/create目录下)
-
CONTEXT/ComponentName – items will be available by right-clicking inside the inspector of the given component.(在Inspector的component点击右键)
[MenuItem("Assets/Load Additive Scene")] private static void LoadAdditiveScene() { var selected = Selection.activeObject; EditorApplication.OpenSceneAdditive(AssetDatabase.GetAssetPath(selected)); } // Adding a new menu item under Assets/Create [MenuItem("Assets/Create/Add Configuration")] private static void AddConfig() { // Create and add a new ScriptableObject for storing configuration } // Add a new menu item that is accessed by right-clicking inside the RigidBody component [MenuItem("CONTEXT/Rigidbody/New Option")] private static void NewOpenForRigidBody() { }
有效点击的情况:[MenuItem("Assets/ProcessTexture")] private static void DoSomethingWithTexture() { } // Note that we pass the same path, and also pass "true" to the second argument. [MenuItem("Assets/ProcessTexture", true)] private static bool NewMenuOptionValidation() { // This returns true when the selected object is a Texture2D (the menu item will be disabled otherwise). return Selection.activeObject.GetType() == typeof(Texture2D); }
控制添加的顺序:1,2,3在同一组,4,5在同一组;
Menu items are also automatically grouped according to their assigned priority in increments of 50:
[MenuItem("NewMenu/Option1", false, 1)] private static void NewMenuOption() { } [MenuItem("NewMenu/Option2", false, 2)] private static void NewMenuOption2() { } [MenuItem("NewMenu/Option3", false, 3)] private static void NewMenuOption3() { } [MenuItem("NewMenu/Option4", false, 51)] private static void NewMenuOption4() { } [MenuItem("NewMenu/Option5", false, 52)] private static void NewMenuOption5() { }
-