从零开始的RPG制作4.2(辅助线和编辑器拓展)

2019-04-29  本文已影响0人  小蜻蜓队长

在上一节中我们提到了辅助线统一管理,但是光是这样是不够,我们还希望能够在编辑器界面实时的监控,比如开关辅助线,查看辅助线位于那个脚本,所以,现在我们来做一下编辑器的设置。
首先,我们在原先的DrawGizmosManager脚本中进行一些修改。
IDrawGizmos类中的修改

[System.Serializable]//将类序列化。
public class IDrawGizmos 


    [SerializeField]
    bool describe = true;//是否需要描绘
    public bool Describe {
        get { return describe; }
        set { describe = value; }
    }

    string caller;//获得这个对象的父节点。
    public string Caller{
        get { return caller; }
    }

    IDrawGizmos(DrawGizmoType dgtype, Color color,int?id) {//每个构造函数都需要的部分
        this.dgType = dgtype;
        this.drawColor = color;
        this.id = id;
        StackTrace trace = new StackTrace();
        MethodBase methodName = trace.GetFrame(3).GetMethod();
        caller = methodName.ReflectedType.FullName;//通过反射获取调用者的类名。
    }

在DrawGizmosManager类中添加,关闭辅助线的操作。

    private void OnDrawGizmos() {
        for (int i = 0; i < IDrawGizmoList.Count; i++) {
            IDrawGizmos ig = IDrawGizmoList[i];
            if (!ig.Describe)
                continue;
            switch (ig.DgType) {
                case IDrawGizmos.DrawGizmoType.Cube:
                    onDrawCube(ig.StartPostion, ig.CubeSize, ig.DrawColor);
                    break;
                case IDrawGizmos.DrawGizmoType.Sphere:
                    onDrawSphere(ig.StartPostion, ig.Radius, ig.DrawColor);
                    break;
                case IDrawGizmos.DrawGizmoType.Line:
                    //待定
                    break;
            }
        }
    }

接着我们创建一个Editor的文件夹,在文件夹内部新建一个名为DrawGizmosEditor的类。
DrawGizmosEditor中的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;

[CanEditMultipleObjects]
[CustomEditor(typeof(DrawGizmosManager))]
public class TestEditor:Editor {


    protected DrawGizmosManager dgm;
    MonoScript script;
    private ReorderableList list;
    private void OnEnable() {
        dgm = (DrawGizmosManager)target;
        //script = MonoScript.FromMonoBehaviour(dgm);
        list = new ReorderableList(serializedObject, serializedObject.FindProperty("IDrawGizmoList"), true, true, false, false);

        list.drawElementCallback = drawElementCallback;
        list.drawHeaderCallback = HeaderCallbackDelegate;

    }

    public override void OnInspectorGUI() {
        serializedObject.Update();
        list.DoLayoutList();
        serializedObject.ApplyModifiedProperties();
    }

    int ColumnNumber = 3;
    void HeaderCallbackDelegate(Rect rect) {
        Rect R_1 = new Rect(rect.x + 10, rect.y, (rect.width - 20) / ColumnNumber - 23, EditorGUIUtility.singleLineHeight);
        EditorGUI.LabelField(R_1, "开关辅助线");

        Rect R_2 = new Rect(rect.x + (rect.width - 20) / ColumnNumber + 15, rect.y, (rect.width - 20) / ColumnNumber, EditorGUIUtility.singleLineHeight);
        EditorGUI.LabelField(R_2, "编号");

        Rect R_3 = new Rect(rect.x + ((rect.width - 20) / ColumnNumber) * 2 + 15, rect.y, ((rect.width - 30) / ColumnNumber) + 11, EditorGUIUtility.singleLineHeight);
        EditorGUI.LabelField(R_3, "调用者");

    }

    void drawElementCallback(Rect rect, int index, bool isActive, bool isFocused) {
        GUIStyle a = new GUIStyle();
        a.fontStyle = FontStyle.Normal;

        var element = dgm.IDrawGizmoList[index];
        rect.y += 2;
        element.Describe = EditorGUI.Toggle(new Rect(rect.x, rect.y, 20, EditorGUIUtility.singleLineHeight), element.Describe);

        Rect R_1 = new Rect(rect.x + (rect.width - 20) / ColumnNumber + 15, rect.y, (rect.width - 20) / ColumnNumber - 23, EditorGUIUtility.singleLineHeight);
        EditorGUI.TextField(R_1, element.Id.ToString(), a);


        Rect R_2 = new Rect(rect.x + ((rect.width - 20) / ColumnNumber) * 2 + 15, rect.y, (rect.width - 20) / ColumnNumber, EditorGUIUtility.singleLineHeight);
        EditorGUI.TextField(R_2, element.Caller);

    }
}

当一切准备就绪,我们就可以看到这样的界面 效果

之后需要什么内容增加就在这基础上修改即可。
上一节
下一节

上一篇下一篇

猜你喜欢

热点阅读