반응형
Notice
Recent Posts
Recent Comments
«   2024/04   »
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
Archives
Today
Total
관리 메뉴

Do Something IT

Unity TweenMaterialColor 본문

Unity3D

Unity TweenMaterialColor

아낙시만더 2018. 11. 15. 11:48
반응형
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


    public class TweenMaterialColor : MonoBehaviour
    {
        public enum ETweenType
        {
            Pingpong,
            Ones
        }

        public ETweenType Type;
        public AnimationCurve Corve;
        public string TargetColorName;
        public Color ColorStart = Color.white;
        public Color ColorEnd = Color.clear;
        public float Duration = 1.0f;


        public Action EndAction;
        private readonly List _materialList = new List();
        private readonly List _rendererList = new List();

        protected bool Flag = false;
        protected float TempDuration;

        // Use this for initialization
        private void Start()
        {
            TempDuration = Duration;
            RecurSetFixedRenderQueue(transform, _materialList);
            SetColor(ColorStart);
        }


        internal void Action(bool p, float duration = 0)
        {
            if (Flag == p)
                return;

            if (0 < duration)
            {
                TempDuration = Duration;
                Duration = duration;
            }

            Flag = p;
            if (p)
                PlayForward();
            else
                PlayReverse();
        }

        public void PlayForward()
        {
            if (Type != ETweenType.Ones)
                return;
            StartCoroutine(Play(ColorStart, ColorEnd));
        }

        public void PlayReverse()
        {
            if (Type != ETweenType.Ones)
                return;
            StartCoroutine(Play(ColorEnd, ColorStart));
        }

        private IEnumerator Play(Color starColor, Color endColor)
        {
            var playTime = 0f;
            while (playTime < Duration)
            {
                playTime += Time.deltaTime;
                var value = Corve.Evaluate(playTime);
                SetColor(Color.Lerp(starColor, endColor, value));
                yield return null;
            }
            SetColor(endColor);

            if (Math.Abs(Duration - TempDuration) > 0)
                Duration = TempDuration;

            if (EndAction != null)
                EndAction();
        }

        public bool IsActive()
        {
            return Flag;
        }

        public void Update()
        {
            if (Type != ETweenType.Pingpong)
                return;

            var lerp = Mathf.PingPong(Time.time, Duration)/Duration;
            var value = Corve.Evaluate(lerp);
            SetColor(Color.Lerp(ColorStart, ColorEnd, value));
        }

        private void SetColor(Color col)
        {
            for (var i = 0; i < _rendererList.Count; i++)
            {
                _rendererList[i].material.SetColor(TargetColorName, col);
            }
            for (var i = 0; i < _materialList.Count; i++)
            {
                _materialList[i].SetColor(TargetColorName, col);
            }
        }


        private void RecurSetFixedRenderQueue(Transform t, List materialList)
        {
            var ren = t.renderer;
            if (ren != null)
            {
                if (ren.enabled)
                {
                    _rendererList.Add(ren);
                }
                var mats = ren.materials;
                if (mats != null)
                {
                    for (int i = 0; i < mats.Length; i++)
                    {
                        var mat = mats[i];
                        materialList.Add(mat);
                    }
                }
            }

            var particles = t.GetComponents();
            if (particles != null)
            {
                for (int i = 0; i < particles.Length; i++)
                {
                    if (particles[i].renderer.enabled)
                    {
                        _rendererList.Add(particles[i].renderer);
                    }
                    var mats = particles[i].renderer.materials;
                    for (int m = 0; m < mats.Length; m++)
                    {
                        materialList.Add(mats[m]);
                    }
                }
            }

            for (int i = 0; i < t.childCount; i++)
            {
                var child = t.GetChild(i);
                RecurSetFixedRenderQueue(child, materialList);
            }
        }
    }
반응형
Comments