13 [RequireComponent(typeof(CompoundButton))]
16 const float AnimationSpeedMultiplier = 25f;
18 [Tooltip(
"Transform that scale and offset will be applied to.")]
22 [Tooltip(
"Mesh renderer button for mesh button.")]
48 public Color StateColor = Color.white;
52 public float StateValue = 0f;
68 private Material instantiatedMaterial;
73 private Material sharedMaterial;
76 private float lastTimePressed = 0f;
79 public void OnWillSaveScene ()
85 if (Renderer != null && instantiatedMaterial != null)
87 Renderer.sharedMaterial = sharedMaterial;
88 GameObject.DestroyImmediate(instantiatedMaterial);
95 Button button = GetComponent<Button>();
98 Debug.LogError(
"No button attached to CompoundButtonMesh in " + name);
105 Debug.LogError(
"No profile selected for CompoundButtonMesh in " + name);
112 enabled = Profile.SmoothStateChanges;
115 UpdateButtonProperties(
false);
125 lastTimePressed = Time.time;
128 currentDatum = Profile.ButtonStates[(int)newState];
131 if (!Profile.SmoothStateChanges)
133 TargetTransform.localScale = currentDatum.
Scale;
134 TargetTransform.localPosition = currentDatum.
Offset;
136 if (Renderer != null)
138 if (instantiatedMaterial == null)
140 sharedMaterial = Renderer.sharedMaterial;
141 instantiatedMaterial =
new Material(sharedMaterial);
142 Renderer.sharedMaterial = instantiatedMaterial;
145 if (!
string.IsNullOrEmpty(Profile.ColorPropertyName))
147 Renderer.sharedMaterial.SetColor(Profile.ColorPropertyName, currentDatum.
StateColor);
149 if (!
string.IsNullOrEmpty(Profile.ValuePropertyName))
151 Renderer.sharedMaterial.SetFloat(Profile.ValuePropertyName, currentDatum.
StateValue);
160 UpdateButtonProperties(
false);
164 Button button = GetComponent<Button>();
165 if (button != null) {
172 UpdateButtonProperties(
true);
177 if (currentDatum == null)
185 if (Profile.StickyPressedEvents && Time.time < lastTimePressed + Profile.StickyPressedTime)
190 if (TargetTransform != null)
194 TargetTransform.localScale = Vector3.Lerp(
195 TargetTransform.localScale, datum.
Scale,
196 Time.deltaTime * Profile.AnimationSpeed * AnimationSpeedMultiplier);
197 TargetTransform.localPosition = Vector3.Lerp(
198 TargetTransform.localPosition, datum.
Offset,
199 Time.deltaTime * Profile.AnimationSpeed * AnimationSpeedMultiplier);
202 TargetTransform.localScale = datum.
Scale;
203 TargetTransform.localPosition = datum.
Offset;
208 if (Renderer != null)
210 if (instantiatedMaterial == null)
212 sharedMaterial = Renderer.sharedMaterial;
213 instantiatedMaterial =
new Material(sharedMaterial);
214 Renderer.sharedMaterial = instantiatedMaterial;
217 if (!
string.IsNullOrEmpty(Profile.ColorPropertyName))
221 Renderer.sharedMaterial.SetColor(
222 Profile.ColorPropertyName,
223 Color.Lerp(Renderer.material.GetColor(Profile.ColorPropertyName),
225 Time.deltaTime * Profile.AnimationSpeed * AnimationSpeedMultiplier));
228 Renderer.sharedMaterial.SetColor(
229 Profile.ColorPropertyName,
233 if (!
string.IsNullOrEmpty(Profile.ValuePropertyName))
237 Renderer.sharedMaterial.SetFloat(
238 Profile.ValuePropertyName,
239 Mathf.Lerp(Renderer.material.GetFloat(Profile.ValuePropertyName),
241 Time.deltaTime * Profile.AnimationSpeed * AnimationSpeedMultiplier));
244 Renderer.sharedMaterial.SetFloat(Profile.ValuePropertyName, datum.
StateValue);
252 public class CustomEditor : MRTKEditor { }