【问题标题】:Changing GUI Button text when clicked the UI Button单击 UI 按钮时更改 GUI 按钮文本
【发布时间】:2019-07-17 20:00:03
【问题描述】:

我想在单击 UI 按钮时更改 GUI 按钮文本。我写的 onGUI() 方法如下。

此方法用于在单击按钮时创建新按钮并显示树结构的子部分。

如果我向您展示我想要在代码中进行的更改,例如,当我单击 UI 拉丁按钮时,currentPart.EnglishTitle 会像 currentPart.LatinTitle 一样发生变化。我在代码中也提到了这部分作为注释

currentPart 是一个 HumanBodyPart 对象。 HumanBodyPart 是一个存储我的树结构节点的类。

我给你我的代码的必要部分。如果我给出的代码中缺少部分,我可以编辑所需的部分。

onGUI() 方法在这里...



    private void OnGUI()
    {

        Vector3 scale = new Vector3(Screen.width / nativeSize.x, Screen.height / nativeSize.y, 1.0f);
        GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, scale);

        float spacing = 25;
        float x = 7 + spacing;
        float y = 63;

        HumanBodyPart mainBodyPart = bodyVisualizer.BodyData.Body.SubParts[0];
        List<HumanBodyPart> nextPartsToRender = new List<HumanBodyPart>(new HumanBodyPart[] { mainBodyPart });
        List<HumanBodyPart> allPartsToRender = new List<HumanBodyPart>(new HumanBodyPart[] { mainBodyPart });

        scrollPosition = GUI.BeginScrollView(new Rect(7, y, 264, 485), scrollPosition, new Rect(7, y, 528, scrollPosition_y));

        while (nextPartsToRender.Count > 0)
        {

            HumanBodyPart currentPart = nextPartsToRender[0];
            nextPartsToRender.RemoveAt(0);

            //The place I want to change above is the place "currentPart.English" in a bottom line
            if (GUI.Button(new Rect(currentPart.DrawDepth * spacing + x, y, 200, 20), currentPart.EnglishTitle))
            {
                if (!currentPart.IsClicked)
                {
                    currentPart.IsClicked = true;
                    HumanBodyVisualizer.ShowMode showModeFullBody = HumanBodyVisualizer.ShowMode.Invisible;
                    bodyVisualizer.ShowBody(showModeFullBody);

                    AllSubPartsAndRoot.Insert(AllSubPartsAndRoot.Count, currentPart);
                    addAllSubPartsOfClickButton(currentPart, AllSubPartsAndRoot, AllSubPartsAndRoot.Count - 1);
                    HumanBodyVisualizer.ShowMode showModeCurrentPart = HumanBodyVisualizer.ShowMode.LowTransparent;

                    for (int i = 0; i < AllSubPartsAndRoot.Count; i++)
                    {
                        bodyVisualizer.ShowBodyPart(showModeCurrentPart, AllSubPartsAndRoot[i]);
                    }
                }
                else
                {
                    currentPart.IsClicked = false;
                    List<HumanBodyPart> RemoveBodyParts = new List<HumanBodyPart>();
                    RemoveBodyParts.Insert(0, currentPart);
                    addAllSubPartsOfClickButton(currentPart, RemoveBodyParts, 1);

                    for (int i = 0; i < RemoveBodyParts.Count; i++)
                    {
                        if (AllSubPartsAndRoot.Contains(RemoveBodyParts[i]))
                        {
                            bodyVisualizer.ShowBodyPart(HumanBodyVisualizer.ShowMode.Invisible, RemoveBodyParts[i]);
                            AllSubPartsAndRoot.Remove(RemoveBodyParts[i]);
                        }
                    }
                    if (AllSubPartsAndRoot.Count == 0)
                    {
                        bodyVisualizer.ShowBody(HumanBodyVisualizer.ShowMode.LowTransparent);
                    }
                    else
                    {
                        for (int ii = 0; ii < AllSubPartsAndRoot.Count; ii++)
                        {

                            bodyVisualizer.ShowBodyPart(HumanBodyVisualizer.ShowMode.LowTransparent, AllSubPartsAndRoot[ii]);
                        }

                    }
                }
            }
            if (currentPart.SubParts.Count != 0)
            {

                if (GUI.Button(new Rect(x - spacing + currentPart.DrawDepth * spacing, y, 20, 20), ">"))
                {
                    if (!currentPart.IsExpanded)
                    {
                        currentPart.IsExpanded = true;

                    }
                    else
                        currentPart.IsExpanded = false;
                }
                if (currentPart.IsExpanded)
                {
                    nextPartsToRender.InsertRange(0, currentPart.SubParts);
                    allPartsToRender.InsertRange(allPartsToRender.Count - 1, currentPart.SubParts);
                    scrollPosition_y = allPartsToRender.Count * spacing;
                }
            }
            y += spacing;
            index++;
        }
        // End the scroll view that we began above.
        GUI.EndScrollView();

    }

public Button turkishButton;
public Button englishButton;
public Button latinButton;

以上是脚本中的 UI 按钮。

【问题讨论】:

  • 你不能说(turkishButton.Text = "我的新文本")
  • 不,我不想更改 turkishButton 文本(它是一个 UI 按钮。),我想更改我在单击 UI turkishButton 时在 onGUI() 方法中创建的 GUI 按钮文本。
  • 这是统一制作的,还是winform的?
  • 统一你可以得到这样的对象: GameObject.Find("buttonName").GetComponentInChildren().text = "My new text";
  • 你使用OnGUI而不是Unity UI有什么原因吗?

标签: c# unity3d


【解决方案1】:

一般来说,您不应使用OnGUI,而应使用Unity UI 系统。


您可以只使用 string 来显示文本并执行例如

private string onGuiButtonLabel;

public Button turkishButton;
public Button englishButton;
public Button latinButton;

private void Awake()
{
    turkishButton.onClick.AddListener(()=>{onGuiButtonLabel = "Turkish";})
    englishButton.onClick.AddListener(()=>{onGuiButtonLabel = "English";})
    latinButton.onClick.AddListener(()=>{onGuiButtonLabel = "Latin";})
}

然后在 OnGUI 你使用

if (GUI.Button(new Rect(currentPart.DrawDepth * spacing + x, y, 200, 20), onGuiButtonLabel))

或者可能使用一种带有枚举的字典

public Language
{
    English,
    Turkish,
    Latin
}

private Dictionary<Language, string>() titles;

private Language curentLanguage;

// or wherever you want to initialize it
private void Awake()
{
     titles = new Dictionary<Language, string>()
     {
         {Language.English, currentPart.EnglishTitle}
         {Language.Turkish, currentPart.TurkishTitle}
         {Language.Latin, currentPart.LatinTitle}
     }

     turkishButton.onClick.AddListener(()=>{curentLanguage = Language.Turkish;})
     englishButton.onClick.AddListener(()=>{curentLanguage = Language.English;})
     latinButton.onClick.AddListener(()=>{curentLanguage = Language.Latin;})
}

然后在OnGUI使用

if (GUI.Button(new Rect(currentPart.DrawDepth * spacing + x, y, 200, 20), titles[currentLanguage]))

【讨论】:

  • currentPart.EnglishTitlecurrentPart.TurkishTitlecurrentPart.LatinTitle 未在 awake() 方法中实现。 currentPart 在 onGUI() 中。我不能从 onGUI() 中取出 currentPart (比如作为类的属性)。我该如何处理?
  • 正如我在评论中所说,我不知道你在哪里或何时得到它。只需将字典的初始化移动到OnGUI,然后使用带有枚举的第二个解决方案。我什至建议宁愿将字典和初始化实现到 currentPart 类中,这样每个部分都已经有了自己的字典,你只能访问。会更有效率。 注意顺便说一句,它是OnGUI(大写字母O不是onGUI
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 2019-07-08
相关资源
最近更新 更多