using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace uTools
{
    [AddComponentMenu("uTools/Tween/Tween MaterialParam(uTools)")]
    public class uTweenMaterialParam : uTweenValue
    {
        [SerializeField]
        string paramName;
        
        [SerializeField]
        List<Renderer> renders;

        private bool _init = false;

        private int m_nameID;

        private MaterialPropertyBlock m_materialPropertyBlock;

        protected override void Awake()
        {
            base.Awake();
            m_materialPropertyBlock = new MaterialPropertyBlock();
            m_nameID = Shader.PropertyToID(paramName);
            _init = true;
        }

        protected override void ValueUpdate(float value, bool isFinished)
        {
            if (!_init)
            {
                Awake();
            }
            m_materialPropertyBlock.SetFloat(m_nameID, value);
            for (int i = 0; i < renders.Count; i++)
            {
                renders[i].SetPropertyBlock(m_materialPropertyBlock);
            }            
        }
    }
}
