扩展
- 检视器扩展开发inspector
- 顶部菜单栏
- 弹窗
- 场景编辑器,编辑器主场景页面
特殊目录
- 多目录会自动合并,都是特殊目录
- 重名会被覆盖
- 游戏逻辑中不能带有UnityEditor中,否则会打包错误
检视器
扩展1 不属于Editor的
using System;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class Numberical {
public float Atk;
public float Def;
}
public enum Profression {
Warrior=0,
Wizard,
}
public class SimpleInspector : MonoBehaviour
{
[HideInInspector]
public int ID;
[SerializeField]
private string ShowID;
public Numberical Num;
[Space(50)]
[Header("年龄")]
[Tooltip("不要填写大约150的年龄")]
[Range(0,150)]
public int Age;
[Multiline(5)]
public string NickName;
[TextArea(5,8)]
public string Decripttion;
public Color Flag;
public Texture Tex;
public List<string> Tags;
public Profression Pro;
[ContextMenu("输出攻防比")]
public void PrintADProportion() {
Debug.Log("攻防比:"+Num.Atk/Num.Def);
}
[ContextMenuItem("输出国家", "OutCountry")]
[Tooltip("右键看看")]
public string Country;
public void OutCountry() {
Debug.Log(Country);
}
}
扩展2 显示开发
using System.Collections.Generic;
using UnityEngine;
public enum PlayerProfression {
Warrior = 0,
Wizard=1,
}
public enum PlayerLoveColor {
Green=1,
Red=2,
Pink=4,
}
[AddComponentMenu("自定义控制器/玩家控制器",1)]
[ExecuteInEditMode]
[RequireComponent(typeof(BoxCollider))]
public class Player : MonoBehaviour
{
public int ID;
public string Name;
public float Atk;
public bool isMan;
public Vector3 HeadDir;
public Color Hair;
public GameObject Weapon;
public Texture Cloth;
public PlayerProfression Pro;
public PlayerLoveColor LoveColor;
public List<string> Items;
private void Update() {
Debug.Log("Update");
}
}
- 检视器高级修改,外挂式编程
- Editor目录下建立外挂式开发脚本
- 将编辑器脚本,与原始脚本关联
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Player))]
public class PlayerEditor : Editor
{
private Player _Component;
private void OnEnable() {
_Component = target as Player;
}
private void OnDisable() {
_Component = null;
}
public override void OnInspectorGUI() {
EditorGUILayout.LabelField("人物相关属性");
_Component.ID=EditorGUILayout.IntField("玩家ID",_Component.ID);
_Component.Hair = EditorGUILayout.ColorField("头发颜色", _Component.Hair);
_Component.Name = EditorGUILayout.TextField("玩家名字", _Component.Name);
_Component.isMan = EditorGUILayout.Toggle("是否位男性", _Component.isMan);
_Component.HeadDir = EditorGUILayout.Vector3Field("头发方向", _Component.HeadDir);
_Component.Atk = EditorGUILayout.FloatField("攻击力", _Component.Atk);
_Component.Weapon = EditorGUILayout.ObjectField("持有武器", _Component.Weapon, typeof
(GameObject),true)as GameObject;
_Component.Cloth = EditorGUILayout.ObjectField("衣服材质贴图", _Component.Cloth, typeof
(Texture), false) as Texture;
_Component.Pro = (PlayerProfression)EditorGUILayout.EnumPopup("玩家职业", _Component.Pro);
_Component.LoveColor = (PlayerLoveColor)EditorGUILayout.EnumFlagsField("玩家喜欢的颜色",
_Component.LoveColor);
serializedObject.Update();
SerializedProperty sp = serializedObject.FindProperty("Items");
EditorGUILayout.PropertyField(sp, new GUIContent("道具信息"), true);
serializedObject.ApplyModifiedProperties();
_Component.Atk = EditorGUILayout.Slider(new GUIContent("玩家攻击力"), _Component.Atk, 0,100);
if (_Component.Atk > 80) {
EditorGUILayout.HelpBox("攻击力太高了", MessageType.Error);
}
if (_Component.Atk < 20) {
EditorGUILayout.HelpBox("攻击力太低了", MessageType.Warning);
}
GUILayout.Button("来个按钮");
GUILayout.Button("来个按钮");
EditorGUILayout.BeginHorizontal();
GUILayout.Button("来个按钮");
GUILayout.Button("来个按钮");
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("测试点击")) {
Debug.Log("测试点击");
}
}
}
顶部菜单
顶部菜单
using UnityEngine;
using UnityEditor;
public class Menu
{
[MenuItem("工具/导出AB资源包")]
static void BuildAB() {
Debug.Log("导出AB资源包");
Debug.Log(Application.persistentDataPath);
}
}
窗口
using UnityEngine;
using UnityEditor;
public class PopWindow : EditorWindow
{
[MenuItem("工具/创建窗口")]
static void OpenWindow() {
PopWindow window= GetWindow<PopWindow>(false,"弹窗",true);
window.minSize = new Vector2(400, 300);
window.maxSize = new Vector2(800, 600);
}
private void OnEnable() {
Debug.Log("Enable");
}
private void OnDisable() {
Debug.Log("DisEnable");
}
private void Update() {
Debug.Log("Update");
}
private void OnGUI() {
GUILayout.Button("来个按钮");
GUILayout.Button("来个按钮");
EditorGUILayout.BeginHorizontal();
GUILayout.Button("来个按钮");
GUILayout.Button("来个按钮");
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("测试点击")) {
Debug.Log("测试点击");
}
}
private void OnHierarchyChange() {
Debug.Log("HierarchyChange");
}
private void OnProjectChange() {
Debug.Log("ProjectChange");
}
private void OnSelectionChange() {
Debug.Log(Selection.activeGameObject.name);
}
}
场景视窗扩展
- 脚本挂在物体上
- 扩展外挂式编程
- 窗口持续位选择状态
- 编辑器状态下,从编辑器GUI界面发射线,而不是摄像机
- 必须开启窗口,不然界面直接切换到新建物体上,编辑停止