美文网首页
从零开始的RPG制作4.1(辅助线的统一管理)

从零开始的RPG制作4.1(辅助线的统一管理)

作者: 小蜻蜓队长 | 来源:发表于2019-04-29 14:44 被阅读0次

在制作中,我们往往需要用到许许多多的辅助线设置,往往分部在不同的脚本中,在这里我们不希望这样,我们希望能够统一进行辅助线的管理,于是我们设计了一个辅助线设置相关类。
DrawGizmosManager:

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using UnityEngine;
using Object = UnityEngine.Object;
using Debug = UnityEngine.Debug;

[System.Serializable]
public class IDrawGizmos {
    public enum DrawGizmoType {
        Null,
        Cube,
        Sphere,
        Line,
        Ray,
    }
    private DrawGizmoType dgType = DrawGizmoType.Null;
    public DrawGizmoType DgType {
        get {
            return dgType;
        }
    }
    [Header("===== 以下是自身数据相关 =====")]
    Vector3 cubeSize;//方形的长度。
    public Vector3 CubeSize {
        get {
            return cubeSize;
        }
    }

    float radius;//圆形的半径。
    public float Radius {
        get {
            return radius;
        }
    }

    Ray ray;//绘制射线
    public Ray Ray {
        get {
            return ray;
        }
    }

    Vector3 startPostion;//描绘起始点。
    public Vector3 StartPostion {
        get {
            return startPostion;
        }
    }

    Vector3 endPostion;//描绘的终点。
    public Vector3 EndPostion {
        get {
            return endPostion;
        }
    }

    Color drawColor;
    public Color DrawColor {
        get {
            return drawColor;
        }
    }

    int? id;//自身id,用于区分重复创建对象。
    public int? Id {
        get {
            return id;
        }
    }

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

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

    static public void deleteID (int? id) {
        if (IDrawGizmos.manager != null)
            if (IDrawGizmos.manager.idExists(id) != null) {
                IDrawGizmos.manager.deleteGizmo(id);
            }
    }

    static DrawGizmosManager manager;
    static DrawGizmosManager getDrawGizmosManager () {
        if (IDrawGizmos.manager == null) {
            DrawGizmosManager managerInScene = Object.FindObjectOfType<DrawGizmosManager>();
            if (managerInScene != null) {
                IDrawGizmos.manager = managerInScene;
            } else {
                GameObject managerObject = new GameObject();
                managerObject.name = "DrawGizmoManager";
                IDrawGizmos.manager = managerObject.AddComponent<DrawGizmosManager>();
            }
        }
        return IDrawGizmos.manager;
    }

    #region 启用关键段落
    /// <summary>
    /// 绘制一个正方形
    /// </summary>
    /// <param name="postion">原点</param>
    /// <param name="size">大小</param>
    /// <param name="color">颜色</param>
    /// <param name="kid">画线ID</param>
    /// <returns></returns>
    static public IDrawGizmos drawCube (Vector3 postion,Vector3 size,Color color,int? kid = null) {
        editor();
        IDrawGizmos.manager = getDrawGizmosManager();
        IDrawGizmos haveIG = null;
        if (kid != null) {//如果画线ID不等于null,就去列表中寻找这个对象,修改内部数值
            haveIG = IDrawGizmos.manager.idExists(kid);
            if (haveIG == null) {//如果不存在这个ID,继续创建一个新的画线对象
                IDrawGizmos idrawgizmos = new IDrawGizmos(DrawGizmoType.Cube,postion,size,color,kid);
                IDrawGizmos.manager.IDrawGizmoListAdd(idrawgizmos);
                return idrawgizmos;
            } else {//如果存在这个ID 修改内部数值。
                if (haveIG.dgType != DrawGizmoType.Cube) {//如果编号相同,类型不同
                    Debug.LogError("这个ID已经被其他类型的辅助线占据,请更换一个ID");
                }
                haveIG.startPostion = postion;
                haveIG.cubeSize = size;
                haveIG.drawColor = color;
                return haveIG;
            }
        } else {//如果画线ID等于null,就不必查找列表,重新创建一个。
            IDrawGizmos idrawgizmos = new IDrawGizmos(DrawGizmoType.Cube,postion,size,color,kid);
            IDrawGizmos.manager.IDrawGizmoListAdd(idrawgizmos);
            return idrawgizmos;
        }
    }

    /// <summary>
    /// 绘制一个圆形
    /// </summary>
    /// <param name="postion">原点</param>
    /// <param name="radius">半径</param>
    /// <param name="color">颜色</param>
    /// <param name="kid">画线id</param>
    /// <returns></returns>
    static public IDrawGizmos drawSphere (Vector3 postion,float radius,Color color,int? kid = null) {
        editor();
        IDrawGizmos.manager = getDrawGizmosManager();
        IDrawGizmos haveIG = null;
        if (kid != null) {//如果画线ID不等于null,就去列表中寻找这个对象,修改内部数值
            haveIG = IDrawGizmos.manager.idExists(kid);
            if (haveIG == null) {//如果不存在这个ID,继续创建一个新的画线对象
                IDrawGizmos idrawgizmos = new IDrawGizmos(DrawGizmoType.Sphere,postion,radius,color,kid);
                IDrawGizmos.manager.IDrawGizmoListAdd(idrawgizmos);
                return idrawgizmos;
            } else {//如果存在这个ID 修改内部数值。
                if (haveIG.dgType != DrawGizmoType.Sphere) {//如果编号相同,类型不同
                    Debug.LogError("这个ID已经被其他类型的辅助线占据,请更换一个ID");
                }
                haveIG.startPostion = postion;
                haveIG.radius = radius;
                haveIG.drawColor = color;
                return haveIG;
            }
        } else {
            IDrawGizmos idrawgizmos = new IDrawGizmos(DrawGizmoType.Sphere,postion,radius,color,kid);
            IDrawGizmos.manager.IDrawGizmoListAdd(idrawgizmos);
            return idrawgizmos;
        }
    }
    /// <summary>
    /// 绘制一条线段
    /// </summary>
    /// <param name="postion">起始点</param>
    /// <param name="endPostion">终点</param>
    /// <param name="color">颜色</param>
    /// <param name="kid">编号</param>
    /// <returns></returns>
    static public IDrawGizmos drawLine (Vector3 postion,Vector3 endPostion,Color color,int? kid = null) {
        editor();
        IDrawGizmos.manager = getDrawGizmosManager();
        IDrawGizmos haveIG = null;
        if (kid != null) {//如果画线ID不等于null,就去列表中寻找这个对象,修改内部数值
            haveIG = IDrawGizmos.manager.idExists(kid);
            if (haveIG == null) {//如果不存在这个ID,继续创建一个新的画线对象
                IDrawGizmos idrawgizmos = new IDrawGizmos(DrawGizmoType.Line,postion,endPostion,color,kid);
                IDrawGizmos.manager.IDrawGizmoListAdd(idrawgizmos);
                return idrawgizmos;
            } else {//如果存在这个ID 修改内部数值。
                if (haveIG.dgType != DrawGizmoType.Line) {//如果编号相同,类型不同
                    Debug.LogError("这个ID已经被其他类型的辅助线占据,请更换一个ID");
                }
                haveIG.startPostion = postion;
                haveIG.endPostion = endPostion;
                haveIG.drawColor = color;
                return haveIG;
            }
        } else {
            IDrawGizmos idrawgizmos = new IDrawGizmos(DrawGizmoType.Line,postion,endPostion,color,kid);
            IDrawGizmos.manager.IDrawGizmoListAdd(idrawgizmos);
            return idrawgizmos;
        }
    }

    static public IDrawGizmos drawRay (Ray ray,Color color,int? kid = null) {
        editor();
        IDrawGizmos.manager = getDrawGizmosManager();
        IDrawGizmos haveIG = null;
        if (kid != null) {//如果画线ID不等于null,就去列表中寻找这个对象,修改内部数值
            haveIG = IDrawGizmos.manager.idExists(kid);
            if (haveIG == null) {//如果不存在这个ID,继续创建一个新的画线对象
                IDrawGizmos idrawgizmos = new IDrawGizmos(DrawGizmoType.Ray,ray,color,kid);
                IDrawGizmos.manager.IDrawGizmoListAdd(idrawgizmos);
                return idrawgizmos;
            } else {//如果存在这个ID 修改内部数值。
                if (haveIG.dgType != DrawGizmoType.Ray) {//如果编号相同,类型不同
                    Debug.LogError("这个ID已经被其他类型的辅助线占据,请更换一个ID");
                }
                haveIG.ray = ray;
                haveIG.drawColor = color;
                return haveIG;
            }
        } else {
            IDrawGizmos idrawgizmos = new IDrawGizmos(DrawGizmoType.Ray,ray,color,kid);
            IDrawGizmos.manager.IDrawGizmoListAdd(idrawgizmos);
            return idrawgizmos;
        }
    }
    #endregion

    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;//通过反射获取调用者的类名。
    }

    IDrawGizmos (DrawGizmoType dgtype,Vector3 postion,Vector3 sizeOrEnd,Color color,int? id)
        : this(dgtype,color,id) {
        this.startPostion = postion;
        if (dgtype == DrawGizmoType.Cube)
            this.cubeSize = sizeOrEnd;
        else if (dgtype == DrawGizmoType.Line)
            this.endPostion = sizeOrEnd;

    }

    IDrawGizmos (DrawGizmoType dgtype,Vector3 postion,float radius,Color color,int? id)
        : this(dgtype,color,id) {
        this.startPostion = postion;
        this.radius = radius;
    }

    IDrawGizmos (DrawGizmoType dgtype,Ray ray,Color color,int? id)
        : this(dgtype,color,id) {
        this.ray = ray;
    }

    private static void editor () {
        if (Application.platform != RuntimePlatform.WindowsEditor)
            return;
    }
}

public class DrawGizmosManager:MonoBehaviour {

    public List<IDrawGizmos> IDrawGizmoList = new List<IDrawGizmos>();
    public void IDrawGizmoListAdd (IDrawGizmos idg) {
        IDrawGizmoList.Add(idg);
    }

    public void deleteGizmo (int? id) {
        for (int i = 0;i < IDrawGizmoList.Count;i++) {
            if (IDrawGizmoList[i].Id == id) {
                IDrawGizmoList.RemoveAt(i);
                return;
            }
        }
    }

    public IDrawGizmos idExists (int? id) {//检验ID是否存在
        if (IDrawGizmoList != null) {

            foreach (var item in IDrawGizmoList) {
                if (id == item.Id) {
                    return item;
                }
            }
        }
        return null;
    }

    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:
                    onDrawLine(ig.StartPostion,ig.EndPostion,ig.DrawColor);
                    break;
                case IDrawGizmos.DrawGizmoType.Ray:
                    onDrawRay(ig.Ray,ig.DrawColor);
                    break;
            }
        }
    }

    private void onDrawCube (Vector3 potion,Vector3 size,Color color) {
        Gizmos.color = color;
        Gizmos.DrawCube(potion,size);
    }

    private void onDrawSphere (Vector3 potion,float radius,Color color) {
        Gizmos.color = color;
        Gizmos.DrawWireSphere(potion,radius);
    }

    private void onDrawLine (Vector3 from,Vector3 to,Color color) {
        Gizmos.color = color;
        Gizmos.DrawLine(from,to);
    }

    private void onDrawRay (Ray ray,Color color) {
        Gizmos.color = color;
        Gizmos.DrawRay(ray);
    }


    // Use this for initialization
    /* public Transform center;
     public Transform CurveStartPoint_Object;
     public Transform CurveEndPoint_Object;
     Vector3 CurveStartPoint;
     Vector3 CurveEndPoint;
     public List<Vector3> CurvePoints;
     void Start() {


     }

     void Update() {

     }*/



    /*private void OnDrawGizmos() {
        CurveStartPoint = CurveStartPoint_Object.position;
        CurveEndPoint = CurveEndPoint_Object.position;
        CurvePoints.Add(CurveStartPoint);
        for (int i = 0; i < 20; i++) {

            var t = i / (20 - 1.0f);
            var position = (1.0f - t) * (1.0f - t) * CurveStartPoint + 2.0f * (1.0f - t) * t * transform.position + t * t * CurveEndPoint;

            CurvePoints.Add(position);
            Gizmos.DrawCube(position, Vector3.one*0.1f);
        }
        CurvePoints.Add(CurveEndPoint);
        Gizmos.color = Color.red;
        Gizmos.DrawCube(CurveStartPoint, Vector3.one * 0.3f);
        Gizmos.DrawCube(CurveEndPoint, Vector3.one * 0.3f);
    }*/

}

完成之后,在我们的scene视图下就可以看到辅助线的效果了。为了看清效果,我们将小姐姐暂时隐藏起来。


辅助线

上一节
下一节

相关文章

网友评论

      本文标题:从零开始的RPG制作4.1(辅助线的统一管理)

      本文链接:https://www.haomeiwen.com/subject/vceynqtx.html