using System.Collections; using System.Collections.Generic; using UnityEngine; public class ObjectCache : MonoBehaviour { public GameObject prefab; public int cacheSize = 10; 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 SpawnHelicopter : 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; // Use this for initialization void Start() { } // Update is called once per frame void Update() { if (Time.time > (count * frequence)) { // if (bShock) // { //Quaternion coneRandomRotation = Quaternion.Euler(Random.Range(-coneAngle, coneAngle), Random.Range(-coneAngle, coneAngle), 0); Quaternion coneRandomRotation = Quaternion.Euler(0, 0, 0); go = Spawn(bulletPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotation); // } // else // { // go = Spawn(bulletPrefab, spawnPoint.position, spawnPoint.rotation); // } count++; } } 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; } }