Guest User

Scaler

a guest
Jun 12th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.12 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. /// <summary>
  6. /// Used to scale objects. Other things can send scales here and they will be applied.
  7. /// That way multiple things can affect the scale of an object.
  8. /// </summary>
  9. public class Scaler : MonoBehaviour
  10. {
  11.     [Tooltip("Object that will be scaled.")]
  12.     public Transform ToScale;
  13.  
  14.     private Vector3 _initialScale;
  15.     private IDictionary<object, float> _x;
  16.     private IDictionary<object, float> _y;
  17.     private IDictionary<object, float> _z;
  18.  
  19.     public Vector3 CurrentScale { get; private set; }
  20.  
  21.     void Start ()
  22.     {
  23.         if (ToScale == null)
  24.         {
  25.             ToScale = this.transform;
  26.         }
  27.  
  28.         _initialScale = ToScale.localScale;
  29.  
  30.         _x = new Dictionary<object, float>();
  31.         _y = new Dictionary<object, float>();
  32.         _z = new Dictionary<object, float>();
  33.  
  34.         CurrentScale = Vector3.one;;
  35.     }
  36.    
  37.     void Update ()
  38.     {
  39.         float x = 1;
  40.         float y = 1;
  41.         float z = 1;
  42.  
  43.         foreach (var value in _x.Values)
  44.         {
  45.             x *= value;
  46.         }
  47.         foreach (var value in _y.Values)
  48.         {
  49.             y *= value;
  50.         }
  51.         foreach (var value in _z.Values)
  52.         {
  53.             z *= value;
  54.         }
  55.  
  56.         CurrentScale = new Vector3(
  57.             _initialScale.x * x,
  58.             _initialScale.y * y,
  59.             _initialScale.z * z
  60.         );
  61.  
  62.         if (ToScale != null)
  63.         {
  64.             ToScale.localScale = CurrentScale;
  65.         }
  66.     }
  67.  
  68.     #region Set
  69.  
  70.     /// <summary>
  71.     /// Sets the values for scaling from the given object.
  72.     /// </summary>
  73.     /// <param name="key">Object who provided scaling.</param>
  74.     /// <param name="values">Values for scaling.</param>
  75.     public void Set(object key, Vector3 values)
  76.     {
  77.         SetX(key, values.x);
  78.         SetY(key, values.y);
  79.         SetZ(key, values.z);
  80.     }
  81.  
  82.     /// <summary>
  83.     /// Sets the value for scaling X from the given object.
  84.     /// </summary>
  85.     /// <param name="key">Object who provided scaling.</param>
  86.     /// <param name="value">Value for scaling.</param>
  87.     public void SetX(object key, float value)
  88.     {
  89.         Set(_x, key, value);
  90.     }
  91.  
  92.     /// <summary>
  93.     /// Sets the value for scaling Y from the given object.
  94.     /// </summary>
  95.     /// <param name="key">Object who provided scaling.</param>
  96.     /// <param name="value">Value for scaling.</param>
  97.     public void SetY(object key, float value)
  98.     {
  99.         Set(_y, key, value);
  100.     }
  101.  
  102.     /// <summary>
  103.     /// Sets the value for scaling Z from the given object.
  104.     /// </summary>
  105.     /// <param name="key">Object who provided scaling.</param>
  106.     /// <param name="value">Value for scaling.</param>
  107.     public void SetZ(object key, float value)
  108.     {
  109.         Set(_z, key, value);
  110.     }
  111.  
  112.     private void Set(IDictionary<object, float> dictionary, object key, float value)
  113.     {
  114.         dictionary[key] = value;
  115.     }
  116.  
  117.     #endregion
  118.  
  119.     #region Remove
  120.  
  121.     /// <summary>
  122.     /// Removes the scaling from the given object.
  123.     /// </summary>
  124.     /// <param name="key">Object who provided scaling.</param>
  125.     public void Remove(object key)
  126.     {
  127.         RemoveX(key);
  128.         RemoveY(key);
  129.         RemoveZ(key);
  130.     }
  131.  
  132.     /// <summary>
  133.     /// Removes the X scaling from the given object.
  134.     /// </summary>
  135.     /// <param name="key">Object who provided scaling.</param>
  136.     public void RemoveX(object key)
  137.     {
  138.         Remove(_x, key);
  139.     }
  140.  
  141.     /// <summary>
  142.     /// Removes the Y scaling from the given object.
  143.     /// </summary>
  144.     /// <param name="key">Object who provided scaling.</param>
  145.     public void RemoveY(object key)
  146.     {
  147.         Remove(_y, key);
  148.     }
  149.  
  150.     /// <summary>
  151.     /// Removes the Z scaling from the given object.
  152.     /// </summary>
  153.     /// <param name="key">Object who provided scaling.</param>
  154.     public void RemoveZ(object key)
  155.     {
  156.         Remove(_z, key);
  157.     }
  158.  
  159.     private void Remove(IDictionary<object, float> dictionary, object key)
  160.     {
  161.         dictionary.Remove(key);
  162.     }
  163.  
  164.     #endregion
  165. }
Advertisement
Add Comment
Please, Sign In to add comment