Unity之在Scene下创建多个物体,用线将所有物体连接
在Editor文件夹下创建脚本 HandlerTest
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(Arraw))]
public class HandlerTest : Editor {
Vector3[] positions;
void OnSceneGUI()
{
Arraw arraw = (Arraw)target;
if (arraw.polyLineObjs.Length > 0) //如果存储的对象个数不为 0
{
positions = new Vector3[arraw.polyLineObjs.Length];
for (int i = 0; i < arraw.polyLineObjs.Length; i++)
{
if (arraw.polyLineObjs[i]) //不为空
{
positions[i] = arraw.polyLineObjs[i].transform.position; //获取坐标位置
}
else
{
positions[i] = Vector3.zero; //如果为空 赋一个值
}
}
Handles.DrawPolyLine(positions); //画出所有的连线
}
if (GUI.changed)
{
EditorUtility.SetDirty(arraw);
}
}
}
Arraw脚本如下
using UnityEngine;
using System.Collections;
public class Arraw : MonoBehaviour {
public GameObject[] polyLineObjs; //存储对象
}