C#特性
[Range(0,100)] //限制数值范围
[Multiline(3)] //字符串多行显示
[TextArea(2,4)] //文本输入框
[SerializeField] //序列化字段,主要用于序列化私有字段
[NonSerialized] //反序列化一个变量,并且在Inspector上隐藏
[HideInInspector] //public变量在Inspector面板隐藏
[FormerlySerializedAs(“Value1”)] //当变量名发生改变时,可以保存原来Value1的值
[ContextMenu(“TestBtn”)] //组件右键菜单按钮
[ContextMenuItem(“Reset Value”,“Reset”)] //定义属性的右键菜单
[Header(“Header Name”)] //加粗效果的标题
[Space(10)] //表示间隔空间,数字越大,间隔越大
[Tooltip(“Tips”)] //显示字段的提示信息
[ColorUsage(true)] //显示颜色面板
[DrawGizmo] //用于Gizmos渲染,将逻辑与调试代码分离
[MenuItem] //添加菜单项
[Serializable] //序列化一个类,作为一个子属性显示在监视面板
[RequireComponent(typeof(Animator))] //挂载该类的对象,必须要有Animator组件
[DisallowMultipleComponent] //不允许挂载多个该类或其子类
[ExecuteInEditMode] //允许脚本在编辑器未运行的情况下运行
[CanEditMultipleObjects] //允许当选择多个挂有该脚本的对象时,统一修改值
[AddComponentMenu] //可以在菜单栏Component内添加组件按钮
[CustomEditor] //要自定义编辑器就要加这个特性
[CustomPropertyDrawer] //用于绘制自定义PropertyDrawer的特性
[SelectionBase] //选择在场景视图中使用此属性的组件对象,即不会误选中子物体
多个特性可以用逗号隔开,例如:[SerializeField, Range(0,5)]
添加菜单
using UnityEngine;
using UnityEditor;
public class HelloWorldEditor
{
//创建一个菜单
[MenuItem("MyTools/Create HelloWorld")]
private static void CreateHelloWorldGameObject()
{
bool yes = EditorUtility.DisplayDialog("Hello World", "Do you really want to do this?", "Create", "Cancel");
if (yes)
{
new GameObject("HelloWorld");
}
}
}
如图点击MyTools/Create HelloWorld菜单,之后会自动创建一个HelloWorld的游戏对象。


Editor文件夹
Unity创建C#的解决方案时,会创建俩个项目,如图所示:

- Assembly-CSharp是所有的运行时脚本的项目。
- Assembly-CSharp-Editor是所有的Editor脚本项目。
Unity在打包的时候只会将Assembly-CSharp项目打包到运行包中,因此如果打包时,有Editor脚本,就会打包失败。有如下俩种解决方案。
方案一:使用宏定义#if UNITY_EDITOR和#endif
如上面的例子
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
public class HelloWorldEditor
{
//创建一个菜单
[MenuItem("MyTools/Create HelloWorld")]
private static void CreateHelloWorldGameObject()
{
bool yes = EditorUtility.DisplayDialog("Hello World", "Do you really want to do this?", "Create", "Cancel");
if (yes)
{
new GameObject("HelloWorld");
}
}
}
#endif
方案二:将编辑脚本放到Editor文件夹中,打包时,Editor文件夹不会打到运行包中。
如图:

常用的Editor类
- UnityEditor命名空间
EditorUtility类,Editor中常用的方法,如对话框,打开系统路径等
//打开对话框
EditorUtility.DisplayDialog("Hello World", "Do you really want to do this?", "Create", "Cancel");
//打开系统保存文件面板
EditorUtility.SaveFilePanel("保存", "Test", "defaultName", "png");
- UnityEditor.SceneManagement命名空间
EditorSceneManager类,在Editor中管理场景的方法。如
//是否保存当前场景
EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
//创建新的场景
EditorSceneManager.NewScene(NewSceneSetup.EmptyScene);
- AssetDatabase类
//获取指定路径下所有的prefab资源的guid。
string[] guids = AssetDatabase.FindAssets("t:Prefab", new string[] {path});
//根据guid获取对应资源的路径
string assetPath = AssetDatabase.GUIDToAssetPath(guids[0]);
//根据路径获取资源
GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
5.获取资源预览图
Texture2D preview = AssetPreview.GetAssetPreview(GameObject);
6.标记预设为修改状态
EditorUtility.SetDirty(prefab);
7.修改GUI颜色
Color c = GUI.backgroundColor;
GUI.backgroundColor = Color.green;
UI绘制...
GUI.backgroundColor = c;
8.选中Inspector中的GameObject
var all = GameObject.FindObjectsOfType<NewBehaviourScript>();
var allGameObjects = all.Select(e => e.gameObject).ToArray();
Selection.objects = allGameObjects;
9.Undo/Redo
Undo.RecordObject(gameObject, "Disable/Enable");
e.gameObject.SetActive(!gameObject.activeSelf);
10.执行Unity菜单选项
EditorApplication.ExecuteMenuItem("File/Build Settings...");
11.在project中选中一个资源
var asset = AssetDatabase.LoadAssetAtPath<SceneAsset>(path);
EditorGUIUtility.PingObject(asset);
网友评论