/// <summary> /// Adds a CrayonState to the specified GameObject, then loads in data on /// transitions and properties from the CrayonStateData class. /// </summary> /// <param name="g">The green component.</param> public void LoadData(GameObject gameObject) { CrayonState state = gameObject.AddComponent <CrayonState> (); state._crayonStateType = _crayonStateType; state._customStateType = _customStateType; state._crayonMatchKey = _crayonMatchKey; state._duration = _duration; state._easing = _easing; state._customEasing = _customEasing; state._tweenAppearance = _tweenAppearance; state._tweenAppearanceMode = _tweenAppearanceMode; state._material = _material; state._color = _color; state._tweenPosition = _tweenPosition; state._relativePosition = _relativePosition; state._tweenRotation = _tweenRotation; state._relativeRotation = _relativeRotation; state._tweenScale = _tweenScale; state._relativeScale = _relativeScale; }
/// <summary> /// Saves data on transitions and properties from a CrayonState. /// </summary> public void SaveData(CrayonState state) { _crayonStateType = state._crayonStateType; _customStateType = state._customStateType; _crayonMatchKey = state._crayonMatchKey; _duration = state._duration; _easing = state._easing; _customEasing = state._customEasing; _tweenAppearance = state._tweenAppearance; _tweenAppearanceMode = state._tweenAppearanceMode; _material = state._material; _color = state._color; _opacity = state._opacity; _tweenPosition = state._tweenPosition; _relativePosition = state._relativePosition; _tweenRotation = state._tweenRotation; _relativeRotation = state._relativeRotation; _tweenScale = state._tweenScale; _relativeScale = state._relativeScale; }
/// <summary> /// This initializes the dictionary used by the State Manager to recall and access specific states. /// </summary> public void InitDictionary() { _allStatesByMatchKey = new Dictionary <string, CrayonState> (); CrayonState[] states = GetComponents <CrayonState> (); foreach (CrayonState state in states) { CrayonState check; if (!_allStatesByMatchKey.TryGetValue(state._crayonMatchKey, out check)) { _allStatesByMatchKey.Add(state._crayonMatchKey, state); } else { Debug.LogWarningFormat("There are multiple states of the same type on {0}. Please check your states.", this.gameObject.name); } } _currentState = _allStatesByMatchKey["default"]; }
/// <summary> /// Begins a new transition between states. /// </summary> public void ChangeState(CrayonStateType stateType, string customState = "") { // Call the delegate so any downstream items know to change as well. if (OnChangeState != null) { OnChangeState(stateType, customState); } // Find the state by match key string matchKey = stateType.ToString().ToLower() + customState; // Don't do anything if we're already on the right state if (matchKey == _currentMatchKey) { return; } CrayonState state; _allStatesByMatchKey.TryGetValue(matchKey, out state); if (state != null) { if (state._tweenAppearance) { // Determine the tween method based on the enum selected switch (state._tweenAppearanceMode) { case CrayonTweenAppearanceMode.Material: gameObject.SetMaterial(state._material, state._duration, state._easing, state._customEasing); break; case CrayonTweenAppearanceMode.Color: gameObject.SetColor(state._color, state._duration, state._easing, state._customEasing); break; } } if (state._tweenPosition) { Vector3 relativePosition = new Vector3(_originalPosition.x + state._relativePosition.x, _originalPosition.y + state._relativePosition.y, _originalPosition.z + state._relativePosition.z); gameObject.SetPosition(relativePosition, state._duration, state._easing, state._customEasing); } if (state._tweenRotation) { Vector3 relativeRotation = new Vector3(_originalRotation.eulerAngles.x + state._relativeRotation.x, _originalRotation.eulerAngles.y + state._relativeRotation.y, _originalRotation.eulerAngles.z + state._relativeRotation.z); gameObject.SetRotation(relativeRotation, state._duration, state._easing, state._customEasing); } if (state._tweenScale) { Vector3 relativeScale = new Vector3(_originalScale.x * state._relativeScale.x, _originalScale.y * state._relativeScale.y, _originalScale.z * state._relativeScale.z); gameObject.SetScale(relativeScale, state._duration, state._easing, state._customEasing); } // Update the StateManager to reflect the new CrayonState. _currentMatchKey = matchKey; _currentState = state; } else { Debug.LogWarning("State '" + stateType + "' has not been assigned for " + gameObject.name); } }
/// <summary> /// Constructor: Create a new CrayonStateData instance from a CrayonState. /// </summary> public CrayonStateData(CrayonState stateToSave) { SaveData(stateToSave); }