Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- /// <summary>
- /// Used to scale objects. Other things can send scales here and they will be applied.
- /// That way multiple things can affect the scale of an object.
- /// </summary>
- public class Scaler : MonoBehaviour
- {
- [Tooltip("Object that will be scaled.")]
- public Transform ToScale;
- private Vector3 _initialScale;
- private IDictionary<object, float> _x;
- private IDictionary<object, float> _y;
- private IDictionary<object, float> _z;
- public Vector3 CurrentScale { get; private set; }
- void Start ()
- {
- if (ToScale == null)
- {
- ToScale = this.transform;
- }
- _initialScale = ToScale.localScale;
- _x = new Dictionary<object, float>();
- _y = new Dictionary<object, float>();
- _z = new Dictionary<object, float>();
- CurrentScale = Vector3.one;;
- }
- void Update ()
- {
- float x = 1;
- float y = 1;
- float z = 1;
- foreach (var value in _x.Values)
- {
- x *= value;
- }
- foreach (var value in _y.Values)
- {
- y *= value;
- }
- foreach (var value in _z.Values)
- {
- z *= value;
- }
- CurrentScale = new Vector3(
- _initialScale.x * x,
- _initialScale.y * y,
- _initialScale.z * z
- );
- if (ToScale != null)
- {
- ToScale.localScale = CurrentScale;
- }
- }
- #region Set
- /// <summary>
- /// Sets the values for scaling from the given object.
- /// </summary>
- /// <param name="key">Object who provided scaling.</param>
- /// <param name="values">Values for scaling.</param>
- public void Set(object key, Vector3 values)
- {
- SetX(key, values.x);
- SetY(key, values.y);
- SetZ(key, values.z);
- }
- /// <summary>
- /// Sets the value for scaling X from the given object.
- /// </summary>
- /// <param name="key">Object who provided scaling.</param>
- /// <param name="value">Value for scaling.</param>
- public void SetX(object key, float value)
- {
- Set(_x, key, value);
- }
- /// <summary>
- /// Sets the value for scaling Y from the given object.
- /// </summary>
- /// <param name="key">Object who provided scaling.</param>
- /// <param name="value">Value for scaling.</param>
- public void SetY(object key, float value)
- {
- Set(_y, key, value);
- }
- /// <summary>
- /// Sets the value for scaling Z from the given object.
- /// </summary>
- /// <param name="key">Object who provided scaling.</param>
- /// <param name="value">Value for scaling.</param>
- public void SetZ(object key, float value)
- {
- Set(_z, key, value);
- }
- private void Set(IDictionary<object, float> dictionary, object key, float value)
- {
- dictionary[key] = value;
- }
- #endregion
- #region Remove
- /// <summary>
- /// Removes the scaling from the given object.
- /// </summary>
- /// <param name="key">Object who provided scaling.</param>
- public void Remove(object key)
- {
- RemoveX(key);
- RemoveY(key);
- RemoveZ(key);
- }
- /// <summary>
- /// Removes the X scaling from the given object.
- /// </summary>
- /// <param name="key">Object who provided scaling.</param>
- public void RemoveX(object key)
- {
- Remove(_x, key);
- }
- /// <summary>
- /// Removes the Y scaling from the given object.
- /// </summary>
- /// <param name="key">Object who provided scaling.</param>
- public void RemoveY(object key)
- {
- Remove(_y, key);
- }
- /// <summary>
- /// Removes the Z scaling from the given object.
- /// </summary>
- /// <param name="key">Object who provided scaling.</param>
- public void RemoveZ(object key)
- {
- Remove(_z, key);
- }
- private void Remove(IDictionary<object, float> dictionary, object key)
- {
- dictionary.Remove(key);
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment