Unity项目内,全局搜索预制体
cs
var title = "";
var progress = 0;
var files = SearchFiles(Application.dataPath, new []{"*.prefab"});
EditorUtility.DisplayProgressBar(title, assetPath, ++progress / filesCount);
EditorUtility.ClearProgressBar();
想全局遍历(旧)-新
cs
delegate bool TransformCallback(Transform tran,string fullPath);
/// <summary>
/// 遍历子节点用
/// </summary>
/// <param name="tran"></param>
/// <param name="cb"></param>
/// <returns>若return true,则中断</returns>
static bool LoopChild(Transform tran, string fullPath,TransformCallback cb)
{
bool ret = cb(tran,fullPath);//先返回父节点,再返回子节点
//if (ret == true) return true;
for(int i=0;i<tran.childCount;i++)
{
var child = tran.GetChild(i);
ret = LoopChild(child,fullPath+"/" + child.name,cb);
//if (ret == true) return true;
}
return false;
}
//使用方法:
LoopChild(prefab.transform,"",(tran,fullPath) =>
{
};
更专业的名字,Traverse,或者也是递归
//有点暴露年龄了
递归应该是90年代的说法,上世纪90年代,现在还有说"递归"的么??
学校里说递归,出了社会,发现都只是Traverse
就you know?,是多老的年代的电影说法,结果现在是Literally
又好像前几年经常听到**拓扑结构,**到底是什么东西,我拓你老母a
//事实证明,名字不重要,英文也不重要,不要被名字影响到才重要
cs
void Traverse(Transform parent, int i, ref StringBuilder builder)
{
builder.Append("\n-");
builder.Append(GetIndentStringForValue(++i * indentValue));
builder.Append('-');
builder.Append(' ');
builder.Append(parent.name);
foreach (Transform child in parent)
{
Traverse(child, i, ref builder);
}
}
//伪代码
StringBuilder sb;
Traverse(child, 1, ref sb);
另一个,正在做的BTree的写法
直接就用上linq 了
cs
/// <summary>
/// "外包"了一个遍历方法(而且是树递归);
/// 另外,找到一个Traverse C#库:https://github.com/madelson/Traverse;可以很简单处理树结构(基于LingQ)
/// </summary>
/// <param name="node"></param>
/// <param name="visiter"></param>
public static void Traverse(Node node, System.Action<Node> visiter) {
if (node) {
visiter.Invoke(node);
var children = GetChildren(node);
children.ForEach((n) => Traverse(n, visiter));
}
}
/// <summary>
/// 这是运行时才会调用的逻辑: Clone()
/// </summary>
/// <returns></returns>
public BehaviourTree Clone() {
BehaviourTree tree = Instantiate(this);
tree.rootNode = tree.rootNode.Clone();
tree.nodes = new List<Node>();
//这个遍历又会导致 引入很多null
//(假如多次修改类的名字,等,不断用这个BTree Canvas,又不断修改的情况。。。)
Traverse(tree.rootNode, (n) => {
if (n == null)
{
Debug.LogError("child==null??");
}
tree.nodes.Add(n);
});
return tree;
}