0

我是 Unity 新手,所以我不知道如何在运行时在 Unity3d 中创建 DropDownList

根据以下链接http://wiki.unity3d.com/index.php?title=PopupList中给出的示例,一旦场景加载,我就可以创建列表

但我不知道如何在运行时创建它们,我尝试过创建这个类。

#pragma strict
class CustomList extends MonoBehaviour 
{
    var title:String;
    var top:int;
    var left:int;
    var width:int;
    var height:int;
    private var listEntry = 0;
    private var list : GUIContent[];
    private var listStyle : GUIStyle;
    private var showList = false;
    public function CustomList(title:String,top:int,left:int,width:int,height:int){
       this.title = title;
       this.top=top;
       this.left = left;
       this.width = width;
       this.height = height; 

       list = new GUIContent[1]; 
       list[0] = new GUIContent("Granite");
       // Make a GUIStyle that has a solid white hover/onHover background to indicate highlighted items
       listStyle = new GUIStyle();
       listStyle.normal.textColor = Color.white;
       var tex = new Texture2D(2, 2);
       var colors = new Color[4];
       for (color in colors) color = Color.white;
       tex.SetPixels(colors);
       tex.Apply();
       listStyle.hover.background = tex;
       listStyle.onHover.background = tex;
       listStyle.padding.left = listStyle.padding.right = listStyle.padding.top = listStyle.padding.bottom = 4;  

    }

    function Start () {

    }
    function OnGUI () {
       Debug.Log("title for list is  : "+title);
       GUI.Label (Rect(10, 10, 100, 10), "You picked !");
       if (Popup.List (Rect(top, left, width, height), showList, listEntry, GUIContent(this.title), list, listStyle)) {
         GUI.Label (Rect(200, 70, 400, 20), "You picked !");
       }
    }
}

但我无法将它添加到我的 for 循环中,如下所示

for(var objCategory:Category in objCategoryList.listCategory){

       new CustomList(objCategory.categoryName,100,ctr*100,100,20);
       ctr++;
    }

任何人都可以建议这个代码片段中有什么问题,以及在 unity3d 中的 javascript 中执行此操作的正确方法是什么。

4

1 回答 1

0

我已将代码更改为以下以使其正常工作

#pragma strict
class CustomLayout extends  MonoBehaviour 
{
    private var listEntry = 0;
    private var list : GUIContent[];
    private var listStyle : GUIStyle;
    private var showList = false;
    public function testLayout(){       
        list = new GUIContent[1];   
        list[0] = new GUIContent("Granite");
        // Make a GUIStyle that has a solid white hover/onHover background to indicate highlighted items
        listStyle = new GUIStyle();
        listStyle.normal.textColor = Color.white;
        var tex = new Texture2D(2, 2);
        var colors = new Color[4];
        for (color in colors) color = Color.white;
        tex.SetPixels(colors);
        tex.Apply();
        listStyle.hover.background = tex;
        listStyle.onHover.background = tex;
        listStyle.padding.left = listStyle.padding.right = listStyle.padding.top = listStyle.padding.bottom = 4;            
    }

    function OnGUI () {
        var ctr:int =0;     
        for(var objCategory:Category in DropdownList.objCategoryList.listCategory){
            if (Popup.List (Rect(50, 50+(ctr*25), 100, 20), showList, listEntry, GUIContent(objCategory.categoryName), list, listStyle)) {
                GUI.Label (Rect(200, 70, 400, 20), "You picked !");
            }
            ctr++;
        }       
    }
}

并像这样在主类中实例化

gameObject.AddComponent(CustomLayout);
于 2013-01-18T08:25:57.403 回答