我在项目中遇到了一个问题,我正在创建一个Scroll listview来显示屏幕中列表中的所有元素。 我在面板中使用按钮来显示列表。 现在,当我调用ShowList()时,它会显示列表中的元素。
但是如果我将一些对象添加到列表并再次调用ShowList(),那么它也会在存在实例化对象时复制先前的对象。
要解决此问题,我使用Destroy()删除克隆,但是当列表包含太多项目(300~400)时,删除它们会导致游戏延迟。如何为ui按钮创建对象池或只是停用它们。
public class two : MonoBehaviour {
public GameObject Button_Template;
private List<GameName> gm = new List<GameName>();
public void Exit()
{
var og = GameObject.FindGameObjectsWithTag("clone");
for (int i = 0; i < 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()
{
for (int i = 0 ; i < gm.Count; i++)
{
GameObject go = Instantiate(Button_Template) as GameObject;
go.SetActive(true);
one TB = go.GetComponent<one>();
TB.SetName(gm[i].GName);
go.transform.SetParent(Button_Template.transform.parent);
go.tag = "clone";
}
}
}
答案 0 :(得分:1)
列表可用于实现对象池。完成使用后,只需disable
GameObject即可。如果要再次使用它,请重新启用它。下面的简单GameObject Pool脚本重新实现了Instantiate
和Destroy
函数。
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 only be called once");
return;
}
this.buttonPrefab = buttonPrefab;
//Create 400 Buttons
buttonPool = new List<GameObject>();
for (int i = 0; i < amount; i++)
{
buttonPool.Add(Instantiate(this.buttonPrefab) as GameObject);
}
ready = true;
}
//Creates/Enables single Button
public GameObject Instantiate()
{
if (!ready)
{
showError();
return null;
}
//Return any Button that is not active
for (int i = 0; i < buttonPool.Count; i++)
{
if (!buttonPool[i].activeSelf)
{
return buttonPool[i];
}
}
//Create new Button if there is none available in the pool. Add it to the list then return it
GameObject tempButton = Instantiate(this.buttonPrefab) as GameObject;
buttonPool.Add(tempButton);
return tempButton;
}
//Destroys/Disables single Button
public void Destroy(GameObject button)
{
if (!ready)
{
showError();
return;
}
button.SetActive(false);
}
//Destroys/Disables all Buttons
public void DestroyAll()
{
if (!ready)
{
showError();
return;
}
for (int i = 0; i < buttonPool.Count; i++)
{
if (buttonPool[i].activeSelf)
{
buttonPool[i].SetActive(false);
}
}
}
private void showError()
{
Debug.LogError("createButtonPool must be called once before any other function can be called");
}
}
用法:
public GameObject ButtonPrefab;
BUTTONPOOL bPool;
void test()
{
if ((bPool = GetComponent<BUTTONPOOL>()) == null)
{
gameObject.AddComponent<BUTTONPOOL>();
bPool = GetComponent<BUTTONPOOL>();
}
//Initiate with 300 Buttons
bPool.createButtonPool(ButtonPrefab, 50);
GameObject tempButton = bPool.Instantiate();
//MUST SET BUTTON ACTIVE CALLING Instantiate()
tempButton.SetActive(true);
//You can do other things with the button
one TB = tempButton.GetComponent<one>();
//To destroy that single button
bPool.Destroy(tempButton);
//OR destroy that all button
bPool.DestroyAll();
}
请注意,在Button
脚本调用自定义Instantiate()
函数后,您必须将BUTTONPOOL
设置为有效。