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

Unity 自带函数 Reset() 的使用

当然我们还有更简单的方式,即是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);
        }
    }
}

你会发现一个神奇的事,当你脚本拖上去的时候,这些 button的名字,text值,颜色,瞬间变了,神奇吧,都不需要你运行程序,这个时候你就可以把脚本删了,已经不需要这个脚本了,

脚本上去的瞬间图(不需要运行程序):

Unity 自带函数 Reset() 的使用

脚本删掉之后的图:

Unity 自带函数 Reset() 的使用

相关文章: