Created
May 26, 2015 13:32
-
-
Save enpel/1b77886b8f16aefe84a7 to your computer and use it in GitHub Desktop.
GameObjectを再利用するための仕組み。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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