Unity之Handles绘制多点连接的线 -七

Unity之在Scene下创建多个物体,用线将所有物体连接

Unity之Handles绘制多点连接的线 -七_第1张图片


Unity之Handles绘制多点连接的线 -七_第2张图片




在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; //存储对象

}













你可能感兴趣的:(Unity之Editor)