Unity用Gizmos画线和图

Unity用Gizmos画线和图_第1张图片

Camera上挂runner.cs脚本,创建几个GameObject进行点的定位,赋值给runner的ts数组

runner中实现OnDrawGizmos方法,在其中用Gizmos进行绘制

Unity用Gizmos画线和图_第2张图片

//runner.cs脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class runner : MonoBehaviour
{
    public Transform[] ts;

    void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        if (null != ts && ts.Length > 1)
        {
            for (int i = 0; i < ts.Length-1; i++)
            {
                if (null != ts[i] && null != ts[i + 1])
                {
                    Gizmos.DrawLine(ts[i].position, ts[i + 1].position);
                    Gizmos.DrawIcon(ts[i].position, "icons/face");
                }
            }
            
        }

    }
}

注意,DrawIcon方法用到的图片,必须放在Assets/Gizmos目录中,如果有子目录,比如Assets/Gizmos/icons/,那么参数要写相对路径icon/xxx,比如上面的icon/face

你可能感兴趣的:(unity3D,Unity3D)