예제 #1
0
        public Timeline()
        {
            areaClocks         = new HashSet <IAreaClock>();
            occurrences        = new HashSet <Occurrence>();
            handledOccurrences = new HashSet <Occurrence>();
            previousDeltaTimes = new Queue <float>();
            timeScale          = lastTimeScale = 1;

            animation    = new AnimationTimeline(this);
            animator     = new AnimatorTimeline(this);
            audioSource  = new AudioSourceTimeline(this);
            navMeshAgent = new NavMeshAgentTimeline(this);
            rigidbody    = new RigidbodyTimeline3D(this);
            rigidbody2D  = new RigidbodyTimeline2D(this);
            transform    = new TransformTimeline(this);
            windZone     = new WindZoneTimeline(this);
        }
        /// <summary>
        /// The components used by the timeline are cached for performance. If you add or remove built-in Unity components on the GameObject, you need to call this method to update the timeline accordingly. 
        /// </summary>
        public virtual void CacheComponents()
        {
            components.Clear();

            // Animator

            var animatorComponent = GetComponent<Animator>();

            if (animator == null && animatorComponent != null)
            {
                animator = new AnimatorTimeline(timeline, animatorComponent);
                animator.Initialize();
                components.Add(animator);
            }
            else if (animator != null && animatorComponent == null)
            {
                animator = null;
            }

            // Animation

            var animationComponent = GetComponent<Animation>();

            if (animation == null && animationComponent != null)
            {
                animation = new AnimationTimeline(timeline, animationComponent);
                animation.Initialize();
                components.Add(animation);
            }
            else if (animation != null && animationComponent == null)
            {
                animation = null;
            }

            // AudioSources

            var audioSourceComponents = GetComponents<AudioSource>();

            // Remove timelines for absent components
            for (int i = 0; i < audioSources.Count; i++)
            {
                var audioSource = audioSources[i];

                bool audioSourceComponentExists = false;

                for (int j = 0; j < audioSourceComponents.Length; j++)
                {
                    var audioSourceComponent = audioSourceComponents[j];

                    if (audioSource.component == audioSourceComponent)
                    {
                        audioSourceComponentExists = true;
                        break;
                    }
                }

                if (!audioSourceComponentExists)
                {
                    audioSources.Remove(audioSource);
                }
            }

            // Add timelines for new components
            for (int i = 0; i < audioSourceComponents.Length; i++)
            {
                var audioSourceComponent = audioSourceComponents[i];

                bool audioSourceExists = false;

                for (int j = 0; j < audioSources.Count; j++)
                {
                    var audioSource = audioSources[j];

                    if (audioSource.component == audioSourceComponent)
                    {
                        audioSourceExists = true;
                        break;
                    }
                }

                if (!audioSourceExists)
                {
                    var newAudioSource = new AudioSourceTimeline(timeline, audioSourceComponent);
                    newAudioSource.Initialize();
                    audioSources.Add(newAudioSource);
                    components.Add(newAudioSource);
                }
            }

            this.audioSource = audioSources.Count > 0 ? audioSources[0] : null;

            // NavMeshAgent

            var navMeshAgentComponent = GetComponent<UnityEngine.AI.NavMeshAgent>();

            if (navMeshAgent == null && navMeshAgentComponent != null)
            {
                navMeshAgent = new NavMeshAgentTimeline(timeline, navMeshAgentComponent);
                navMeshAgent.Initialize();
                components.Add(navMeshAgent);
            }
            else if (animation != null && navMeshAgentComponent == null)
            {
                navMeshAgent = null;
            }

            // ParticleSystem

            var particleSystemComponent = GetComponent<ParticleSystem>();

            if (particleSystem == null && particleSystemComponent != null)
            {
                if (timeline.rewindable)
                {
                    particleSystem = new RewindableParticleSystemTimeline(timeline, particleSystemComponent);
                    particleSystem.Initialize();
                }
                else
                {
                    particleSystem = new NonRewindableParticleSystemTimeline(timeline, particleSystemComponent);
                    particleSystem.Initialize();
                }

                components.Add(particleSystem);
            }
            else if (particleSystem != null && particleSystemComponent == null)
            {
                particleSystem = null;
            }

            // WindZone

            var windZoneComponent = GetComponent<WindZone>();

            if (windZone == null && windZoneComponent != null)
            {
                windZone = new WindZoneTimeline(timeline, windZoneComponent);
                windZone.Initialize();
                components.Add(windZone);
            }
            else if (windZone != null && windZoneComponent == null)
            {
                windZone = null;
            }

            // Terrain

            var terrainComponent = GetComponent<Terrain>();

            if (terrain == null && terrainComponent != null)
            {
                terrain = new TerrainTimeline(timeline, terrainComponent);
                terrain.Initialize();
                components.Add(terrain);
            }
            else if (terrain != null && terrainComponent == null)
            {
                terrain = null;
            }

            // TrailRenderer

            var trailRendererComponent = GetComponent<TrailRenderer>();

            if (trailRenderer == null && trailRendererComponent != null)
            {
                trailRenderer = new TrailRendererTimeline(timeline, trailRendererComponent);
                trailRenderer.Initialize();
                components.Add(trailRenderer);
            }
            else if (trailRenderer != null && trailRendererComponent == null)
            {
                trailRenderer = null;
            }

            // WheelCollider

            var wheelColliderComponent = GetComponent<WheelCollider>();

            if (wheelCollider == null && wheelColliderComponent != null)
            {
                wheelCollider = new WheelColliderTimeline(timeline, wheelColliderComponent);
                wheelCollider.Initialize();
                components.Add(wheelCollider);
            }
            else if (wheelCollider != null && wheelColliderComponent == null)
            {
                wheelCollider = null;
            }

            // Only activate one of Rigidbody / Rigidbody2D / Transform timelines at once

            var rigidbodyComponent = GetComponent<Rigidbody>();
            var rigidbody2DComponent = GetComponent<Rigidbody2D>();
            var transformComponent = GetComponent<Transform>();

            if (rigidbody == null && rigidbodyComponent != null)
            {
                rigidbody = new RigidbodyTimeline3D(timeline, rigidbodyComponent);
                rigidbody.Initialize();
                components.Add(rigidbody);
                rigidbody2D = null;
                transform = null;
            }
            else if (rigidbody2D == null && rigidbody2DComponent != null)
            {
                rigidbody2D = new RigidbodyTimeline2D(timeline, rigidbody2DComponent);
                rigidbody2D.Initialize();
                components.Add(rigidbody2D);
                rigidbody = null;
                transform = null;
            }
            else if (transform == null)
            {
                transform = new TransformTimeline(timeline, transformComponent);
                transform.Initialize();
                components.Add(transform);
                rigidbody = null;
                rigidbody2D = null;
            }
        }