实例化一个按钮数组
Instantiate an array of buttons
我需要 instantiate/create 按钮里面有一个 table。我希望所有的按钮都贴上标签,然后也涂上颜色,但是根据我的研究和测试,我相信我只能有一个或另一个。我希望它们位于可滚动的 table 中。
public void OnGUI()
{
//create a window
GUI.Window(0, windowRect, WindowFunction, "Meeting Request Viewer");
}
public void WindowFunction(int windowID)
{
//Fetches all user Data
string[][] userArray = GetComponent<Userdata>().CallDetail();
string[][] StudentArray = GetComponent<Userdata>().UserSorting(userArray);
Debug.Log("here");
//Calls the SortStudentArray method
string[,] SortedStudentArray = SortStudentList();
//Creates a box with a scrolling bar to taverse the y axis
scrollPosition = GUI.BeginScrollView(new Rect(Screen.width / 6, Screen.height / 6, 350, 250), scrollPosition, new Rect(0, 0, 300, 40 * SortedStudentArray.Length));
//for each row in the sorted student array
for (int x = 0; x < SortedStudentArray.Length; x++)
{
GameObject StudentButton = Instantiate(GUI.Button(new Rect(0, BSpace, 300, 20), (SortedStudentArray[x, 6])));
//This keeps the gap between each button consistent
BSpace = +scrollPosition.height;
}
GUI.EndScrollView();
}
可以同时给按钮添加标签和颜色。 Unity 的 GUI 是一个即时模式系统,因此您无需实例化并且必须在每次调用 GUI.Button 之前设置 GUI.color*。这是一个例子;
public int ButtonSpacing = 10;
public int ButtonWidth = 80;
public int ButtonHeight = 30;
public string[] Labels = { "Black", "Red", "Green", "Blue" };
public Color[] Colors = { Color.black, Color.red, Color.green, Color.blue };
private void OnGUI ()
{
var y = ButtonSpacing + ButtonHeight;
for (var i = 0; i < 4; i++)
{
GUI.backgroundColor = Colors[i];
GUI.Button(new Rect(ButtonSpacing, ButtonSpacing + i * y, ButtonWidth, ButtonHeight), Labels[i]);
}
}
*实际上有3种给GUI上色的方法。选项为 GUI.backgroundColor
、GUI.contentColor
和 GUI.color
。请参阅 GUI 文档以获取有关其用法的更多信息。
我需要 instantiate/create 按钮里面有一个 table。我希望所有的按钮都贴上标签,然后也涂上颜色,但是根据我的研究和测试,我相信我只能有一个或另一个。我希望它们位于可滚动的 table 中。
public void OnGUI()
{
//create a window
GUI.Window(0, windowRect, WindowFunction, "Meeting Request Viewer");
}
public void WindowFunction(int windowID)
{
//Fetches all user Data
string[][] userArray = GetComponent<Userdata>().CallDetail();
string[][] StudentArray = GetComponent<Userdata>().UserSorting(userArray);
Debug.Log("here");
//Calls the SortStudentArray method
string[,] SortedStudentArray = SortStudentList();
//Creates a box with a scrolling bar to taverse the y axis
scrollPosition = GUI.BeginScrollView(new Rect(Screen.width / 6, Screen.height / 6, 350, 250), scrollPosition, new Rect(0, 0, 300, 40 * SortedStudentArray.Length));
//for each row in the sorted student array
for (int x = 0; x < SortedStudentArray.Length; x++)
{
GameObject StudentButton = Instantiate(GUI.Button(new Rect(0, BSpace, 300, 20), (SortedStudentArray[x, 6])));
//This keeps the gap between each button consistent
BSpace = +scrollPosition.height;
}
GUI.EndScrollView();
}
可以同时给按钮添加标签和颜色。 Unity 的 GUI 是一个即时模式系统,因此您无需实例化并且必须在每次调用 GUI.Button 之前设置 GUI.color*。这是一个例子;
public int ButtonSpacing = 10;
public int ButtonWidth = 80;
public int ButtonHeight = 30;
public string[] Labels = { "Black", "Red", "Green", "Blue" };
public Color[] Colors = { Color.black, Color.red, Color.green, Color.blue };
private void OnGUI ()
{
var y = ButtonSpacing + ButtonHeight;
for (var i = 0; i < 4; i++)
{
GUI.backgroundColor = Colors[i];
GUI.Button(new Rect(ButtonSpacing, ButtonSpacing + i * y, ButtonWidth, ButtonHeight), Labels[i]);
}
}
*实际上有3种给GUI上色的方法。选项为 GUI.backgroundColor
、GUI.contentColor
和 GUI.color
。请参阅 GUI 文档以获取有关其用法的更多信息。