/// <summary> /// On state change swap out the active mesh based on the state /// </summary> public override void OnStateChange(ButtonStateEnum newState) { MeshButtonDatum stateDatum = ButtonStates[(int)newState]; // if filter or renderer are null set them if (_meshFilter == null) { _meshFilter = this.GetComponent <MeshFilter>(); } if (_renderer == null) { _renderer = this.GetComponent <MeshRenderer>(); } if (_animator == null) { _animator = this.GetComponent <Animator>(); } // Play animator state if (UseAnimator) { _animator.Play(stateDatum.Name); } // Set the color from the datum if (_renderer != null) { _renderer.material.color = stateDatum.StateColor; } base.OnStateChange(newState); }
protected void Start() { Button button = GetComponent <Button>(); if (button == null) { Debug.LogError("No button attached to CompoundButtonMesh in " + name); enabled = false; return; } if (Profile == null) { Debug.LogError("No profile selected for CompoundButtonMesh in " + name); enabled = false; return; } button.StateChange += StateChange; // Disable this script if we're not using smooth changes enabled = Profile.SmoothStateChanges; // Set the current datum so our first state is activated currentDatum = Profile.ButtonStates[(int)ButtonStateEnum.Observation]; UpdateButtonProperties(false); }
/// <summary> /// On state change swap out the active mesh based on the state /// </summary> protected void StateChange(ButtonStateEnum newState) { if (newState == ButtonStateEnum.Pressed) { lastTimePressed = Time.time; } currentDatum = Profile.ButtonStates[(int)newState]; // If we're not using smooth states, just set them now if (!Profile.SmoothStateChanges) { TargetTransform.localScale = currentDatum.Scale; TargetTransform.localPosition = currentDatum.Offset; if (Renderer != null) { if (instantiatedMaterial == null) { sharedMaterial = Renderer.sharedMaterial; instantiatedMaterial = new Material(sharedMaterial); Renderer.sharedMaterial = instantiatedMaterial; } if (!string.IsNullOrEmpty(Profile.ColorPropertyName)) { Renderer.sharedMaterial.SetColor(Profile.ColorPropertyName, currentDatum.StateColor); } if (!string.IsNullOrEmpty(Profile.ValuePropertyName)) { Renderer.sharedMaterial.SetFloat(Profile.ValuePropertyName, currentDatum.StateValue); } } } }
protected void UpdateButtonProperties(bool smooth) { if (currentDatum == null) { return; } MeshButtonDatum datum = currentDatum; // If we're using sticky events, and we're still not past the 'sticky' pressed time, use that datum if (Profile.StickyPressedEvents && Time.time < lastTimePressed + Profile.StickyPressedTime) { datum = Profile.ButtonStates[(int)ButtonStateEnum.Pressed]; } if (TargetTransform != null) { if (smooth) { TargetTransform.localScale = Vector3.Lerp( TargetTransform.localScale, datum.Scale, Time.deltaTime * Profile.AnimationSpeed * AnimationSpeedMultiplier); TargetTransform.localPosition = Vector3.Lerp( TargetTransform.localPosition, datum.Offset, Time.deltaTime * Profile.AnimationSpeed * AnimationSpeedMultiplier); } else { TargetTransform.localScale = datum.Scale; TargetTransform.localPosition = datum.Offset; } } // Set the color from the datum if (Renderer != null) { if (instantiatedMaterial == null) { sharedMaterial = Renderer.sharedMaterial; instantiatedMaterial = new Material(sharedMaterial); Renderer.sharedMaterial = instantiatedMaterial; } if (!string.IsNullOrEmpty(Profile.ColorPropertyName)) { if (smooth) { Renderer.sharedMaterial.SetColor( Profile.ColorPropertyName, Color.Lerp(Renderer.material.GetColor(Profile.ColorPropertyName), datum.StateColor, Time.deltaTime * Profile.AnimationSpeed * AnimationSpeedMultiplier)); } else { Renderer.sharedMaterial.SetColor( Profile.ColorPropertyName, datum.StateColor); } } if (!string.IsNullOrEmpty(Profile.ValuePropertyName)) { if (smooth) { Renderer.sharedMaterial.SetFloat( Profile.ValuePropertyName, Mathf.Lerp(Renderer.material.GetFloat(Profile.ValuePropertyName), datum.StateValue, Time.deltaTime * Profile.AnimationSpeed * AnimationSpeedMultiplier)); } else { Renderer.sharedMaterial.SetFloat(Profile.ValuePropertyName, datum.StateValue); } } } }