单击UI按钮时更改GUI按钮文本

时间:2019-07-17 11:07:55

标签: c# unity3d

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

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

例如,如果我向您显示要在代码中进行的更改,例如,当我单击UI Latin Button时,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按钮。

1 个答案:

答案 0 :(得分:0)

通常,您不应该使用OnGUI,而应该使用https://reactjs.org/docs/context.html系统。


您可能只有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))

或者可以使用一种带有类似枚举的Dictionary

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]))