首页 > 编程知识 正文

unity 画线,unity两点之间画线

时间:2023-05-06 20:34:28 阅读:275958 作者:4454

/// <summary>/// 脚本位置:Path身上/// </summary>using UnityEngine;using System.Collections;using System.Collections.Generic;public class PathTrail : MonoBehaviour { // 是否显示场景信息 public bool ShowTrail = false; // 一个列表用于保存多条路径 private GameObject[] Paths; // 一个字典用于保存多条路径点 private Dictionary<GameObject, List<Transform>> PathNodes = new Dictionary<GameObject, List<Transform>>(); void Awake() { BuildPath(); } // 创建路径 - 可以在编辑状态下执行BuildPath [ContextMenu("BuildPath")] public void BuildPath() { PathNodes.Clear(); // 找到path1和path2 Paths = GameObject.FindGameObjectsWithTag("path"); for (int i = 0; i <Paths.Length; i++) {// 调用FindPathNode方法,查找path1中的所有路点 PathNodes.Add(Paths[i], FindPathNode(Paths[i].transform)); } } // 查找路点 List<Transform> FindPathNode(Transform path) { int childCount = path.childCount; List<Transform> pathNode = new List<Transform>(); Transform node; for (int i = 0; i < childCount; i++) { node = path.GetChild(i); node.name = path.name + "_Node" + i; pathNode.Add(node); } return pathNode; } // 绘制路径 // 系统函数,自动调用 void OnDrawGizmos() { if (!ShowTrail || Paths == null || Paths.Length == 0) {return; } Gizmos.color = Color.black; foreach (var pair in PathNodes) { // pair的返回值是KeyValuePair<GameObject, List<Transform>>键值对类型// value指的就是list,count就是数组的长度,当路点数超过1个的时候才会画线 if (pair.Value.Count <= 1) { continue; } for (int i = 1; i < pair.Value.Count; i++) {// 画线 Gizmos.DrawLine(pair.Value[i-1].position, pair.Value[i].position); } } }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。