![Unity3D游戏开发之定义统一的对象搜索接口.docx_第1页](http://file.renrendoc.com/FileRoot1/2020-1/16/121cdfbd-7d89-492c-80ec-de1b69dc5b28/121cdfbd-7d89-492c-80ec-de1b69dc5b281.gif)
![Unity3D游戏开发之定义统一的对象搜索接口.docx_第2页](http://file.renrendoc.com/FileRoot1/2020-1/16/121cdfbd-7d89-492c-80ec-de1b69dc5b28/121cdfbd-7d89-492c-80ec-de1b69dc5b282.gif)
![Unity3D游戏开发之定义统一的对象搜索接口.docx_第3页](http://file.renrendoc.com/FileRoot1/2020-1/16/121cdfbd-7d89-492c-80ec-de1b69dc5b28/121cdfbd-7d89-492c-80ec-de1b69dc5b283.gif)
![Unity3D游戏开发之定义统一的对象搜索接口.docx_第4页](http://file.renrendoc.com/FileRoot1/2020-1/16/121cdfbd-7d89-492c-80ec-de1b69dc5b28/121cdfbd-7d89-492c-80ec-de1b69dc5b284.gif)
![Unity3D游戏开发之定义统一的对象搜索接口.docx_第5页](http://file.renrendoc.com/FileRoot1/2020-1/16/121cdfbd-7d89-492c-80ec-de1b69dc5b28/121cdfbd-7d89-492c-80ec-de1b69dc5b285.gif)
全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Unity3D游戏开发之定义统一的对象搜索接口1、定义统一的搜索接口/ / 游戏对象搜索接口/ public interface IGameObjectFinder / / 搜索 / / 搜索的开始位置/根节点 / 搜索存放的结果 void Find(Transform root, List findResult);2、定义一个使用上面搜索接口的方法public class Finder / / 查找指定根节点下符合指定的查找器的Transform并保持到findResult中 / / / / public static void Find(Transform root, List 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、实现一个查找某个组件的接口/ / 根据组件搜索/ / public class GameObjectFinderByComponent : IGameObjectFinder where T : Component public void Find(Transform root, List findResult) foreach (var componentsInChild in root.GetComponentsInChildren() findResult.Add(componentsInChild.transform); 可以看到只要实现IGameObjectFinder就可以了。那么如果这时候想调用,应该怎么调用呢? 比如想查找transform下面的刚体组件,然后保存到result里面。只要:Finder.Find(transform, result, new GameObjectFinderByComponent();什么?看起来好像有点小题大作,直接用GetComponentsInChildren不就好吗?先不急。我们继续看看其它查找需求。比如我想查找名字为xxx的对象。4、实现一个迭代查找 首先,要查找指定节点下的某个名字的所有节点(不管深度多少),这个需要遍历。那么,我们可以先抽象出一个遍历的接口。为了保证搜索的统一,那么我们还是从IGameObjectFinder中继承。/ / 迭代遍历搜索/ public class GameObjectFinderByIteration : IGameObjectFinder private IGameObjectFinderForIteration finderForIteration; public GameObjectFinderByIteration(IGameObjectFinderForIteration finderForIteration) this.finderForIteration = finderForIteration; public void Find(Transform root, List 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来判断是否符合要求,是的话则加入结果列表。然后继续查找这个子结点下的其他子节点。(该搜索是不包括第一个开始节点的)IGameObjectFinderForIteration的接口也很简单:/ / 迭代搜索判断/ public interface IGameObjectFinderForIteration / / 指定节点是否合法 / / / bool isVaild(Transform node); 好的,这样就意味着我们要想查找某个根节点下的所有子节点(包括直接和间接的),只要实现一个IGameObjectFinderForIteration。那么OK。看看怎么按名字搜索。文章出处【狗刨学习网】/ / 迭代遍历按名字搜索 / public class FinderForIterationByName : IGameObjectFinderForIteration protected readonly string NAME; public FinderForIterationByName(string name) NAME = name; public bool isVaild(Transform getChild) return getChild.gameO.Equals(NAME); 使用也很简单,加入想找transform节点下,名字为“abc”的对象,并保存在result列表里面。只要这样:Finder.Find(transform, result, new GameObjectFinderByIteration(new FinderForIterationByName(abc);5、有什么用? 很多人可能还不明白有什么用,通过上面的抽象。你可以发现我们把搜索全部统一为:Finder.Find(transform, result, objectFinderImpl);1、代码是统一的,不管你的搜索条件有多复杂。便于代码管理、维护、拓展。2、可配置,你可以简单通过数字关联不同的搜
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 元旦老师发言晚会致辞范文
- 春节期间调研报告总结与反思
- 业主工地用水申请书范文
- 2024年长沙轨道交通职业学院单招职业技能测试题库一套
- 苏教版科学小学五年级上册 期末测试卷及完整答案【夺冠系列】
- 2024年辽宁省葫芦岛市单招职业适应性测试题库带答案
- 三年级上册英语教案-Unit 1 Lesson 5 How many 冀教版 (三起)
- 短方案拮抗剂方案
- 公司发展资金保障方案范文
- 公文工作提示函范文格式
- 2024年中考地理试卷附详细答案3篇
- 读书分享《曾国藩传》
- 期末模拟测试冲刺卷(试题)-2023-2024学年三年级下册数学人教版
- 2024安全生产月人人讲安全个个会应急畅通生命通道宣传课件
- 泰山文化概论智慧树知到期末考试答案2024年
- 巨量直播电商运营认证考试64题
- 特种设备安全总监、安全员任命
- 污水处理厂运维方案
- 温州市企业技术中心管理办法
- 不锈钢应力腐蚀原理及防治方法
- XXX公司年度安全教育培训计划方案
评论
0/150
提交评论