c# - Unity Reusing Ui element in list -
i have ran sort of problem in project, creating scroll listview show elements present in list in screen. using button in panel show list. when call showlist() shows elements in list.
but if add objects list , again call showlist() make clone of previous object instantiated objects present.
to solve deleting clones using destroy() when list contain many item (300~400) deleting them cause lag in game . how can create object pool ui button or deactivate them.
public class 2 : monobehaviour { public gameobject button_template; private list<gamename> gm = new list<gamename>(); public void exit() { var og = gameobject.findgameobjectswithtag("clone"); (int = 0; < og.length; i++) { destroy(og[i]); } } void start() { gm.add(new gamename("1")); gm.add(new gamename("2")); gm.add(new gamename("3")); gm.add(new gamename("4")); } public void showlist() { (int = 0 ; < gm.count; i++) { gameobject go = instantiate(button_template) gameobject; go.setactive(true); 1 tb = go.getcomponent<one>(); tb.setname(gm[i].gname); go.transform.setparent(button_template.transform.parent); go.tag = "clone"; } } }
list can used implement object pooling. disable
gameobject when done using it. re-enable again if want use again. simple gameobject pool script below re-implements instantiate
, destroy
functions.
public class buttonpool : monobehaviour { gameobject buttonprefab; list<gameobject> buttonpool; bool ready = false; public void createbuttonpool(gameobject buttonprefab, int amount = 400) { if (ready) { debug.logerror("createbuttonpool can called once"); return; } this.buttonprefab = buttonprefab; //create 400 buttons buttonpool = new list<gameobject>(); (int = 0; < amount; i++) { buttonpool.add(instantiate(this.buttonprefab) gameobject); } ready = true; } //creates/enables single button public gameobject instantiate() { if (!ready) { showerror(); return null; } //return button not active (int = 0; < buttonpool.count; i++) { if (!buttonpool[i].activeself) { return buttonpool[i]; } } //create new button if there none available in pool. add list return gameobject tempbutton = instantiate(this.buttonprefab) gameobject; buttonpool.add(tempbutton); return tempbutton; } //destroys/disables single button public void destroy(gameobject button) { if (!ready) { showerror(); return; } button.setactive(false); } //destroys/disables buttons public void destroyall() { if (!ready) { showerror(); return; } (int = 0; < buttonpool.count; i++) { if (buttonpool[i].activeself) { buttonpool[i].setactive(false); } } } private void showerror() { debug.logerror("createbuttonpool must called once before other function can called"); } }
usage:
public gameobject buttonprefab; buttonpool bpool; void test() { if ((bpool = getcomponent<buttonpool>()) == null) { gameobject.addcomponent<buttonpool>(); bpool = getcomponent<buttonpool>(); } //initiate 300 buttons bpool.createbuttonpool(buttonprefab, 50); gameobject tempbutton = bpool.instantiate(); //must set button active calling instantiate() tempbutton.setactive(true); //you can other things button 1 tb = tempbutton.getcomponent<one>(); //to destroy single button bpool.destroy(tempbutton); //or destroy button bpool.destroyall(); }
note must set button
active after calling custom instantiate()
function buttonpool
script.
Comments
Post a Comment