summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/UnitTimeline.cs
blob: 478840a9e6b866048338d9468db9ca822f777a5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

// 处理动画帧事件和碰撞盒
[DisallowMultipleComponent]
public partial class UnitTimeline : MonoBehaviour
{
    public enum EEventType
    {
        EventCamera_Zoom,     // 相机聚焦
        EventCamera_Shake,    // 相机晃动
        EventCamera_Blur,     // 相机模糊
        EventCamera_WhiteOut, // 相机白屏

        EventMesh_AfterImage, // 角色残像
        EventMesh_Fade,       // 角色透明度
        EventMesh_Gloss,      // 角色泛光

        EventEnv_Dark,
        EventEnv_Exposure,

        EventUI_Drift,        // 
        EventUI_Blur,         //  

        EventProjectile,      // 发射体
        EventEffect,          // 特效
        EventSound,           // 音效

        EventBulletTime,      // 子弹时间
    }

    public const int kMaxEventsPerFrame = 10;
    public const int FPS = 30; // timeline 每秒采样30次

    private int m_PrevFrame = -1;

    public static Type GetTypeByName(string name)
    {
        Type type = Type.GetType(name);
        return type;
    }

    public void ExecuteAnimationEvents(AnimationData animData, float animFrame)
    {
        if (animData == null)
            return;
        int frame = (int)animFrame;
        if (frame != m_PrevFrame)
        {
            for (int i = m_PrevFrame + 1; i <= frame; i++)
            {
                List<int> framesHasEvent = animData.GetAnimationEventFrameIndices();
                if (framesHasEvent.Contains(i))
                {
                    List<AnimationEventBase> events = animData.GetAnimationEventsAtFrame(i);
                    ExecuteEvents(events);
                    ListPool<AnimationEventBase>.Release(events);
                }
                ListPool<int>.Release(framesHasEvent);
            }
        }
        m_PrevFrame = frame;
    }

    void ExecuteEvents(List<AnimationEventBase> events)
    {
        if (events == null || events.Count == 0)
            return;
        foreach (var e in events)
        {
            string name = e.type.ToString();
            MethodInfo method = GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(AnimationEventBase) }, null);
            if (method != null)
            {
                object[] param = new object[] { e };
                method.Invoke(this, param);
            }
        }
    }

    #region Event handle

    void EventEffect(AnimationEventBase animEvent)
    {
        EventEffect effect = animEvent as EventEffect;
        if (effect == null)
            return;
        string path = effect.effectPath;
#if UNITY_EDITOR
        GameObject prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
        if (prefab != null)
        {
            GameObject root = new GameObject();

            GameObject go = GameObject.Instantiate(prefab);
            go.transform.SetParent(root.transform);
            FxClear onClear = root.AddComponent<FxClear>();
            onClear.RunInEditor = true;
            onClear.Initialize(new PlayEffectInfo(path, EffectPlayTypes.Oneshot, transform, effect.position, effect.rotation, effect.scale, 0, false));
        }
#endif
    }

    void EventCamera_Shake(AnimationEventBase animEvent)
    {

    }

    #endregion 
}