[Unity 3d] LINQ To GameObject For Unity- GitHub

GitHub 上的工程多如繁星,有些好的仓库,但凡不经意间错过了就很难找回,故稍作采撷,希望能帮助到有心人。
本文集以一个小的功能点为单位行文,也便于拾取罢!

简介:

笔者今天推荐的仓库叫 LINQ-to-GameObject-for-Unity
LINQ to GameObject is GameObject extensions for Unity that allows traverse hierarchy and append GameObject. The design aims both to get the power of LINQ and performance of iteration. - LINQ to GameObject是 Unity 的 GameObject 扩展,允许遍历层次结构并对游戏对象进行各种骚操作的插件。将LINQ的强大功能和高性能迭代器引入实际开发中。

功能:

该插件 具有众多实用 API(示例) :

  • Append child(Add, AddFirst, AddBeforeSelf, AddAfterSelf) - 支持附加子节点游戏对象
  • Append multiple objects(AddRange, AddFirstRange,
  • AddBeforeSelfRange, AddAfterSelfRange) - 支持附加多个游戏对象
  • Destroy object(Destroy). - 支持Linq语法中销毁游戏对象
  • All traverse methods can find inactive object. - 支持遍历游戏对象
  • LINQ to GameObject is optimized heavily. - 大幅优化枚举过程中的GC问题

演示:

以下图片演示API访问时达到的效果(或者叫设计理念和约定)


3600713-1558530a6093169f.png
image.png

遍历函数将返回 IEnumerable<GameObject> 并延迟执行,例如: (务必对照上图↑)

origin.Ancestors();   // Container, Root  
                      //- 返回祖先
origin.Children();    // Sphere_A, Sphere_B, Group, Sphere_A, Sphere_B
                      // - 返回子节点所有
origin.Descendants(); // Sphere_A, Sphere_B, Group, P1, Group, Sphere_B, P2, Sphere_A, Sphere_B
                      // - 返回所有后代,即包含孙节点及以后的节点
origin.BeforeSelf(); // C1, C2   - 返回位于自己上面的同级
origin.AfterSelf();  // C3, C4   - 返回位于自己下方的同级

利用本插件你可以使用 链式语法 操作游戏对象:包含但不限于断言子节点,删除游戏对象,获取组件。

// destroy all filtered(tag == "foobar") objects
// 将删除所有 tag 为 “foobar” 的游戏对象
root.Descendants().Where(x => x.tag == "foobar").Destroy();

// destroy all cloned objects
// 将删除所有 克隆出来的对象 (即断言 后代名称以 “Clone”结尾)
origin.transform.root.gameObject
    .Descendants()
    .Where(x => x.name.EndsWith("(Clone)"))
    .Destroy();

// get FooScript under self childer objects and self
//演示获取 自身 以及 子节点 的指定组件 FooScript 
var fooScripts = root.ChildrenAndSelf().OfComponent<FooScript>(); 

篇幅有限,仅仅节选该仓库部分ReadMe 内容,更多 API/功能 请挪步该仓库即可!

链接

LINQ to GameObject - Traverse GameObject Hierarchy by LINQ

结语:

喜欢链式语法或者熟悉且喜欢Linq语法的同学可以进去一探究竟。

本文集持续更新ing

转载于:https://www.jianshu.com/p/dc6c03635476

猜你喜欢

转载自blog.csdn.net/weixin_33719619/article/details/91056277