标签:roo 结果 mono using har 关系 lan lis 递归遍历
简单总结一下深度优先算法和广度优先算法在Unity中最直观和最多见的使用。这里我所举的例子是应用到Unity中3D 人物的全部骨骼关键的遍历,推广开就是能够对全部物体的层级关系进行简单的遍历。。。算法
数据结构中的树的遍历在Unity中最直观的表现就是对某物体的全部子物体的遍历关系。数据结构
以下所示就是对Unity全部子物体层级的转换出的数据结构(树)3d
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 深度优先
/// </summary>
public class DepthFirst : MonoBehaviour
{
//用于存储所遍历到的物体
List<Transform> DepthFirstList = new List<Transform>();
void Start()
{
DepthFirst_0(transform);
//打印序列
foreach (var item in DepthFirstList)
{
Debug.Log(item);
}
}
void DepthFirst_0(Transform tran)
{
foreach (Transform item in tran)
{
DepthFirstList.Add(item);
//判断是否存在子物体
if (item.childCount != 0)
{
DepthFirst_0(item);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 获取全部关键
/// </summary>
public class BreathFirst : MonoBehaviour
{
List<Transform> BreadthFirstList = new List<Transform>();//层次遍历所获得的List
/// <summary>
/// 首先把物体的第一层子物体放入一个List,便于以后使用
/// </summary>
void Start()
{
List<Transform> joinsList = new List<Transform>();
foreach (Transform itemJoins in transform)
{
joinsList.Add(itemJoins);
}
BreadthFirst_1(joinsList);
foreach (var item in BreadthFirstList)
{
Debug.Log(item);
}
}
/// <summary>
/// 递归遍历每一层
/// </summary>
/// <param name="joins"></param>
void BreadthFirst_1(List<Transform> joins)
{
List<Transform> joinsChildListss = new List<Transform>();
foreach (Transform item in joins)
{
BreadthFirstList.Add(item);//将每一层依次放入列表中
if (item.childCount != 0)//判断每一次的序列项是否存在子物体,存在就将全部子物体放入一个新的List
{
foreach (Transform itemss in item)
{
joinsChildListss.Add(itemss);
}
}
}
if (joinsChildListss != null && joinsChildListss.Count != 0)//判断是不是最后一层
BreadthFirst_1(joinsChildListss);
}
}
打印结果以下所示:using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 获取全部关键
/// </summary>
public class ListOfJoints : MonoBehaviour
{
public Transform HumModel;
int numCount = 0;
List<Transform> BreadthFirstList = new List<Transform>();
Dictionary<Transform, int> BreathFirstDic = new Dictionary<Transform, int>();
List<Transform> DepthFirstList = new List<Transform>();
IEnumerator Start()
{
yield return new WaitForEndOfFrame();
DepthFirst_0(HumModel);
BreadthFirst_0(HumModel);
yield return new WaitForEndOfFrame();
foreach (var item in DepthFirstList)
{
if (BreathFirstDic.ContainsKey(item))
{
string str = "";
for (int i = 0; i < BreathFirstDic[item]; i++)
{
str += " ";
}
Debug.Log(str + item);
}
}
}
void BreadthFirst_0(Transform tran)
{
List<Transform> joinsList = new List<Transform>();
foreach (Transform itemJoins in HumModel)
{
joinsList.Add(itemJoins);
}
BreadthFirst_1(joinsList);
}
void BreadthFirst_1(List<Transform> joins)
{
numCount++;//层数
List<Transform> joinsChildListss = new List<Transform>();
foreach (Transform item in joins)//打印子物体
{
BreadthFirstList.Add(item);
BreathFirstDic.Add(item, numCount);
if (item.childCount != 0)
{
foreach (Transform itemss in item)
{
joinsChildListss.Add(itemss);
}
}
}
if (joinsChildListss != null && joinsChildListss.Count != 0)
BreadthFirst_1(joinsChildListss);
}
void DepthFirst_0(Transform tran)
{
foreach (Transform item in tran)
{
DepthFirstList.Add(item);
if (item.childCount != 0)
{
DepthFirst_0(item);
}
}
}
}
标签:roo 结果 mono using har 关系 lan lis 递归遍历
原文地址:https://www.cnblogs.com/gangtie/p/13937403.html