using UnityEngine; namespace Lean.Common { /// This component allows you to rescale the current GameObject using events. [HelpURL(LeanHelper.PlusHelpUrlPrefix + "LeanManualRescale")] [AddComponentMenu(LeanHelper.ComponentPathPrefix + "Manual Rescale")] public class LeanManualRescale : MonoBehaviour { /// If you want this component to work on a different GameObject, then specify it here. This can be used to improve organization if your GameObject already has many components. public GameObject Target { set { target = value; } get { return target; } } [SerializeField] private GameObject target; /// The first scale axes, used when calling ScaleA or ScaleAB. public Vector3 AxesA { set { axesA = value; } get { return axesA; } } [SerializeField] private Vector3 axesA = Vector3.right; /// The second scale axes, used when calling ScaleB or ScaleAB. public Vector3 AxesB { set { axesB = value; } get { return axesB; } } [SerializeField] private Vector3 axesB = Vector3.up; /// The scale value is multiplied by this. /// 1 = Normal distance. /// 2 = Double distance. public float Multiplier { set { multiplier = value; } get { return multiplier; } } [SerializeField] private float multiplier = 1.0f; /// If you want this component to change smoothly over time, then this allows you to control how quick the changes reach their target value. /// -1 = Instantly change. /// 1 = Slowly change. /// 10 = Quickly change. public float Damping { set { damping = value; } get { return damping; } } [SerializeField] private float damping = 10.0f; /// If you enable this then the scale will be multiplied by Time.deltaTime. This allows you to maintain frame rate independent movement. public bool ScaleByTime { set { scaleByTime = value; } get { return scaleByTime; } } [SerializeField] private bool scaleByTime; /// If you call the ResetScale method, the scale will be set to this. public Vector3 DefaultScale { set { defaultScale = value; } get { return defaultScale; } } [SerializeField] private Vector3 defaultScale = Vector3.one; [SerializeField] private Vector3 remainingDelta; /// This method will reset the scale to the specified DefaultScale value. [ContextMenu("Reset Scale")] public void ResetScale() { var finalTransform = target != null ? target.transform : transform; remainingDelta = defaultScale - finalTransform.localScale; } /// This method will cause the scale to immediately snap to its final value. [ContextMenu("Snap To Target")] public void SnapToTarget() { UpdateScale(1.0f); } /// This method allows you to scale by AxesA, with the specified multiplier. public void AddScaleA(float magnitude) { AddScale(axesA * magnitude); } /// This method allows you to scale by AxesB, with the specified multiplier. public void AddScaleB(float magnitude) { AddScale(axesB * magnitude); } /// This method allows you to scale by AxesA and AxesB, with the specified multipliers. public void AddScaleAB(Vector2 magnitude) { AddScale(axesA * magnitude.x + axesB * magnitude.y); } /// This method allows you to scale by the specified vector in local space. public void AddScale(Vector3 vector) { if (scaleByTime == true) { vector *= Time.deltaTime; } remainingDelta += vector * multiplier; } protected virtual void Update() { var factor = LeanHelper.GetDampenFactor(damping, Time.deltaTime); UpdateScale(factor); } private void UpdateScale(float factor) { var finalTransform = target != null ? target.transform : transform; var newDelta = Vector3.Lerp(remainingDelta, Vector3.zero, factor); finalTransform.localScale += remainingDelta - newDelta; remainingDelta = newDelta; } } } #if UNITY_EDITOR namespace Lean.Common.Editor { using TARGET = LeanManualRescale; [UnityEditor.CustomEditor(typeof(TARGET))] public class LeanManualRescale_Editor : LeanEditor { protected override void OnInspector() { TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts); Draw("target", "If you want this component to work on a different GameObject, then specify it here. This can be used to improve organization if your GameObject already has many components."); Draw("axesA", "The first scale axes, used when calling ScaleA or ScaleAB."); Draw("axesB", "The second scale axes, used when calling ScaleB or ScaleAB."); Separator(); Draw("multiplier", "The scale value is multiplied by this.\n\n1 = Normal distance.\n\n2 = Double distance."); Draw("scaleByTime", "If you enable this then the scale will be multiplied by Time.deltaTime. This allows you to maintain frame rate independent movement."); Draw("damping", "If you want this component to change smoothly over time, then this allows you to control how quick the changes reach their target value.\n\n-1 = Instantly change.\n\n1 = Slowly change.\n\n10 = Quickly change."); Draw("defaultScale", "If you call the ResetPosition method, the position will be set to this."); } } } #endif