unity中获取子物体的相关组件

using System;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.GraphicsBuffer;

public class ControlComponent : MonoBehaviour
{
    Dictionary<string, UnityEngine.Component> d_obj;

    public void Awake()
    {
        d_obj = new Dictionary<string, UnityEngine.Component>();

        // 定义你想要查找的组件类型
        Type[] componentTypes = { typeof(Image), typeof(Text), typeof(Button) };

        foreach (Type type in componentTypes)
        {
            //d_obj.AddRange(targetObject.GetComponentsInChildren(type, true));
            UnityEngine.Component[] coms = this.gameObject.GetComponentsInChildren(type, true);
            foreach (UnityEngine.Component target in coms)
            {
                d_obj.TryAdd(target.name, target);
            }
        }
    }

    public T GetComponetMul<T>(string name) where T : UnityEngine.Component
    {
        UnityEngine.Component d_comp;
        if (d_obj.TryGetValue(name, out d_comp))
        {
            T d = d_comp.GetComponent<T>();
            if (d == null)
            {
                d = d_comp.AddComponent<T>();
            }
            return d;
        }
        return null;
    }

    private void OnDestroy()
    {
        d_obj.Clear();
        d_obj = null;
    }
}

使用方法:在类中继承ControlComponent 

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

public class RankFSTHView : ControlComponent
{
    HeroSpinLoad heroSpine;
    long id;
    private EntityManager entityManager;
    private PlayerDataComponent playerData;
    private void Start()
    {
        entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
        playerData = entityManager.GetComponentData<PlayerDataComponent>(GlobalVariables.PlayerDataEntity);
        Transform spine = this.transform.Find("spine");
        heroSpine = spine.gameObject.AddComponent<HeroSpinLoad>();
        UnityTools.AddClickEventToButton(this.GetComponent<Button>(), OnOpenRankInfoPanel);
    }
    public void UpdateUI(_RANK_ITEM data, int type)
    {
        id = data.Id;
        Text nameTxt = GetComponetMul<Text>("nameTxt");
        nameTxt.text = playerData.Id == (ulong)data.Id ? playerData.Name.ToString(): data.Name;
        Text levelTxt = GetComponetMul<Text>("levelTxt");
        if (type == 1)
        {
            levelTxt.text = string.Format("{0}层", data.Score);
        }
        else {
            levelTxt.text = string.Format("{0}", data.Score);
        }
        if (data.HeroId > 0)
        {
            heroSpine.LoadHeroSpine(data.HeroId);
        }
    }

    private void OnOpenRankInfoPanel()
    {
        UIHelp.CreateUI<OneRankPanel_View>(null, null, id);
    }

}

比如        Text nameTxt = GetComponetMul<Text>("nameTxt");可以直接获取相关的组件

Unity,我们可以使用GetComponent方法来获取其他游戏物体上的组件。当我们需要和其他物体进行交互或控制时,这个方法非常有用。 首先,我们需要确保我们有对应游戏物体的引用。这可以通过在脚本声明一个公共变量,并通过检查器将相应的游戏物体拖入来完成。例如,我们可以声明一个公共的Rigidbody2D变量: public Rigidbody2D targetRigidbody; 然后,我们可以通过在代码使用GetComponent方法来获取物体上的Rigidbody2D组件。例如,在某个方法,我们可以使用以下代码来获得targetRigidbody上的Rigidbody2D组件: Rigidbody2D rb = targetRigidbody.GetComponent<Rigidbody2D>(); 现在,我们可以使用rb变量来操作targetRigidbody上的Rigidbody2D组件。例如,我们可以通过设置速度来移动该物体: rb.velocity = new Vector2(1, 0); 通过GetComponent方法,我们可以轻松地获取其他物体上的各种组件,如碰撞器、动画控制器等。这样,我们就可以与其他物体进行交互,实现各种游戏功能。 需要注意的是,当我们使用GetComponent方法来获取其他物体上的组件时,确保目标物体上确实有所需的组件,否则会返回空引用错误。另外,如果我们需要频繁地获取同一个组件,最好在Start或Awake方法将其缓存起来,以避免每次都重新获取。这样可以提高性能和效率。 通过使用GetComponent方法,我们可以轻松地在Unity获取其他物体上的组件,从而实现更丰富的交互和控制效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值