Unity批量重命名 批量给图片赋值

如下图,Button有4个按钮 我们可以在程序运行的时候用代码动态的修改这些 button 的名字,这样当然可以,但是需要程序运行,还是会消耗程序的资源,等程序关闭,这些 button 的名字又会回到向下图一样的名字,

当然我们还有更简单的方式,即是Unity 自带的 Reset()函数,新建一个脚本挂载到 Content 上,代码如下:

 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class NewBehaviourScript : MonoBehaviour
{
 
    private void Reset()
    {
        int count = transform.childCount;
        for (int index = 0; index < count; index++)
        {
            transform.GetChild(index).name = (index + 1).ToString();
            transform.GetChild(index).GetChild(0).GetComponent<Text>().text = null;
            transform.GetChild(index).GetComponent<Image>().color = new Color(55f, 55f, 55f, 0.5f);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36848370/article/details/105171741