【发布时间】: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有什么原因吗?