递归查找子物体+生命周期函数

递归查找子物体

相关代码:

cs 复制代码
    Transform FindChild(string childName, Transform parent)
    {
        if (childName == parent.name) {
            return parent;
        }
        if (parent.childCount < 1)
        {
            return null;
        }
        Transform obj = null;
        for(int i = 0; i < parent.childCount; i++)
        {
            Transform t = parent.GetChild(i);
            obj = FindChild(childName, t);
            if (obj != null)
            {
                break;
            }
        }
        return obj;
    }

在Unity上运行,例如:将该部分代码内容所在脚本挂载到 Cube (7)上,查找Cube (5):

cs 复制代码
    void Update()
    {
         Transform t = FindChild("Cube (5)", transform);
        if (t)
        {
            Debug.Log("t:" + t.name);
        }
       
    }

运行结果如下图:

Unity生命周期函数

相关代码:

cs 复制代码
    private void Awake()
    {
        Debug.Log("Awake");
    }
    /// <summary>
    /// 第二执行的方法
    /// 每次当前游戏物体激活都会被调用
    /// </summary>
    private void OnEnable()
    {
        Debug.Log("OnEnable");
    }
    /// <summary>
    /// 每次当前游戏物体失活都会被调用
    /// </summary>
    private void OnDisable()
    {
        Debug.Log("OnDisable");
    }
    /// <summary>
    /// 只会执行一次
    /// </summary>
    void Start()
    {
        Debug.Log("Start");
    }
    /// <summary>
    /// 物理更新写在这里面
    /// 更新频率是根据Fixed Timestep来的
    /// </summary>
    private void FixedUpdate()
    {
        Debug.Log("FixedUpdate");
    }
    // Update is called once per frame
    void Update()
    {
        Debug.Log("Update");
    }
    /// <summary>
    /// 在Update后执行,同样也是每一帧都会执行
    /// </summary>
    private void LateUpdate()
    {
        Debug.Log("LateUpdate");
    }
    /// <summary>
    /// 游戏物体被销毁时,先调用Disable,然后再调用OnDestory
    /// </summary>
    private void OnDestroy()
    {
        Debug.Log("OnDestroy");
    }

该系列专栏为网课课程笔记,仅用于学习参考。

相关推荐
探求虚无1 天前
用 AI 在 Unity 里做一款 RTS:最难的不是生成代码,而是证明它真的能玩
人工智能·unity·游戏引擎
huaqianzkh1 天前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
用户3721574261352 天前
C# 实现 Excel 单元格保护:锁定公式并允许编辑指定区域
c#·ai编程·编程语言
17岁的勇气2 天前
Unity Shader unity文档学习笔记(二十二):shader变体
笔记·学习·unity
公子小六2 天前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
三雷科技2 天前
第6章第4节:自定义分词器
开发语言·c#
zhanghaha13142 天前
C# 系列教程_12 字符串 string 保姆级系统教程(面向新手,大量示例 + 原理讲解)
开发语言·数据库·c#
Coder_Shenshen3 天前
罗克韦尔ACD工程归档离线变量树解析
算法·c#
寂寞的船3 天前
【Unity知识速递】第10期 Unity GC 零分配实战:从 UnityEngine.Pool 到手动 GC 控制的全链路优化
jvm·unity·c#·游戏引擎·lucene
1-1=03 天前
C# 数据结构和特殊类型
数据结构·c#·.net·.netcore