Skip to content

Instantly share code, notes, and snippets.

@enpel
Created May 26, 2015 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enpel/1b77886b8f16aefe84a7 to your computer and use it in GitHub Desktop.
Save enpel/1b77886b8f16aefe84a7 to your computer and use it in GitHub Desktop.
GameObjectを再利用するための仕組み。
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
public class UIScrollViewPool : MonoBehaviour
{
Stack<HogeItemComponent> stackIcons = new Stack<HogeItemComponent>();
// NGUIの場合はUIWidgetでなければ描画がおかしくなる。UnityUIとかならGameObjectでも良いかも?
UIWidget poolRoot;
void Awake()
{
// 使用しないオブジェクトの退避先
var obj = new GameObject ();
obj.transform.parent = this.transform;
poolRoot = obj.AddComponent<UIWidget> ();
}
public void Push(HogeItemComponent icon)
{
if (icon == null) {
Debug.LogError("HogeItemComponent Push: this icon is null! ");
return;
}
stackIcons.Push (icon);
icon.transform.parent = poolRoot.transform;
icon.transform.position = Vector3.zero;
icon.gameObject.SetActive (false);
}
public ItemIconComponent Pop(IHogeInfo info, Transform parent)
{
if (stackIcons.Count == 0)
return null;
IHogeItemComponent icon = stackIcons.Pop ();
icon.gameObject.SetActive (true);
icon.gameObject.transform.parent = parent;
icon.Initialize (info);
return icon;
}
}
public interface IHogeItemComponent
{
Initialize(IHogeInfo info);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment