Change text of Unity 4.6 UI button

2019-03-01 00:05发布

I've been trying for days without success. How do I change the text of a Unity 4.6 button using C# script? Any suggestions are appreciated.

1条回答
等我变得足够好
2楼-- · 2019-03-01 00:40

Create a script and add it to your UIButton you should add the reference Unity.UI

using UnityEngine.UI;

then declare a variable

Text yourButtonText;

on the function Start put this

void Start()
{
    yourButtonText = transform.FindChild("Text").GetComponent<Text>();
}

then when you want to change your text add this

yourButtonText.text = "i am a button!";

if you need to apply more modifications visit http://docs.unity3d.com/ScriptReference/UI.Text.html

[VERSION WITH TAGS]

add this reference

using System.Collections.Generic;

public Text[] yourButtonTextArrays = new Text[15];

void Start()
{
    for (int i = 0; i < 15; i++ )
    {
        yourButtonTextArrays[i] = GameObject.FindGameObjectWithTag("Button" + i+1.ToString()).transform.FindChild("Text").GetComponent<Text>();
    }
}

then when you want to change your text add this

yourButtonTextArrays[yourButtonNumber].text = "i am a button from the array of buttons";
查看更多
登录 后发表回答