游戏开发的过程中,我们经常需要让某些物体按照固定的路线来移动,这时候就需要提供给策划同学们一个路径编辑器来让他们充分发挥"想象力"。。。
先来看下最终效果:
下面来简单说下实现过程
制作路径点
首先制作路径点,每一个路径点要记录自己都连接到哪些点,我们用一个数组来记录,所以路径点的脚本应该是这样:
public class MapWayPoint : MonoBehaviour {
public List<GameObject> pointList;
}
绘制路径
接下来就是在这些路径点之间绘制出连接的带箭头的线:
创建一个脚本用来专门绘制这些线:MapDraw
这个脚本要引入命名空间:
#if UNITY_EDITOR
using UnityEditor;
#endif
这里要判断一下平台,因为在其它平台上是没有这个命名空间的
我们把绘制逻辑放在MonoBehaviour的OnDrawGizmos中:
public void OnDrawGizmos()
{
#if UNITY_EDITOR
Gizmos.color = Color.blue;
Handles.color = Color.blue;
//获取场景中全部道具
Object[] objects = Object.FindObjectsOfType(typeof(GameObject));
Dictionary<string, List<string>> post = new Dictionary<string, List<string>>();
foreach (GameObject sceneObject in objects){
if(sceneObject.name == "WayPoint"){
foreach (Transform child in sceneObject.transform)
{
MapWayPoint editor = child.GetComponent<MapWayPoint>();
if(editor != null){
for(int i = 0 ; i < editor.pointList.Count ; i ++){
if(editor.pointList[i] == null){
continue;
}
editor.transform.LookAt(editor.pointList[i].transform);
DrawLine(editor.transform,editor.pointList[i].transform,Vector3.Distance(editor.transform.position,editor.pointList[i].transform.position));
}
}
}
}
}
#endif
}
这里主要就是在场景中找到WayPoint对象,因为我们规定所有的路径点都要放到这个对象里边,所以我们遍历这个对象的所有子物体就找到了全部的路径点。然后遍历每个路径点所连接的所有目标点并调用绘制方法。
绘制方法:
public void DrawLine(Transform t,Transform t2,float dis){
#if UNITY_EDITOR
Handles.ArrowCap(0, t.position + (dis-Mathf.Mi