using UnityEngine; using System.Collections; using System.Collections.Generic; public class BulletFactory { public enum BulletType { Bullet = 1, } // public static GameObject CreateBullet(string path) // { // GameObject bulletPrefab = Resources.Load("Prefabs/Bullets/"+path); // if(bulletPrefab != null) // { // return GameObject.Instantiate(bulletPrefab); // } // // Debuger.LogWarning("Could not find bullet prefab["+path+"]"); // return new GameObject("Empty Bullet"); // } private static Dictionary> bulletCacheDict = new Dictionary>(); public static T CreateBullet(string path) { GameObject gameObj = null; if(bulletCacheDict.ContainsKey(path) && bulletCacheDict[path].Count > 0) { gameObj = bulletCacheDict[path][0]; bulletCacheDict[path].RemoveAt(0); gameObj.SetActive(true); ParticleSystem ps = gameObj.GetComponent(); if(ps != null) ps.Play(); Animator animator = gameObj.GetComponent(); if(animator != null) animator.Play(0, 0, 0); } else { GameObject bulletPrefab = Resources.Load("Prefabs/Bullets/"+path); if(bulletPrefab != null) { gameObj = GameObject.Instantiate(bulletPrefab); } else { Debuger.LogWarning("Could not find bullet prefab["+path+"]"); gameObj = new GameObject("Empty Bullet"); } gameObj.AddComponent(typeof(T)); } Bullet bullet = gameObj.GetComponent(); bullet.path = path; bullet.Reset(); return gameObj.GetComponent(); } public static void Recycle(string path, GameObject obj) { // Debuger.Log("Recycal Bullet["+path+"] "+obj); ParticleSystem ps = obj.GetComponent(); if(ps != null) ps.Stop(); obj.SetActive(false); if(bulletCacheDict.ContainsKey(path)) { bulletCacheDict[path].Add(obj); } else { List list = new List(); list.Add(obj); bulletCacheDict.Add(path, list); } } public static void ClearCache() { bulletCacheDict.Clear(); } }