/// <summary> /// Changes the state of the animation state machine. Doesn't check for valid transitions. /// </summary> /// <param name="state">New state of the animation.</param> private void SwitchState(State state) { this.state = state; switch (state) { case State.Active: if (_native != null) _native.Destroy(); _native = new NativeAnimation(); _native.OnEventTriggered += EventTriggered; animatedRenderable = SceneObject.GetComponent<Renderable>(); // Restore saved values after reset _native.WrapMode = serializableData.wrapMode; _native.Speed = serializableData.speed; _native.Cull = serializableData.cull; UpdateBounds(); if (serializableData.defaultClip != null) _native.Play(serializableData.defaultClip); primaryClip = _native.GetClip(0); if (primaryClip != null) RebuildFloatProperties(primaryClip); SetBoneMappings(); UpdateSceneObjectMapping(); if (animatedRenderable != null) animatedRenderable.RegisterAnimation(this); break; case State.EditorActive: if (_native != null) _native.Destroy(); _native = new NativeAnimation(); animatedRenderable = SceneObject.GetComponent<Renderable>(); UpdateBounds(); SetBoneMappings(); if (animatedRenderable != null) animatedRenderable.RegisterAnimation(this); break; case State.Inactive: if (animatedRenderable != null) animatedRenderable.UnregisterAnimation(); if (_native != null) { _native.Destroy(); _native = null; } primaryClip = null; mappingInfo.Clear(); floatProperties = null; break; } }
/// <summary> /// Builds a list of properties that will be animated using float animation curves. /// </summary> /// <param name="clip">Clip to retrieve the float animation curves from.</param> private void RebuildFloatProperties(AnimationClip clip) { if (clip == null) { floatProperties = null; return; } AnimationCurves curves = clip.Curves; List<FloatCurvePropertyInfo> newFloatProperties = new List<FloatCurvePropertyInfo>(); for (int i = 0; i < curves.FloatCurves.Length; i++) { bool isMorphCurve = curves.FloatCurves[i].Flags.HasFlag(AnimationCurveFlags.MorphWeight) || curves.FloatCurves[i].Flags.HasFlag(AnimationCurveFlags.MorphFrame); if (isMorphCurve) continue; string suffix; SerializableProperty property = FindProperty(SceneObject, curves.FloatCurves[i].Name, out suffix); if (property == null) continue; int elementIdx = 0; if (!string.IsNullOrEmpty(suffix)) { PropertySuffixInfo suffixInfo; if (PropertySuffixInfos.TryGetValue(suffix, out suffixInfo)) elementIdx = suffixInfo.elementIdx; } Action<float> setter = null; Type internalType = property.InternalType; switch (property.Type) { case SerializableProperty.FieldType.Vector2: if (internalType == typeof(Vector2)) { setter = f => { Vector2 value = property.GetValue<Vector2>(); value[elementIdx] = f; property.SetValue(value); }; } break; case SerializableProperty.FieldType.Vector3: if (internalType == typeof(Vector3)) { setter = f => { Vector3 value = property.GetValue<Vector3>(); value[elementIdx] = f; property.SetValue(value); }; } break; case SerializableProperty.FieldType.Vector4: if (internalType == typeof(Vector4)) { setter = f => { Vector4 value = property.GetValue<Vector4>(); value[elementIdx] = f; property.SetValue(value); }; } else if (internalType == typeof(Quaternion)) { setter = f => { Quaternion value = property.GetValue<Quaternion>(); value[elementIdx] = f; property.SetValue(value); }; } break; case SerializableProperty.FieldType.Color: if (internalType == typeof(Color)) { setter = f => { Color value = property.GetValue<Color>(); value[elementIdx] = f; property.SetValue(value); }; } break; case SerializableProperty.FieldType.Bool: setter = f => { bool value = f > 0.0f; property.SetValue(value); }; break; case SerializableProperty.FieldType.Int: setter = f => { int value = (int)f; property.SetValue(value); }; break; case SerializableProperty.FieldType.Float: setter = f => { property.SetValue(f); }; break; } if (setter == null) continue; FloatCurvePropertyInfo propertyInfo = new FloatCurvePropertyInfo(); propertyInfo.curveIdx = i; propertyInfo.setter = setter; newFloatProperties.Add(propertyInfo); } floatProperties = newFloatProperties.ToArray(); }