简体   繁体   中英

how to create a DropDownList at runtime in unity3d

I am new to Unity, so i don't know how to create a DropDownList at runtime in Unity3d

I am able to create list once the scene loads as per example given in below link http://wiki.unity3d.com/index.php?title=PopupList

But i don't know how to create those at runtime, i have tried creating this class.

#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 !");
       }
    }
}

But i am not able to add it in my for loop as below

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

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

Can anyone suggest what is wrong in this code snippet and what is the right way of doing this in javascript in unity3d.

I have changed my code to below to make it working

#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++;
        }       
    }
}

And instantiating in main class like this

gameObject.AddComponent(CustomLayout);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM