提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
注意事项
编辑器开发的代码要放在Editor目录下,这样才不会被打包到游戏。
需要打包到游戏的脚本不能引用UnityEditor命名空间,否则打包失败
Inspector面板一些常用C#特性
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class TestClass
{
public int a, b;
}
//该脚本在AddComponent菜单下的分类 参数1:路径,参数2:排在第几位
[AddComponentMenu("这是一个归类/SimpleInspetor脚本",1)]
//可以在编辑模式下运行(不用Run)
[ExecuteInEditMode]
//挂上这个脚本的物体必须有某个组件,这里是Transform
[RequireComponent(typeof(Transform))]
public class SimpleInspetor : MonoBehaviour
{
//在检视面板隐藏
[HideInInspector]
public int id = 0;
//在检视面板可见
//SerializeField 表示序列化,MonoBehaviour里公有变量默认是可序列化的
//可序列化的才能在检视面板显示
//其他class想要在检视面板显示也要加Serializable,哪怕它是公有的,class默认是不可序列化
[SerializeField]
[Multiline(5)]//多行输入,这里是5行
[ContextMenuItem("输出名字", "OutName")]//右键点击变量就显示这个菜单,点击后就调用OutName这个函数
private string name = "";
public TestClass testClass;
[Header("范围")]//在检视面板显示
[Tooltip("这是提示语句")]//智能提示与鼠标停留时候显示
[Range(0,100)]//滑动条
[Space(50)]//与上面的显示间隔50个像素
public float rag;
[TextArea(5,10)]//也是多行输入,有最小显示行数与最大显示行数,实际可以输入很多行
public string des;
[ContextMenu("调个函数试试")]//可以在脚本组件右上角菜单里看到
public void TestFun()
{
Debug.Log("调用TestFun");
}
public void OutName()
{
Debug.Log(name);
}
}
重写Inspector面板
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public int ID;
public string name = "233";
public List<int> myList = new List<int>();
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
//关联对应的组件 这里是Player组件
//Player组件如何在面板显示将由这里决定
[CustomEditor(typeof(Player))]
public class PlayerEditor : Editor
{
private Player _pCom;
//组件添加或被选中时调用
private void OnEnable()
{
//获得当前选中的对象
_pCom = target as Player;
}
private void OnDisable()
{
}
//决定选中后的组件在Inspector中如何显示
public override void OnInspectorGUI()
{
//base.OnInspectorGUI();
EditorGUILayout.LabelField("人物相关属性");
//EditorGUILayout.IntField返回面板中修改好的数
int id = EditorGUILayout.IntField("人物ID:", _pCom.ID);
if (id < 0)
{
//输出错误或警告
EditorGUILayout.HelpBox("ID必须大于0", MessageType.Error);
}
else
{
_pCom.ID = id;
}
//绘制List数据
serializedObject.Update();
SerializedProperty sp = serializedObject.FindProperty("myList");
EditorGUILayout.PropertyField(sp, new GUIContent("这是个列表"), true);
serializedObject.ApplyModifiedProperties();
}
}
顶部菜单栏
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Menu
{
[MenuItem("工具/打印一下")]
static void OutDebug()
{
Debug.Log("打印了一下");
}
}
弹出窗口
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class PopWindow : EditorWindow
{
[MenuItem("工具/创建窗口")]
static void CreateWindow()
{
PopWindow window = GetWindow<PopWindow>(false, "窗口标题", true);
window.minSize = new Vector2(500, 500);
}
private void OnEnable()
{
}
private void OnDisable()
{
}
private void Update()
{
}
private void OnGUI()
{
EditorGUILayout.LabelField("这是一段字");
if (GUILayout.Button("打印一下"))
{
Debug.Log("打印了一下");
}
}
}