foreach Transform 同时chils.setParent引起的bug
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的更多相关文章
- 项目中遇到的各种bug和踩过的坑
zepto 赋值时单位转换问题 zepto 的 animate 方法移动某个元素的位置时,例如修改某个绝对定位的元素的 left 值,要与修改前的值单位一致,修改前如果是像素值,修改后也要是像素值,否 ...
- NGUI UIGrid 动态刷新布局 && BUG FIX
/// <summary> /// "1" => 对应的一个UISpirte,"1234" => 对应四个预设 /// </sum ...
- unity transform 常用操作
1.寻找物体 1.1 寻找满足条件的子物体 ` public static Transform FindObj(Transform transform, Func<Transform, bool ...
- unity中遍历Transform的子物体
1.遍历Transform直接子transform private void Start() { var Equipment = building.transform.FindChild(" ...
- GameObject.Find与Transform.Find的区别
1.GameObject.Find 函数原型: public static GameObject Find(string name); 说明:1.GameObject只能查找到active的物体 2. ...
- Unity初级案例——贪吃蛇
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...
- NetworkManager网络通讯_NetworkLobbyManager(三)
此部分可以先建立游戏大厅,然后进入游戏,此处坑甚多耗费大量时间.国内百度出来的基本没靠谱的,一些专栏作家大V也不过是基本翻译了一下用户手册(坑啊),只能通过看youtube视频以及不停的翻阅用户手册解 ...
- Unity3D核心类型一览
Unity3D核心类型一览 本文记录了Unity3D的最基本的核心类型.包括Object.GameObject.Component.Transform.Behaviour.Renderer.Colli ...
- Unity3d项目入门之虚拟摇杆
Unity本身不提供摇杆的组件,开发者可以使用牛逼的EasyTouch插件或者应用NGUI实现相关的需求,下面本文通过Unity自身的UGUI属性,实现虚拟摇杆的功能. 主参考 <Unity:使 ...
随机推荐
- vue项目全局引入vue-awesome-swiper插件做出轮播效果
在安装了vue的前提下,打开命令行窗口,输入vue init webpack swiper-test,创建一个vue项目且名为swiper-test(创建速度可能会有点慢,耐心等),博文讲完后,源码托 ...
- Nacos系列:欢迎来到Nacos的世界!
什么是Nacos? Nacos 是构建以"服务"为中心的现代应用架构 (例如微服务范式.云原生范式) 的服务基础设施. Nacos可以做什么? 1.动态配置服务:支持以中心化.外部 ...
- iconfont图标应用
一.什么是iconfont? 我们现在通常所指的iconfont,是用字体文件取代图片文件,来展示图标.特殊字体等元素的方法.iconfont是阿里巴巴矢量图标库是由阿里巴巴体验团队倾力打造的中国第一 ...
- ubuntu16.4系统和Gentos6.8系统查看开机自启动服务
ubuntu16.4系统查看自启服务: 需要自行安装一个sysv-rc-conf的工具来查看: sudo apt-get install sysv-rc-conf 查看自启命令: sudo sysv- ...
- python基础学习(五)while循环语句
while循环基本使用 循环的作用就是让指定的代码重复的执行 while循环最常用的应用场景就是让执行的代码按照指定的次数重复执行 流程图 基本语法 初始条件设置 —— 通常是重复执行的 计数器 wh ...
- spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean
5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...
- 三问助你Fundebug
译者按: Debug也要三省吾身! 原文: Three Questions About Each Bug You Find 译者: Fundebug 为了保证可读性,本文采用意译而非直译.另外,本文版 ...
- JavaScript 中的相等操作符 ( 详解 [] == []、[] == ![]、{} == !{} )
ECMAScript 中的相等操作符由两个等于号 ( == ) 表示,如果两个操作数相等,则返回 true. 相等操作符会先转换操作数(通常称为强制转型),然后比较它们的相等性. 在转换不同的数据类型 ...
- js 实现 0-9 随机排序
function randomsort(a, b) {return Math.random()>0.5 ? -1 : 1;//用Math.random()函数生成0~1之间的随机数与0.5比较, ...
- 51nod"省选"模测 A 树的双直径(树形dp)
题意 题目链接 Sol 比赛结束后才调出来..不多说啥了,就是因为自己菜. 裸的up-down dp,维护一下一个点上下的直径就行,一开始还想了个假的思路写了半天.. 转移都在代码注释里 毒瘤题目卡空 ...