由于使用foreach循环最终会在调用期间产生不必要的堆内存分配。可以使用但不推荐使用

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 * Author:W
 * 循环遍历
 */
public class ForeachTest : MonoBehaviour {

	// Use this for initialization
	void Start () {

		//会在堆上分配一个Enumerator类对象,产生一些消耗
		foreach (Transform child in transform)
		{
			if (child != null)
			{
				
			}
		}


		for (int i = 0; i < transform.childCount; i++)
		{
			Transform child = transform.GetChild(i);

			if (child != null)
			{
				
			}

		}
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐