Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实例化一个按钮数组_C#_Unity3d_Button_Instantiation - Fatal编程技术网

C# 实例化一个按钮数组

C# 实例化一个按钮数组,c#,unity3d,button,instantiation,C#,Unity3d,Button,Instantiation,我需要在表中实例化/创建按钮。我希望所有的按钮都贴上标签,然后也涂上颜色,但是从我的研究和测试来看,我相信我只能有一个或另一个。我想把它们放在可滚动的桌子里 public void OnGUI() { //create a window GUI.Window(0, windowRect, WindowFunction, "Meeting Request Viewer"); } public void WindowFunction(int windowID) {

我需要在表中实例化/创建按钮。我希望所有的按钮都贴上标签,然后也涂上颜色,但是从我的研究和测试来看,我相信我只能有一个或另一个。我想把它们放在可滚动的桌子里

        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();

}
public void OnGUI()
{
//创建一个窗口
窗口(0,windowRect,WindowFunction,“会议请求查看器”);
}
public void WindowFunction(int windowID)
{
//获取所有用户数据
字符串[][]userArray=GetComponent().CallDetail();
字符串[][]StudentArray=GetComponent().UserSorting(userArray);
Debug.Log(“此处”);
//调用SortStudentArray方法
字符串[,]SortedStudentArray=SortStudentList();
//创建一个带有滚动条的框,使其与y轴相对
scrollPosition=GUI.BeginScrollView(新矩形(Screen.width/6,Screen.height/6350250),scrollPosition,新矩形(0,0300,40*SortedStudentArray.Length));
//对于已排序学生数组中的每一行
对于(int x=0;x
可以同时为按钮添加标签和颜色。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]);
    }
}
public int ButtonSpacing=10;
公共int按钮宽度=80;
公共int按钮高度=30;
公共字符串[]标签={“黑色”、“红色”、“绿色”、“蓝色”};
公共颜色[]颜色={Color.black,Color.red,Color.green,Color.blue};
私有void OnGUI()
{
变量y=按钮位置+按钮高度;
对于(变量i=0;i<4;i++)
{
GUI.backgroundColor=颜色[i];
按钮(新的矩形(按钮间距,按钮间距+i*y,按钮宽度,按钮宽度),标签[i]);
}
}

*实际上有三种方法可以给GUI上色。选项有
GUI.backgroundColor
GUI.contentColor
GUI.color
。有关其用法的更多信息,请参阅文档。

只是一个小提示:使用属性文件或大量常量以避免出现幻数。后来没有人知道为什么要使用屏幕宽度/6,或者在其他地方你不知道宽度公式和变量会很好。介意告诉我们这个代码在哪里吗?它在
OnGUI()中吗?还有什么是
SortedStudentArray
?在问题中添加了代码,这也是我使用
屏幕的唯一位置。宽度/6
,但我会确保注释whyAhh,好的,谢谢!如何注册已按下的按钮?GUI.button将返回一个布尔值,指示是否在该帧中单击了该按钮。如果有,则为True,否则为false。所以你可以
if(GUI.Button())DoSomething()