欢迎来到天天文库
浏览记录
ID:39891089
大小:16.07 KB
页数:5页
时间:2019-07-14
《Unity3D游戏开发之定义统一的对象搜索接口》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、Unity3D游戏开发之定义统一的对象搜索接口1、定义统一的搜索接口//////游戏对象搜索接口///publicinterfaceIGameObjectFinder{ /// ///搜索 /// ///搜索的开始位置/根节点 ///搜索存放的结果 voidFind(Transformroot,ListfindR
2、esult);}2、定义一个使用上面搜索接口的方法publicclassFinder{ /// ///查找指定根节点下符合指定的查找器的Transform并保持到findResult中 /// /// /// /// publicstaticvoidFind(Transformroot,List3、>findResult,IGameObjectFinderfinder) { if(root==null) { thrownewException("rootcannotbenull,itdefinesthestartingpointofthefindpath"); } if(findResult==null) { thrownewException("findResultcannotbenull,itusedtocollectthefindresult"4、); } if(finder==null) { thrownewException("findercannotbenull,itdefineshowtofindtransform"); } finder.Find(root,findResult); }}可以看到,2步骤只是简单调用1的接口进行搜索。但是1只是接口,有啥用?接着看。文章出处【狗刨学习网】3、实现一个查找某个组件的接口//////根据组件搜索//////5、amname="T">publicclassGameObjectFinderByComponent:IGameObjectFinderwhereT:Component{ publicvoidFind(Transformroot,ListfindResult) { foreach(varcomponentsInChildinroot.GetComponentsInChildren()) { findResult.Add(componentsI6、nChild.transform); } }} 可以看到只要实现IGameObjectFinder就可以了。那么如果这时候想调用,应该怎么调用呢?比如想查找transform下面的刚体组件,然后保存到result里面。只要:Finder.Find(transform,result,newGameObjectFinderByComponent());什么?看起来好像有点小题大作,直接用GetComponentsInChildren不就好吗?先不急。我们继续看看其它查找需求。比如我想查找名字为xxx7、的对象。4、实现一个迭代查找首先,要查找指定节点下的某个名字的所有节点(不管深度多少),这个需要遍历。那么,我们可以先抽象出一个遍历的接口。为了保证搜索的统一,那么我们还是从IGameObjectFinder中继承。//////迭代遍历搜索///publicclassGameObjectFinderByIteration:IGameObjectFinder{ privateIGameObjectFinderForIterationfinderForIteration; publicGameOb8、jectFinderByIteration(IGameObjectFinderForIterationfinderForIteration) { this.finderForIteration=finderF
3、>findResult,IGameObjectFinderfinder) { if(root==null) { thrownewException("rootcannotbenull,itdefinesthestartingpointofthefindpath"); } if(findResult==null) { thrownewException("findResultcannotbenull,itusedtocollectthefindresult"
4、); } if(finder==null) { thrownewException("findercannotbenull,itdefineshowtofindtransform"); } finder.Find(root,findResult); }}可以看到,2步骤只是简单调用1的接口进行搜索。但是1只是接口,有啥用?接着看。文章出处【狗刨学习网】3、实现一个查找某个组件的接口//////根据组件搜索//////5、amname="T">publicclassGameObjectFinderByComponent:IGameObjectFinderwhereT:Component{ publicvoidFind(Transformroot,ListfindResult) { foreach(varcomponentsInChildinroot.GetComponentsInChildren()) { findResult.Add(componentsI6、nChild.transform); } }} 可以看到只要实现IGameObjectFinder就可以了。那么如果这时候想调用,应该怎么调用呢?比如想查找transform下面的刚体组件,然后保存到result里面。只要:Finder.Find(transform,result,newGameObjectFinderByComponent());什么?看起来好像有点小题大作,直接用GetComponentsInChildren不就好吗?先不急。我们继续看看其它查找需求。比如我想查找名字为xxx7、的对象。4、实现一个迭代查找首先,要查找指定节点下的某个名字的所有节点(不管深度多少),这个需要遍历。那么,我们可以先抽象出一个遍历的接口。为了保证搜索的统一,那么我们还是从IGameObjectFinder中继承。//////迭代遍历搜索///publicclassGameObjectFinderByIteration:IGameObjectFinder{ privateIGameObjectFinderForIterationfinderForIteration; publicGameOb8、jectFinderByIteration(IGameObjectFinderForIterationfinderForIteration) { this.finderForIteration=finderF
5、amname="T">publicclassGameObjectFinderByComponent:IGameObjectFinderwhereT:Component{ publicvoidFind(Transformroot,ListfindResult) { foreach(varcomponentsInChildinroot.GetComponentsInChildren()) { findResult.Add(componentsI
6、nChild.transform); } }} 可以看到只要实现IGameObjectFinder就可以了。那么如果这时候想调用,应该怎么调用呢?比如想查找transform下面的刚体组件,然后保存到result里面。只要:Finder.Find(transform,result,newGameObjectFinderByComponent());什么?看起来好像有点小题大作,直接用GetComponentsInChildren不就好吗?先不急。我们继续看看其它查找需求。比如我想查找名字为xxx
7、的对象。4、实现一个迭代查找首先,要查找指定节点下的某个名字的所有节点(不管深度多少),这个需要遍历。那么,我们可以先抽象出一个遍历的接口。为了保证搜索的统一,那么我们还是从IGameObjectFinder中继承。//////迭代遍历搜索///publicclassGameObjectFinderByIteration:IGameObjectFinder{ privateIGameObjectFinderForIterationfinderForIteration; publicGameOb
8、jectFinderByIteration(IGameObjectFinderForIterationfinderForIteration) { this.finderForIteration=finderF
此文档下载收益归作者所有