国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > 在Unity中定义统一的对象搜索接口

在Unity中定义统一的对象搜索接口

来源:程序员人生   发布时间:2014-12-23 09:04:31 阅读次数:2246次

我们常常要在Unity中以各种方式搜索对象。比如按名字搜索、按tag、layer或是查找名字为xxx开头的对象。

本文是介绍以1种统1的接口来搜索对象。


1、定义统1的搜索接口

/// <summary> /// 游戏对象搜索接口 /// </summary> public interface IGameObjectFinder { /// <summary> /// 搜索 /// </summary> /// <param name="root">搜索的开始位置/根节点</param> /// <param name="findResult">搜索寄存的结果</param> void Find(Transform root, List<Transform> findResult); }

2、定义1个使用上面搜索接口的方法

public class Finder{ /// <summary> /// 查找指定根节点下符合指定的查找器的Transform并保持到findResult中 /// </summary> /// <param name="root"></param> /// <param name="findResult"></param> /// <param name="finder"></param> public static void Find(Transform root, List<Transform> findResult, IGameObjectFinder finder) { if (root == null) { throw new Exception("root can not be null, it defines the starting point of the find path"); } if (findResult == null) { throw new Exception("findResult can not be null, it used to collect the find result"); } if (finder == null) { throw new Exception("finder can not be null, it defines how to find transform"); } finder.Find(root, findResult); } }

可以看到,2步骤只是简单调用1的接口进行搜索。但是1只是接口,有啥用?接着看。

3、实现1个查找某个组件的接口

/// <summary> /// 根据组件搜索 /// </summary> /// <typeparam name="T"></typeparam> public class GameObjectFinderByComponent<T> : IGameObjectFinder where T : Component { public void Find(Transform root, List<Transform> findResult) { foreach (var componentsInChild in root.GetComponentsInChildren<T>()) { findResult.Add(componentsInChild.transform); } } }

可以看到只要实现IGameObjectFinder就能够了。那末如果这时候候想调用,应当怎样调用呢?
比如想查找transform下面的刚体组件,然后保存到result里面。只要:
Finder.Find(transform, result, new GameObjectFinderByComponent<Rigidbody>());
甚么?看起来好像有点小题大作,直接用GetComponentsInChildren不就好吗?
先不急。我们继续看看其它查找需求。比如我想查找名字为xxx的对象。

4、实现1个迭代查找

首先,要查找指定节点下的某个名字的所有节点(不管深度多少),这个需要遍历。那末,我们可以先抽象出1个遍历的接口。
为了保证搜索的统1,那末我们还是从IGameObjectFinder中继承。
/// <summary> /// 迭代遍历搜索 /// </summary> public class GameObjectFinderByIteration : IGameObjectFinder { private IGameObjectFinderForIteration finderForIteration; public GameObjectFinderByIteration(IGameObjectFinderForIteration finderForIteration) { this.finderForIteration = finderForIteration; } public void Find(Transform root, List<Transform> findResult) { for (int i = 0, childCount = root.childCount; i < childCount; i++) { Transform t = root.GetChild(i); if (finderForIteration.isVaild(t)) { findResult.Add(t); } Find(t, findResult); } } }
这个代码的意思就是:我先把开始节点下面的每一个子节点通过另外一个接口IGameObjectFinderForIteration来判断是不是符合要求,是的话则加入结果列表。然后继续查找这个子结点下的其他子节点。(该搜索是不包括第1个开始节点的)
IGameObjectFinderForIteration的接口也很简单:
/// <summary> /// 迭代搜索判断 /// </summary> public interface IGameObjectFinderForIteration { /// <summary> /// 指定节点是不是合法 /// </summary> /// <param name="node"></param> /// <returns></returns> bool isVaild(Transform node); }
好的,这样就意味着我们要想查找某个根节点下的所有子节点(包括直接和间接的),只要实现1个IGameObjectFinderForIteration。那末OK。看看怎样按名字搜索。
/// <summary> /// 迭代遍历按名字搜索 /// </summary> public class FinderForIterationByName : IGameObjectFinderForIteration { protected readonly string NAME; public FinderForIterationByName(string name) { NAME = name; } public bool isVaild(Transform getChild) { return getChild.gameObject.name.Equals(NAME); } }

使用也很简单,加入想找transform节点下,名字为“abc”的对象,并保存在result列表里面。只要这样:
Finder.Find(transform, result, new GameObjectFinderByIteration(new FinderForIterationByName("abc")));

5、有甚么用?

很多人可能还不明白有甚么用,通过上面的抽象。你可以发现我们把搜索全部统1为:
Finder.Find(transform, result, objectFinderImpl);
1、代码是统1的,不管你的搜索条件有多复杂。便于代码管理、保护、拓展。
2、可配置,你可以简单通过数字关联不同的搜索器的实现,然后通过附带的参数传递给具体的搜索器,来实现通过配置文本采取不同的搜索器进行搜索。这个在你做新手引导的时候很好用,由于,策划可能有时候要把1个xxx手指放到甚么图标、甚么按钮那里。那你就能够把这些查找交给策划来配置了~
3、如果还没明白有甚么用,那就疏忽吧。总有1天你还会回来的。










生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生