Created
October 24, 2016 07:57
-
-
Save takashicompany/f5a9030bca6ee322f2c0b070af01e55f to your computer and use it in GitHub Desktop.
[Unity] GameObjectを生成・再利用するクラス
This file contains hidden or 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.Generic; | |
/// <summary> | |
/// GameObjectを再利用するコンテイナー | |
/// </summary> | |
[System.Serializable] | |
public class PoolingContainer | |
{ | |
[SerializeField] | |
private GameObject _prefab; | |
[SerializeField] | |
private Transform _container; | |
[SerializeField] | |
private int _poolingOnAwake; | |
private List<GameObject> _list = new List<GameObject>(); | |
public List<GameObject> list {get{return _list;}} | |
/// <summary> | |
/// 指定した個数までオブジェクトを生成しておく | |
/// </summary> | |
public void Prepare() | |
{ | |
while(_list.Count < _poolingOnAwake) | |
{ | |
GenerateObject(); | |
} | |
} | |
/// <summary> | |
/// 全てのオブジェクトを回収する | |
/// </summary> | |
public void CollectAllObject() | |
{ | |
foreach (var obj in _list) | |
{ | |
obj.SetActive(false); | |
} | |
} | |
/// <summary> | |
/// activeがtrue/falseのオブジェクトを一つ取得する | |
/// </summary> | |
/// <param name="active"></param> | |
/// <returns></returns> | |
private GameObject Find(bool active) | |
{ | |
if (_list == null) _list = new List<GameObject>(); | |
return _list.Find(m => m.gameObject.activeSelf == active); | |
} | |
/// <summary> | |
/// activeがtrue/falseのオブジェクトリストを取得する | |
/// </summary> | |
/// <param name="active"></param> | |
/// <returns></returns> | |
private List<GameObject> FindList(bool active) | |
{ | |
if (_list == null) _list = new List<GameObject>(); | |
return _list.FindAll(m => m.gameObject.activeSelf == active); | |
} | |
/// <summary> | |
/// オブジェクトを取得する | |
/// </summary> | |
/// <returns></returns> | |
public GameObject GetObject() | |
{ | |
var myObject = Find(false); | |
if (myObject == null) | |
{ | |
myObject = GenerateObject(); | |
} | |
myObject.SetActive(true); | |
return myObject; | |
} | |
/// <summary> | |
/// アクティブなオブジェクトを取得する | |
/// </summary> | |
/// <returns></returns> | |
public List<GameObject> GetActiveObjects() | |
{ | |
return FindList(true); | |
} | |
/// <summary> | |
/// アクティブなオブジェクトを取得する | |
/// </summary> | |
/// <returns></returns> | |
public List<T> GetActiveObjects<T>() where T : Component | |
{ | |
var objects = GetActiveObjects(); | |
var list = new List<T>(); | |
foreach (var obj in objects) | |
{ | |
T component = obj.GetComponent<T>(); | |
if (component != null) | |
{ | |
list.Add(component); | |
} | |
} | |
return list; | |
} | |
/// <summary> | |
/// オブジェクトを取得する | |
/// </summary> | |
/// <returns></returns> | |
public T GetObject<T>() where T : Component | |
{ | |
return GetObject().GetComponent<T>(); | |
} | |
/// <summary> | |
/// オブジェクトを生成する | |
/// </summary> | |
/// <returns></returns> | |
private GameObject GenerateObject() | |
{ | |
var myObject = (GameObject)GameObject.Instantiate(_prefab); | |
myObject.SetActive(false); | |
myObject.transform.parent = _container; | |
myObject.name = _prefab.name + "_" + _list.Count; | |
myObject.transform.localPosition = Vector3.zero; | |
myObject.transform.localScale = _prefab.transform.localScale; | |
_list.Add(myObject); | |
return myObject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment