Exemplo n.º 1
0
        public override void previewFrame(AMITarget target, float frame, int frameRate, bool play, float playSpeed)
        {
            if (keys == null || keys.Count <= 0)
            {
                return;
            }

            GameObject go   = GetTarget(target) as GameObject;
            Component  comp = GetTargetComp(target);

            if (!comp || !go)
            {
                return;
            }

            if (!isCached)
            {
                RefreshData(comp);
            }

            // if before or equal to first frame, or is the only frame
            AMPropertyKey firstKey = keys[0] as AMPropertyKey;

            if (firstKey.endFrame == -1 || (frame <= (float)firstKey.frame && !firstKey.canTween))
            {
                //go.rotation = (cache[0] as AMPropertyAction).getStartQuaternion();
                setComponentValueFromCachedInfo(comp, firstKey.getValue(valueType));
                refreshTransform(go);
                return;
            }

            // if lies on property action
            for (int i = 0; i < keys.Count; i++)
            {
                AMPropertyKey key     = keys[i] as AMPropertyKey;
                AMPropertyKey keyNext = i + 1 < keys.Count ? keys[i + 1] as AMPropertyKey : null;

                if (frame >= (float)key.endFrame && keyNext != null && (!keyNext.canTween || keyNext.endFrame != -1))
                {
                    continue;
                }
                // if no ease
                if (!key.canTween || keyNext == null)
                {
                    setComponentValueFromCachedInfo(comp, key.getValue(valueType));
                    refreshTransform(go);
                    return;
                }
                // else find value using easing function

                float numFrames = (float)key.getNumberOfFrames(frameRate);

                float framePositionInAction = Mathf.Clamp(frame - (float)key.frame, 0f, numFrames);

                float t;

                if (key.hasCustomEase())
                {
                    t = Utility.EaseCustom(0.0f, 1.0f, framePositionInAction / key.getNumberOfFrames(frameRate), key.easeCurve);
                }
                else
                {
                    var ease = Utility.GetEasingFunction((Ease)key.easeType);
                    t = ease(framePositionInAction, key.getNumberOfFrames(frameRate), key.amplitude, key.period);
                }

                //qCurrent.x = ease(qStart.x,qEnd.x,percentage);
                switch ((ValueType)valueType)
                {
                case ValueType.Integer:
                    setComponentValueFromCachedInfo(comp, keyNext ? Mathf.RoundToInt(Mathf.Lerp(Convert.ToSingle(key.val), Convert.ToSingle(keyNext.val), t)) : Convert.ToInt32(key.val));
                    break;

                case ValueType.Long:
                    setComponentValueFromCachedInfo(comp, keyNext ? (long)Mathf.RoundToInt(Mathf.Lerp(Convert.ToSingle(key.val), Convert.ToSingle(keyNext.val), t)) : Convert.ToInt64(key.val));
                    break;

                case ValueType.Float:
                    setComponentValueFromCachedInfo(comp, keyNext ? Mathf.Lerp(Convert.ToSingle(key.val), Convert.ToSingle(keyNext.val), t) : Convert.ToSingle(key.val));
                    break;

                case ValueType.Double:
                    setComponentValueFromCachedInfo(comp, keyNext ? key.val + ((double)t) * (keyNext.val - key.val) : key.val);
                    break;

                case ValueType.Vector2:
                    setComponentValueFromCachedInfo(comp, keyNext ? Vector2.Lerp(key.vect2, keyNext.vect2, t) : key.vect2);
                    break;

                case ValueType.Vector3:
                    setComponentValueFromCachedInfo(comp, keyNext ? Vector3.Lerp(key.vect3, keyNext.vect3, t) : key.vect3);
                    break;

                case ValueType.Color:
                    setComponentValueFromCachedInfo(comp, keyNext ? Color.Lerp(key.color, keyNext.color, t) : key.color);
                    break;

                case ValueType.Rect:
                    if (keyNext)
                    {
                        Rect vStartRect   = key.rect;
                        Rect vEndRect     = keyNext.rect;
                        Rect vCurrentRect = new Rect();
                        vCurrentRect.x      = Mathf.Lerp(vStartRect.x, vEndRect.x, t);
                        vCurrentRect.y      = Mathf.Lerp(vStartRect.y, vEndRect.y, t);
                        vCurrentRect.width  = Mathf.Lerp(vStartRect.width, vEndRect.width, t);
                        vCurrentRect.height = Mathf.Lerp(vStartRect.height, vEndRect.height, t);
                        setComponentValueFromCachedInfo(comp, vCurrentRect);
                    }
                    else
                    {
                        setComponentValueFromCachedInfo(comp, key.rect);
                    }
                    break;

                case ValueType.Vector4:
                    setComponentValueFromCachedInfo(comp, keyNext ? Vector4.Lerp(key.vect4, keyNext.vect4, t) : key.vect4);
                    break;

                case ValueType.Quaternion:
                    setComponentValueFromCachedInfo(comp, keyNext ? Quaternion.Slerp(key.quat, keyNext.quat, t) : key.quat);
                    break;

                default:
                    Debug.LogError("Animator: Invalid ValueType " + valueType.ToString());
                    break;
                }
                refreshTransform(go);
                return;
            }
        }
Exemplo n.º 2
0
        public string getValueString(System.Type type, int valueType, AMPropertyKey nextKey, bool brief)
        {
            System.Text.StringBuilder s = new System.Text.StringBuilder();

            if (AMPropertyTrack.isValueTypeNumeric(valueType))
            {
                //s+= start_val.ToString();
                s.Append(formatNumeric(val));
                if (!brief && nextKey)
                {
                    s.Append(" -> "); s.Append(formatNumeric(nextKey.val));
                }
                //if(!brief && endFrame != -1) s += " -> "+end_val.ToString();
            }
            else if (valueType == (int)AMPropertyTrack.ValueType.Bool)
            {
                s.Append(val > 0.0 ? "(true)" : "(false)");
            }
            else if (valueType == (int)AMPropertyTrack.ValueType.String)
            {
                s.AppendFormat("\"{0}\"", valString);
            }
            else if (valueType == (int)AMPropertyTrack.ValueType.Vector2)
            {
                s.Append(vect2.ToString());
                if (!brief && nextKey)
                {
                    s.Append(" -> "); s.Append(nextKey.vect2.ToString());
                }
            }
            else if (valueType == (int)AMPropertyTrack.ValueType.Vector3)
            {
                s.Append(vect3.ToString());
                if (!brief && nextKey)
                {
                    s.Append(" -> "); s.Append(nextKey.vect3.ToString());
                }
            }
            else if (valueType == (int)AMPropertyTrack.ValueType.Color)
            {
                //return null;
                s.Append(color.ToString());
                if (!brief && nextKey)
                {
                    s.Append(" -> "); s.Append(nextKey.color.ToString());
                }
            }
            else if (valueType == (int)AMPropertyTrack.ValueType.Rect)
            {
                //return null;
                s.Append(rect.ToString());
                if (!brief && nextKey)
                {
                    s.Append(" -> "); s.Append(nextKey.rect.ToString());
                }
            }
            else if (valueType == (int)AMPropertyTrack.ValueType.Vector4)
            {
                s.Append(vect4.ToString());
                if (!brief && nextKey)
                {
                    s.Append(" -> "); s.Append(nextKey.vect4.ToString());
                }
            }
            else if (valueType == (int)AMPropertyTrack.ValueType.Quaternion)
            {
                s.Append(quat.ToString());
                if (!brief && nextKey)
                {
                    s.Append(" -> "); s.Append(nextKey.quat.ToString());
                }
            }
            else if (valueType == (int)AMPropertyTrack.ValueType.Sprite)
            {
                s.AppendFormat("\"{0}\"", valObj ? valObj.name : "none");
            }
            else if (valueType == (int)AMPropertyTrack.ValueType.Enum)
            {
                s.Append(System.Enum.ToObject(type, (int)val).ToString());
            }
            return(s.ToString());
        }
Exemplo n.º 3
0
        // add key
        public AMPropertyKey addKey(AMITarget target, OnAddKey addCall, int _frame)
        {
            Component comp = GetTargetComp(target);

            RefreshData(comp);

            AMPropertyKey k = null;

            foreach (AMPropertyKey key in keys)
            {
                // if key exists on frame, update key
                if (key.frame == _frame)
                {
                    k = key;
                }
            }

            if (k == null)
            {
                k        = addCall(gameObject, typeof(AMPropertyKey)) as AMPropertyKey;
                k.frame  = _frame;
                k.interp = (int)(canTween ? AMKey.Interpolation.Linear : AMKey.Interpolation.None);
                // add a new key
                keys.Add(k);
            }

            if (isValueTypeNumeric(valueType))
            {
                k.val = Convert.ToDouble(getCachedInfoValue(target));
            }
            else if (valueType == (int)ValueType.Bool)
            {
                k.valb = Convert.ToBoolean(getCachedInfoValue(target));
            }
            else if (valueType == (int)ValueType.String)
            {
                k.valString = Convert.ToString(getCachedInfoValue(target));
            }
            else if (valueType == (int)ValueType.Vector2)
            {
                k.vect2 = (Vector2)getCachedInfoValue(target);
            }
            else if (valueType == (int)ValueType.Vector3)
            {
                k.vect3 = (Vector3)getCachedInfoValue(target);
            }
            else if (valueType == (int)ValueType.Color)
            {
                k.color = (Color)getCachedInfoValue(target);
            }
            else if (valueType == (int)ValueType.Rect)
            {
                k.rect = (Rect)getCachedInfoValue(target);
            }
            else if (valueType == (int)ValueType.Vector4)
            {
                k.vect4 = (Vector4)getCachedInfoValue(target);
            }
            else if (valueType == (int)ValueType.Quaternion)
            {
                k.quat = (Quaternion)getCachedInfoValue(target);
            }
            else if (valueType == (int)ValueType.Sprite)
            {
                k.valObj = (UnityEngine.Object)getCachedInfoValue(target);
            }
            else if (valueType == (int)ValueType.Enum)
            {
                k.val = Convert.ToDouble(getCachedInfoValue(target));
            }
            else
            {
                Debug.LogError("Animator: Invalid ValueType " + valueType.ToString());
            }

            // update cache
            updateCache(target);
            return(k);
        }
Exemplo n.º 4
0
        public override void build(AMSequence seq, AMTrack track, int index, UnityEngine.Object target)
        {
            AMPropertyTrack propTrack = track as AMPropertyTrack;

            if (endFrame == -1 && canTween && propTrack.canTween)
            {
                return;
            }

            int valueType = propTrack.valueType;

            //get component and fill the cached method info
            Component comp = propTrack.GetTargetComp(target as GameObject);

            if (comp == null)
            {
                return;
            }

            string varName = propTrack.getMemberName();

            int frameRate = seq.take.frameRate;

            //change to use setvalue track in AMSequence
            if (!string.IsNullOrEmpty(varName))
            {
                propTrack.RefreshData(comp);

                //allow tracks with just one key
                if (!propTrack.canTween || !canTween || track.keys.Count == 1)
                {
                    seq.Insert(this, GenerateTweener(seq, propTrack, frameRate, comp));
                }
                else
                {
                    //grab end frame
                    AMPropertyKey endKey = track.keys[index + 1] as AMPropertyKey;

                    if (targetsAreEqual(valueType, endKey))
                    {
                        return;
                    }

                    Tweener tween = null;

                    PropertyInfo propInfo = propTrack.GetCachedPropertyInfo();
                    if (propInfo != null)
                    {
                        switch ((AMPropertyTrack.ValueType)valueType)
                        {
                        case AMPropertyTrack.ValueType.Integer:
                            tween = DOTween.To(new IntPlugin(), () => System.Convert.ToInt32(propInfo.GetValue(comp, null)), (x) => propInfo.SetValue(comp, x, null), System.Convert.ToInt32(endKey.val), getTime(frameRate)); break;

                        case AMPropertyTrack.ValueType.Float:
                            tween = DOTween.To(new FloatPlugin(), () => System.Convert.ToSingle(propInfo.GetValue(comp, null)), (x) => propInfo.SetValue(comp, x, null), System.Convert.ToSingle(endKey.val), getTime(frameRate)); break;

                        case AMPropertyTrack.ValueType.Double:
                            tween = DOTween.To(new DoublePlugin(), () => System.Convert.ToDouble(propInfo.GetValue(comp, null)), (x) => propInfo.SetValue(comp, x, null), endKey.val, getTime(frameRate)); break;

                        case AMPropertyTrack.ValueType.Long:
                            tween = DOTween.To(new LongPlugin(), () => System.Convert.ToInt64(propInfo.GetValue(comp, null)), (x) => propInfo.SetValue(comp, x, null), System.Convert.ToInt64(endKey.val), getTime(frameRate)); break;

                        case AMPropertyTrack.ValueType.Vector2:
                            tween = DOTween.To(new Vector2Plugin(), () => (Vector2)propInfo.GetValue(comp, null), (x) => propInfo.SetValue(comp, x, null), endKey.vect2, getTime(frameRate)); break;

                        case AMPropertyTrack.ValueType.Vector3:
                            tween = DOTween.To(new Vector3Plugin(), () => (Vector3)propInfo.GetValue(comp, null), (x) => propInfo.SetValue(comp, x, null), endKey.vect3, getTime(frameRate)); break;

                        case AMPropertyTrack.ValueType.Color:
                            tween = DOTween.To(new ColorPlugin(), () => (Color)propInfo.GetValue(comp, null), (x) => propInfo.SetValue(comp, x, null), endKey.color, getTime(frameRate)); break;

                        case AMPropertyTrack.ValueType.Rect:
                            tween = DOTween.To(new RectPlugin(), () => (Rect)propInfo.GetValue(comp, null), (x) => propInfo.SetValue(comp, x, null), endKey.rect, getTime(frameRate)); break;

                        case AMPropertyTrack.ValueType.Vector4:
                            tween = DOTween.To(new Vector4Plugin(), () => (Vector4)propInfo.GetValue(comp, null), (x) => propInfo.SetValue(comp, x, null), endKey.vect4, getTime(frameRate)); break;

                        case AMPropertyTrack.ValueType.Quaternion:
                            tween = DOTween.To(new PureQuaternionPlugin(), () => (Quaternion)propInfo.GetValue(comp, null), (x) => propInfo.SetValue(comp, x, null), endKey.quat, getTime(frameRate)); break;
                        }
                    }
                    else
                    {
                        FieldInfo fieldInfo = propTrack.GetCachedFieldInfo();
                        if (fieldInfo != null)
                        {
                            switch ((AMPropertyTrack.ValueType)valueType)
                            {
                            case AMPropertyTrack.ValueType.Integer:
                                tween = DOTween.To(new IntPlugin(), () => System.Convert.ToInt32(fieldInfo.GetValue(comp)), (x) => fieldInfo.SetValue(comp, x), System.Convert.ToInt32(endKey.val), getTime(frameRate)); break;

                            case AMPropertyTrack.ValueType.Float:
                                tween = DOTween.To(new FloatPlugin(), () => System.Convert.ToSingle(fieldInfo.GetValue(comp)), (x) => fieldInfo.SetValue(comp, x), System.Convert.ToSingle(endKey.val), getTime(frameRate)); break;

                            case AMPropertyTrack.ValueType.Double:
                                tween = DOTween.To(new DoublePlugin(), () => System.Convert.ToDouble(fieldInfo.GetValue(comp)), (x) => fieldInfo.SetValue(comp, x), endKey.val, getTime(frameRate)); break;

                            case AMPropertyTrack.ValueType.Long:
                                tween = DOTween.To(new LongPlugin(), () => System.Convert.ToInt64(fieldInfo.GetValue(comp)), (x) => fieldInfo.SetValue(comp, x), System.Convert.ToInt64(endKey.val), getTime(frameRate)); break;

                            case AMPropertyTrack.ValueType.Vector2:
                                tween = DOTween.To(new Vector2Plugin(), () => (Vector2)fieldInfo.GetValue(comp), (x) => fieldInfo.SetValue(comp, x), endKey.vect2, getTime(frameRate)); break;

                            case AMPropertyTrack.ValueType.Vector3:
                                tween = DOTween.To(new Vector3Plugin(), () => (Vector3)fieldInfo.GetValue(comp), (x) => fieldInfo.SetValue(comp, x), endKey.vect3, getTime(frameRate)); break;

                            case AMPropertyTrack.ValueType.Color:
                                tween = DOTween.To(new ColorPlugin(), () => (Color)fieldInfo.GetValue(comp), (x) => fieldInfo.SetValue(comp, x), endKey.color, getTime(frameRate)); break;

                            case AMPropertyTrack.ValueType.Rect:
                                tween = DOTween.To(new RectPlugin(), () => (Rect)fieldInfo.GetValue(comp), (x) => fieldInfo.SetValue(comp, x), endKey.rect, getTime(frameRate)); break;

                            case AMPropertyTrack.ValueType.Vector4:
                                tween = DOTween.To(new Vector4Plugin(), () => (Vector4)fieldInfo.GetValue(comp), (x) => fieldInfo.SetValue(comp, x), endKey.vect4, getTime(frameRate)); break;

                            case AMPropertyTrack.ValueType.Quaternion:
                                tween = DOTween.To(new PureQuaternionPlugin(), () => (Quaternion)fieldInfo.GetValue(comp), (x) => fieldInfo.SetValue(comp, x), endKey.quat, getTime(frameRate)); break;
                            }
                        }
                    }

                    if (tween != null)
                    {
                        if (hasCustomEase())
                        {
                            tween.SetEase(easeCurve);
                        }
                        else
                        {
                            tween.SetEase((Ease)easeType, amplitude, period);
                        }

                        seq.Insert(this, tween);
                    }
                }
            }
            else
            {
                Debug.LogError("Animator: No FieldInfo or PropertyInfo set.");
            }

            return;
        }