这里使用的是unity2020.1
对于unity编辑器开发也不是很了解,这方面的教程也不多,也是慢慢摸索的
效果显示
首先简单
介绍下Unity编辑器开发
1:Editor下打开新窗口需要继承EditorWindow,然后使用获取窗口即可,注意放在Editor文件夹下
public class DrawGameLevel : EditorWindow
{
[MenuItem("Maps/Create Map &M")]
public static void OpenMapCreate()
{
DrawGameLevel window = EditorWindow.GetWindow<DrawGameLevel>("地图编辑器");
window.Show();
window.minSize = new Vector2(500, 800);
window.maxSize = new Vector2(600, 1200);
}
}
2:因为是在Scene视图下进行操作,所以注册SceneView.duringSceneGui事件,在OnEnable中
void OnEnable()
2 {
3 SceneView.duringSceneGui += OnSceneGUI;
4 //初始化一些东西
5 }
6
7 void OnDestroy()
8 {
9 SceneView.duringSceneGui -= OnSceneGUI;
10 }
3:unity 编辑器开发,需要在GUI中写,使用EditorGUILayout或者GUILayout来显示ui,基本的组件都是有的
这是我实现的界面和代码
#region GUI显示
private string brushY = "";// 刷新的y坐标
private string brushX = "";// 刷新的x坐标
private string brushDisY = "0";// 刷新的y坐标间距
private string brushDisX = "0";// 刷新的x坐标间距
private string zIndex = "-0.2";// 显示的层级
private string fixCount = "0";// 固定生成个数,设置0,一直生成
private float _lastBrushY = -10000;// 保持Y合理间距 上次刷的y的坐标
private float _lastBrushX = -10000;// 保持X合理间距 上一个刷的x的坐标
private int _curImgIndex = -1;//当前正在使用的图
private int _curFixCount = 0;// 当前已经生成的个数
private bool _drag = false;// 鼠标拖拽?
private int _select = 0;// 当前选择的图片 index
private string ck = "1";// 关卡
private Texture[] _items = null;// 地形基础图 要使用的地块
private GameObject parentMap = null;// 地图节点的父节点
private GameObject bgParent = null;// 背景图挂点
private string[] bgNames = { "Forest", "Night", "Winter" };// 背景图名字
private int selectMapIndex = 0;// 选择的地图背景图index
private int lastSelectMapIndex = -1;// 上次选择的地图背景图index
private int funcTypeIndex = 0;// 功能类型的index
private string[] funcTypeArry = { "无","橡皮擦","选中单个物体"};// 选择单个物体
private int selectTypeIndex = 0;
private int lastSelectTypeIndex = 0;// 上次选择的类型
private string[] typeArry = {"不显示选图", "地形图","障碍物",};
// Update is called once per frame
void OnGUI()
{
GUI.skin.label.normal.textColor = Color.yellow;
GUI.skin.label.fontSize = 15;
GUIStyle popStyle = new GUIStyle("Popup");
popStyle.fontSize = 15;
// 选择地图
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
GUILayout.Label("1.选择地图", GUILayout.Width(200));
this.selectMapIndex = EditorGUILayout.Popup(this.selectMapIndex, bgNames, popStyle, GUILayout.MaxWidth(100));
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
LoadBg();
ck = EditorGUILayout.TextField("2.设置关卡", ck);
// 输入数据
EditorGUILayout.Space();
GUILayout.Label("设置坐标,批量坐标保持一致的时候使用,不输入是按照鼠标位置刷新");
brushY = EditorGUILayout.TextField("3.固定Y的坐标(从左到右刷)", brushY);
brushX = EditorGUILayout.TextField("4.固定X的坐标(从上到下刷)", brushX);
brushDisY = EditorGUILayout.TextField("5.固定Y间距(从左到右刷)", brushDisY);
brushDisX = EditorGUILayout.TextField("6.固定X间距(从上到下刷)", brushDisX);
fixCount = EditorGUILayout.TextField("7.固定生成的个数", fixCount);
EditorGUILayout.Space(10);
zIndex = EditorGUILayout.TextField("8.物体显示层级,小的靠前显示", zIndex);
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
GUILayout.Label("9.选择橡皮擦 或者 选中一个场景物体", GUILayout.Width(250));
this.funcTypeIndex = EditorGUILayout.Popup(this.funcTypeIndex, funcTypeArry, popStyle, GUILayout.MaxWidth(100));
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal("box");
GUIStyle btnStyle = new GUIStyle("Button");
btnStyle.fontSize = 15;
btnStyle.padding = new RectOffset(10, 10, 10, 10);
if (GUILayout.Button("10.清除当前地图数据",btnStyle))
{
RemoveParentChild(parentMap);
_lastBrushY = -10000;// 防止后面刷新跟着这个位置
_lastBrushX = -10000;
_curFixCount = 0;
}
if (GUILayout.Button("11.导出json数据",btnStyle))
{
DataMgr.Instance.OutFile(parentMap,ck);
EditorUtility.DisplayDialog("导出数据成", "导出关卡数据成功"+ck, "确定");
}
if (GUILayout.Button("12.导入json数据",btnStyle))
{
DataMgr.Instance.ImportFile(parentMap,ck);