using System.Collections; using System.Collections.Generic; using UnityEngine; public class ObjectCache : MonoBehaviour { public GameObject prefab; public int cacheSize = 20; private GameObject[] objects; private int cacheIndex = 0; void Initialize() { objects = new GameObject[cacheSize]; // Instantiate the objects in the array and set them to be inactive for (var i = 0; i < cacheSize; i++) { objects[i] = MonoBehaviour.Instantiate(prefab) as GameObject; objects[i].SetActive(false); objects[i].name = objects[i].name + i; } } public GameObject GetNextObjectInCache() { GameObject obj = null; // The cacheIndex starts out at the position of the object created // the longest time ago, so that one is usually free, // but in case not, loop through the cache until we find a free one. for (int i = 0; i < cacheSize; i++) { obj = objects[cacheIndex]; // If we found an inactive object in the cache, use that. if (!obj.activeSelf) break; // If not, increment index and make it loop around // if it exceeds the size of the cache cacheIndex = (cacheIndex + 1) % cacheSize; } // The object should be inactive. If it's not, log a warning and use // the object created the longest ago even though it's still active. if (obj.activeSelf) { Debug.LogWarning( "Spawn of " + prefab.name + " exceeds cache size of " + cacheSize + "! Reusing already active object.", obj); Destroy(obj.gameObject); } // Increment index and make it loop around // if it exceeds the size of the cache cacheIndex = (cacheIndex + 1) % cacheSize; return obj; } } public class DragFighter : MonoBehaviour { public GameObject bulletPrefab; public Transform spawnPoint; //public bool bShock; // public float coneAngle = 1.5f; private int count = 0; private double frequence = 0.5f; private GameObject go; //static SP spawner : Spawner; public ObjectCache[] caches; public Hashtable activeCachedObjects; float px, py; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 100)) { if (hit.transform.tag == "Background") { px = hit.point.x; py = hit.point.y; this.transform.position = new Vector3(px, py, 0); ; // audioSource.PlayOneShot(HitSound); Quaternion coneRandomRotation = Quaternion.Euler(0, 0, 0); go = Spawn(bulletPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotation); } } } } private GameObject Spawn(GameObject prefab, Vector3 position, Quaternion rotation) { ObjectCache cache = null; // Find the cache for the specified prefab // if (spawner) { for (int i = 0; i < caches.Length; i++) { if (caches[i].prefab == prefab) { cache = caches[i]; } } // } // If there's no cache for this prefab type, just instantiate normally if (cache == null) { return Instantiate(prefab, position, rotation) as GameObject; } // Find the next object in the cache GameObject obj = cache.GetNextObjectInCache(); // Set the position and rotation of the object obj.transform.position = position; obj.transform.rotation = rotation; // Set the object to be active obj.SetActive(true); activeCachedObjects[obj] = true; //return null; return obj; } }