Tilt Five™ Unity API  1.4.1
UniformScaleTransform.cs
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020-2023 Tilt Five, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 using UnityEngine;
18 
19 namespace TiltFive
20 {
24  [RequireComponent(typeof(Transform))]
25  [ExecuteInEditMode]
26  public class UniformScaleTransform : MonoBehaviour
27  {
28  #region Public Fields
29 
33  public float localScale
34  {
35  get => transform.localScale.x;
36  set
37  {
38  base.transform.localScale = Vector3.one * value;
39  _previousScale = base.transform.localScale;
40  }
41  }
42 
46  public Vector3 position
47  {
48  get => transform.position;
49  set => transform.position = value;
50  }
51 
55  public Quaternion rotation
56  {
57  get => transform.rotation;
58  set => base.transform.rotation = value;
59  }
60 
61  #endregion Public Fields
62 
63 
64  #region Private Fields
65 
66  private Vector3 _previousScale;
67 
68  #endregion Private Fields
69 
70 
71  #region Private Functions
72 
81  protected void UnifyScale()
82  {
83  // Compare the current scale against the previous scale.
84  if (transform.localScale == _previousScale)
85  {
86  return;
87  }
88 
89  // Get the component that changed the most, and set the scale to that value.
90  var deltaScale = transform.localScale - _previousScale;
91  var largestPositiveChange = Mathf.Max(deltaScale.x, deltaScale.y, deltaScale.z);
92  var largestNegativeChange = Mathf.Min(deltaScale.x, deltaScale.y, deltaScale.z);
93  var largestAbsoluteChange = Mathf.Abs(largestPositiveChange) > Mathf.Abs(largestNegativeChange)
94  ? largestPositiveChange
95  : largestNegativeChange;
96 
97  transform.localScale = _previousScale + Vector3.one * largestAbsoluteChange;
98  _previousScale = transform.localScale;
99  }
100 
101  #endregion Private Functions
102 
103 
104  #region Unity Functions
105 
106  public void Awake()
107  {
108  // Initial pass, in case the transform's scale is already non-uniform before UniformScaleTransform is initialized.
109  _previousScale = Vector3.one;
110  UnifyScale();
111 
112  _previousScale = transform.localScale;
113  }
114 
115  // Update is called once per frame
116  void Update()
117  {
118  UnifyScale();
119  }
120 
121  #endregion Unity Functions
122  }
123 }
Enforces uniform scaling by setting the x, y, and z scale components of the associated transform to b...
float localScale
The size of the object as a single float value, rather than a scale vector.
Vector3 position
The position vector for the associated transform.
Quaternion rotation
The rotation vector for the associated transform.
void UnifyScale()
Synchronizes the component values of the game object's local scale vector (e.g. [1,...
Definition: Log.cs:21