Ejemplo n.º 1
0
        /// <summary>
        /// Destroy cache GameObject and add to free list
        /// </summary>
        /// <param name="objectToDestroy">GameObject with CacheBehavior component</param>
        /// <remarks>
        /// If GameObject has not a CacheBehavior component, spawner destroy it normally ( by GameObject.Destroy method )
        /// </remarks>
        public static void DestroyCache(GameObject objectToDestroy)
        {
            if (objectToDestroy == null)
            {
                return;                          // maybe engine destroy it before
            }
            CacheObject cache = GetCacheObject(objectToDestroy);

            if (cache != null)
            {
                CacheBehavior cacheBehavior = objectToDestroy.GetComponent <CacheBehavior>();
                if (!cacheBehavior.IsCached)
                {
                    cacheBehavior.IsCached = true;
                    cache.Free(objectToDestroy);

                    EventManager eventManager = objectToDestroy.GetComponent <EventManager>();
                    if (eventManager != null)
                    {
                        eventManager.RaiseCached(null, new CacheEventArgs(cache.CacheId));
                    }
                }
            }
            else
            {
                GameObject.Destroy(objectToDestroy);
            }
        }
Ejemplo n.º 2
0
 private static CacheObject GetCacheObject(GameObject prefab)
 {
     try
     {
         CacheBehavior cacheBehavior = prefab.GetComponent <CacheBehavior>();
         if (cacheBehavior != null)
         {
             if (cacheBehavior.Group != null)
             {
                 foreach (var item in cacheBehavior.Group.Caches)
                 {
                     if (item.CacheId == cacheBehavior.CacheId)
                     {
                         return(item);
                     }
                 }
             }
         }
     }
     catch (MissingReferenceException)
     {
         Debug.LogWarning("MissingReference of " + prefab.name);
         throw;
     }
     return(null);
 }
Ejemplo n.º 3
0
        private void SetCacheIdAndGroup(GameObject obj)
        {
            CacheBehavior cacheable = obj.GetComponent <CacheBehavior>();

            if (cacheable != null)
            {
                cacheable.CacheId = this.CacheId;
                cacheable.Group   = Group;
            }
            else
            {
                throw new Exception("The prefab you set for cache does not have CacheBehavior component : " + obj.name);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Spawn a cache object
        /// </summary>
        /// <param name="prefab">GameObject with CacheBehavior component</param>
        /// <param name="position">Position</param>
        /// <param name="rotation">Rotation</param>
        /// <param name="active">Activate at spawn time or let caller activate it self</param>
        /// <returns>Spawned GameObject</returns>
        /// <remarks>
        /// If GameObject has not a CacheBehavior component, spawner instantiate it normally ( by GameObject.Instantiate method )
        /// </remarks>
        public static GameObject Spawn(GameObject prefab, Vector3 position, Quaternion rotation, bool active = true)
        {
            if (prefab == null)
            {
                Debug.LogError("Invalid GameObject to spawn");
                return(null);
            }

            GameObject  obj   = null;
            CacheObject cache = GetCacheObject(prefab);

            // If there's no cache for this prefab type, just instantiate normally
            if (cache == null)
            {
                obj = GameObject.Instantiate(prefab, position, rotation) as GameObject;
            }
            else
            {
                // Find the next object in the cache
                obj = cache.Next();
                CacheBehavior cacheBehavior = obj.GetComponent <CacheBehavior>();
                cacheBehavior.IsCached = false;

                try
                {
                    // Set the position and rotation of the object
                    obj.transform.position = position;
                    obj.transform.rotation = rotation;
                }
                catch (MissingReferenceException)
                {
                    Debug.LogWarning(string.Format("MissingReferenceException when spawn prefab.name : {0}, catchId : {1}.", prefab.name, cache.CacheId));
                    throw;
                }
            }

            if (obj != null && active)
            {// Set the object to be active
                obj.SetActive(active);
            }
            return(obj);
        }