Ejemplo n.º 1
0
 // Get a DecalPool by DecalData
 private DecalPool GetPool(ScriptableDecal decalData)
 {
     for (int i = 0; i < pools.Count; i++)
     {
         if (pools[i].decalData == decalData)
         {
             return(pools[i]);
         }
     }
     return(InitializePool(decalData));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a new Decal directly. Does not handle Transform or Renderer setup.
        /// </summary>
        /// <param name="decalData">DecalData to set.</param>
        public static Decal CreateDecalDirect(ScriptableDecal decalData)
        {
            GameObject obj = new GameObject();

            obj.name = string.Format("Decal_{0}", decalData.name);
            Transform transform = obj.transform;

            transform.position   = Vector3.zero;
            transform.rotation   = Quaternion.identity;
            transform.localScale = Vector3.one;
            return(transform.gameObject.AddComponent <Decal>());
        }
Ejemplo n.º 3
0
        // -------------------------------------------------- //
        //                   PUBLIC METHODS                   //
        // -------------------------------------------------- //

        /// <summary>
        /// Try to get a Decal instance from pools.
        /// </summary>
        /// <param name="decalData">DecalData to get an instance of.</param>
        /// <param name="decal">Decal instance out.</param>
        public bool TryGetInstance(ScriptableDecal decalData, out Decal decal)
        {
            DecalPool pool = GetPool(decalData);

            ValidatePool(pool);
            for (int i = 0; i < pool.decals.Length; i++)
            {
                if (!pool.decals[i].gameObject.activeSelf)
                {
                    decal             = pool.decals[i];
                    pool.initTimes[i] = Time.realtimeSinceStartup;
                    return(true);
                }
            }
            decal = null;
            return(false);
        }
Ejemplo n.º 4
0
        // -------------------------------------------------- //
        //                  INTERNAL METHODS                  //
        // -------------------------------------------------- //

        // Initialize a new DecalPool
        private DecalPool InitializePool(ScriptableDecal decalData)
        {
            Decal[] decals   = new Decal[decalData.maxInstances];
            float[] initTime = new float[decalData.maxInstances];
            for (int i = 0; i < decals.Length; i++)
            {
                Decal decal = DecalSystem.CreateDecalDirect(decalData);
                decal.transform.localScale = this.transform.InverseTransformVector(Vector3.one);
                decal.transform.SetParent(this.transform);
                decals[i] = decal;
                decal.gameObject.SetActive(false);
            }

            DecalPool pool = new DecalPool(decalData, decals, initTime);

            pools.Add(pool);
            return(pool);
        }
Ejemplo n.º 5
0
        // -------------------------------------------------- //
        //                  INTERNAL METHODS                  //
        // -------------------------------------------------- //

        // Get a Decal instance, either from pooling or directly
        private static Decal GetDecalInstance(ScriptableDecal decalData, bool usePooling)
        {
            if (usePooling)
            {
                // Ensure there is an active DecalPooler
                DecalPooler decalPooler = GameObject.FindObjectOfType <DecalPooler>();
                if (decalPooler == null)
                {
                    CreateDecalPooler();
                }

                // Get a poolable Decal instance
                Decal decal;
                DecalPooler.Instance.TryGetInstance(decalData, out decal);
                return(decal);
            }
            else
            {
                // Create single Decal
                return(CreateDecalDirect(decalData));
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Set DecalData for the Decal and refresh its renderer.
 /// </summary>
 /// <param name="value">DecalData to set.</param>
 public void SetData(ScriptableDecal value)
 {
     m_DecalData = value;
     RefreshInternal();
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Get a Decal instance.
        /// </summary>
        /// <param name="positionWS">Decal position in World space.</param>
        /// <param name="rotationWS">Decal rotation in World space.</param>
        /// <param name="scaleWS">Decal scale in World space.</param>
        /// <param name="decalData">DecalData to set.</param>
        /// <param name="usePooling">If true the Decal will be taken from a DecalPooler instance.</param>
        public static Decal GetDecal(Vector3 positionWS, Quaternion rotationWS, Vector2 scaleWS, ScriptableDecal decalData, bool usePooling)
        {
            Decal decal = GetDecalInstance(decalData, usePooling);

            decal.SetActive(true);
            decal.SetTransform(positionWS, rotationWS, scaleWS);
            decal.SetData(decalData);
            return(decal);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Get a Decal instance.
 /// </summary>
 /// <param name="positionWS">Decal position in World space.</param>
 /// <param name="rotationWS">Decal rotation in World space.</param>
 /// <param name="decalData">DecalData to set.</param>
 /// <param name="usePooling">If true the Decal will be taken from a DecalPooler instance.</param>
 public static Decal GetDecal(Vector3 positionWS, Quaternion rotationWS, ScriptableDecal decalData, bool usePooling)
 {
     return(GetDecal(positionWS, rotationWS, Vector2.one, decalData, usePooling));
 }
Ejemplo n.º 9
0
 public DecalPool(ScriptableDecal decalData, Decal[] decals, float[] initTime)
 {
     this.decalData = decalData;
     this.decals    = decals;
     this.initTimes = initTime;
 }