12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- 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<GameObject>("Prefabs/Bullets/"+path);
- // if(bulletPrefab != null)
- // {
- // return GameObject.Instantiate<GameObject>(bulletPrefab);
- // }
- //
- // Debuger.LogWarning("Could not find bullet prefab["+path+"]");
- // return new GameObject("Empty Bullet");
- // }
- private static Dictionary<string, List<GameObject>> bulletCacheDict = new Dictionary<string, List<GameObject>>();
-
- public static T CreateBullet<T>(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<ParticleSystem>();
- if(ps != null)
- ps.Play();
- Animator animator = gameObj.GetComponent<Animator>();
- if(animator != null)
- animator.Play(0, 0, 0);
- }
- else
- {
- GameObject bulletPrefab = Resources.Load<GameObject>("Prefabs/Bullets/"+path);
- if(bulletPrefab != null)
- {
- gameObj = GameObject.Instantiate<GameObject>(bulletPrefab);
- }
- else
- {
- Debuger.LogWarning("Could not find bullet prefab["+path+"]");
- gameObj = new GameObject("Empty Bullet");
- }
- gameObj.AddComponent(typeof(T));
- }
- Bullet bullet = gameObj.GetComponent<Bullet>();
- bullet.path = path;
- bullet.Reset();
- return gameObj.GetComponent<T>();
- }
-
- public static void Recycle(string path, GameObject obj)
- {
- // Debuger.Log("Recycal Bullet["+path+"] "+obj);
- ParticleSystem ps = obj.GetComponent<ParticleSystem>();
- if(ps != null)
- ps.Stop();
- obj.SetActive(false);
- if(bulletCacheDict.ContainsKey(path))
- {
- bulletCacheDict[path].Add(obj);
- }
- else
- {
- List<GameObject> list = new List<GameObject>();
- list.Add(obj);
- bulletCacheDict.Add(path, list);
- }
- }
-
- public static void ClearCache()
- {
- bulletCacheDict.Clear();
- }
- }
|