Transform继承自IEnumerable,可以对它进行迭代。但当你在迭代的同时,又对child进行setParent操作时,会出现意想不到的结果。

下面是我使用foreach和getchild得到的bug,及解决办法。

使用foreach

当在使用foreach获取所有的child,并且同时修改child的parent为其它,会出现只能修改部分,但不会报错。

foreach (var tran in rideEffect.InstanceAsset.transform)
{
var child = tran as Transform;
if (child == null)
{
continue;
}
KTool.SetChild(child, boneTrans.transform);
}

使用GetChild

使用getchild获取每一个child,同时设置child的parent为其它时,会报:Transform child out of bounds

var childCount = rideEffect.InstanceAsset.transform.childCount;
for (int idx = ; idx < childCount; idx++)
{
var child = rideEffect.InstanceAsset.transform.GetChild(idx);
KTool.SetChild(child, boneTrans.transform);
}

解决办法

添加一个扩展方法获取所有的childs,存起来。

或者也可以不写扩展方法,直接使用List<Transform>存child。

public static IEnumerable<Transform> GetChildren(this Transform tr)
{
List<Transform> children = new List<Transform>();
foreach (Transform child in tr)
{
children.Add(child);
}
// You can make the return type an array or a list or else.
return children as IEnumerable<Transform>;
}

调用方法,这样就可以修改完全部的child

var childs = rideEffect.InstanceAsset.transform.GetChildren();
foreach (var child in childs)
{
KTool.SetChild(child, boneTrans.transform);
}

foreach Transform 同时chils.setParent引起的bug的更多相关文章

  1. 项目中遇到的各种bug和踩过的坑

    zepto 赋值时单位转换问题 zepto 的 animate 方法移动某个元素的位置时,例如修改某个绝对定位的元素的 left 值,要与修改前的值单位一致,修改前如果是像素值,修改后也要是像素值,否 ...

  2. NGUI UIGrid 动态刷新布局 && BUG FIX

    /// <summary> /// "1" => 对应的一个UISpirte,"1234" => 对应四个预设 /// </sum ...

  3. unity transform 常用操作

    1.寻找物体 1.1 寻找满足条件的子物体 ` public static Transform FindObj(Transform transform, Func<Transform, bool ...

  4. unity中遍历Transform的子物体

    1.遍历Transform直接子transform private void Start() { var Equipment = building.transform.FindChild(" ...

  5. GameObject.Find与Transform.Find的区别

    1.GameObject.Find 函数原型: public static GameObject Find(string name); 说明:1.GameObject只能查找到active的物体 2. ...

  6. Unity初级案例——贪吃蛇

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...

  7. NetworkManager网络通讯_NetworkLobbyManager(三)

    此部分可以先建立游戏大厅,然后进入游戏,此处坑甚多耗费大量时间.国内百度出来的基本没靠谱的,一些专栏作家大V也不过是基本翻译了一下用户手册(坑啊),只能通过看youtube视频以及不停的翻阅用户手册解 ...

  8. Unity3D核心类型一览

    Unity3D核心类型一览 本文记录了Unity3D的最基本的核心类型.包括Object.GameObject.Component.Transform.Behaviour.Renderer.Colli ...

  9. Unity3d项目入门之虚拟摇杆

    Unity本身不提供摇杆的组件,开发者可以使用牛逼的EasyTouch插件或者应用NGUI实现相关的需求,下面本文通过Unity自身的UGUI属性,实现虚拟摇杆的功能. 主参考 <Unity:使 ...

随机推荐

  1. Java 容器源码分析之 Set

    Set 表示由无重复对象组成的集合,也是集合框架中重要的一种集合类型,直接扩展自 Collection 接口.在一个 Set 中,不能有两个引用指向同一个对象,或两个指向 null 的引用.如果对象 ...

  2. python 闯关之路二(模块的应用)

    1.有如下字符串:n = "路飞学城"(编程题) - 将字符串转换成utf-8的字符编码的字节,再将转换的字节重新转换为utf-8的字符编码的字符串 - 将字符串转换成gbk的字符 ...

  3. Perl正则表达式引用

    正则表达式两篇: 基础正则 Perl正则 本文是对Perl正则的一点扩展,主要内容是使用qr//创建正则对象,以及一些其它的技巧. qr//创建正则对象 因为可以在正则模式中使用变量替换,所以我们可以 ...

  4. 五分钟彻底学会iptables防火墙--技术流ken

    iptables简介 IPTABLES 是与最新的 3.5 版本 Linux内核集成的 IP 信息包过滤系统.如果 Linux 系统连接到因特网或 LAN.服务器或连接 LAN 和因特网的代理服务器, ...

  5. [转]分别使用Node.js Express 和 Koa 做简单的登录页

    本文转自:https://blog.csdn.net/weixin_38498554/article/details/79204240 刚刚学了Koa2,由于学的不是很深,并没有感受到网上所说的Koa ...

  6. [nodejs] nodejs开发个人博客(二)入口文件

    错误处理中间件 定义错误处理中间件必须使用4个参数,否则会被作为普通中间件 /*错误处理器*/ application.use(function(err,req,res,next){ console. ...

  7. Linux-bg和fg命令(19)

    使用ctrl+z将程序挂在后台: jobs 查看后台的命令: fg(fore go) 将后台的命令,放置前台(fore)继续执行,比如:fg 2     //等价于vi 2.txt bg(back g ...

  8. mybatis_ The content of element type association must match (constructor,id,result,ass ociation,collection,discriminator)

    一般遇到这种问题肯定要看一看association中元素编写顺序, <resultMap id="orderRslMap" type="orders"&g ...

  9. python面向对象学习(四)继承

    目录 1. 单继承 1.1 继承的概念.语法和特点 1.2 方法的重写 1.3 父类的 私有属性 和 私有方法 2. 多继承 2.1 多继承的使用注意事项 2.2 新式类与旧式(经典)类 1. 单继承 ...

  10. [小知识点] react 性能

    场景: jsx 绑定方法 方法有3种 1: // 在html中,使用箭头函数,自动绑定this class SearchHistory extends React.Component {      c ...