作者:uai_128366833952 | 来源:互联网 | 2023-09-23 22:35
特殊目录
Plugins:需要跨语言调用的代码逻辑代码储存目录,手机SDK接入
Resource:储存跟随游戏包的资源目录
StreamingAssets:只读,存储跟游戏包的资源目录
编辑目录:Editor
制作多目录合并
项目中建立的Editor目录,编辑器相关的逻辑和资源会放在其内部,相关内容,
在打包生成时,不会一起生成到项目中,玩家也不会使用到编辑器相关的内容。
//隐藏公共成员变量,同时保证脚本中变量的可访问度
[HideInInspector]
//私有变量,检视面板可见
[SerializeField]
//对象如果不标记为可序列化,则Unity在存储的时候,会认为他不可被序列化,那么也就无法被显示
[Serializable]
//给数值设定范围
[Range(int,int)]
//添加变量悬浮提示文字
[Tooltip(string)]
//当前成员变量上方加入一个标题文字
[Header(string)]
//指定输入框,拥有int行数据
[Multiline(int)]
//默认显示min行,最多显示max行内容,再多用滚动条控制显示区域
[TextArea(min,max)]
//小齿轮增加一个回调函数
[ContextMenu(string)]
//第一个参数是菜单名称,第二个参数是右键点击的回调函数
[ContextMenuItem(name,funntion)]
//当前组件依赖于盒子碰撞器,当前组件挂载再对象上时,盒子碰撞体会一起被添加上,当该脚本没有被移除时,盒子碰撞体不能被移除
[RequireComponent(typeof(BoxCollider))]
//使生命周期函数,在编辑状态下可以运行,游戏中也可以正常使用
[ExecuteInEditMode]
//将Player组件添加到AddComponent上,第一个参数:分组名/组件名,第二个参数:列表中显示的顺序
[AddComponentMenu("自定义文件夹/自定义脚本",1)]
检视器高级修改(外挂式编程)
项目中建立Editor目录

Editor目录下建立外挂式开发脚本

将编辑器脚本,与原始脚本进行关联
外挂脚本的方法
1.引入编辑器的命名空间,检视器属于编辑器开发范畴

2.继承Editor类,使用编辑器相关的成员变量和生命周期函数

3.将编辑器开发脚本与需要编辑的组件脚本建立外挂关联关系

4.需要在当前的外挂脚本中,获得需要被扩展的Player组件对象

5.获取Player组件对象

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;[CustomEditor(typeof(Player))]
//外挂的脚本因为存储在Editor目录下,所以不会被打入最终的游戏包
//不继承自Mono,而是继承自Editor
public class PlayerEditor : Editor
{//获得到需要编辑显示的组件private Player _Component;//当关联组件所在对象被添加或选中时调用private void OnEnable(){_CompOnent= target as Player;}//当关联组件所在对象被删除或取消时调用private void OnDisable(){_CompOnent= null;}
}
private void OnEnable()
当关联组件所在对象被选中或组件被添加时,调用

当关联组件所在对象被取消或组件被移除时,调用
private void OnDisable()

//用于绘制检视面板的生命周期函数
public override void OnInspectorGUI()

//标题显示
EditorGUILayout.LabelField("人物相关属性");
将玩家的ID展示出来
_Component.ID = EditorGUILayout.IntField("玩家ID", _Component.ID);

对象数据类型绘制
参数1:标题
参数2:原始组件的值
参数3:成员变量的类型
参数4:是否可以将场景中的对象拖给这个成员变量
_Component.Weapon = EditorGUILayout.ObjectField("持有武器", _Component.Weapon, typeof(GameObject), true) as GameObject;

单选枚举(标题, 组件上的原始值)
_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();

滑动条绘制
滑动条显示(1.标题,2.原始变量,最小值,最大值)
_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("来个按钮");
if (GUILayout.Button("测试点击"))
{
Debug.Log("测试点击");
}
开始横向排列绘制
EditorGUILayout.BeginHorizontal();
GUILayout.Button("再来个按钮");
GUILayout.Button("再来个按钮");
结束横向排列绘制
EditorGUILayout.EndHorizontal();

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;[CustomEditor(typeof(Player))]
//外挂的脚本因为存储在Editor目录下,所以不会被打入最终的游戏包
//不继承自Mono,而是继承自Editor
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);//参数4:是否可以将场景中的对象拖给这个成员变量_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();滑动条绘制//滑动条显示(1.标题,2.原始变量,最小值,最大值)_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("来个按钮");if (GUILayout.Button("测试点击")){Debug.Log("测试点击");}//开始横向排列绘制EditorGUILayout.BeginHorizontal();GUILayout.Button("再来个按钮");GUILayout.Button("再来个按钮");//结束横向排列绘制EditorGUILayout.EndHorizontal();}
}