seefood_diet / Assets / UIKit / UIView.cs
UIView.cs
Raw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UIKit {

    [ExecuteInEditMode]
    [RequireComponent (typeof (UIElement))]
    public class UIView : MonoBehaviour, IUIStyleObserver, IUIColorObserver {

        [SerializeField] UIStyleId m_StyleId;
        public UIStyleId StyleId { get { return m_StyleId; } set { m_StyleId = value; RefreshStyle (); } }

        [SerializeField] UIColorId m_ColorId;
        public UIColorId ColorId { get { return m_ColorId; } set { m_ColorId = value; RefreshColor (); } }

        public UIElement Element;

        #region life cycle

        void Awake () {

            Element = this.gameObject.GetComponent<UIElement> ();

            RefreshStyle ();
            RefreshColor ();

            UIStyleSheetController.Default.RegisterListener (this);
            UIColorPaletteController.Default.RegisterListener (this);
        }

        void OnEnable () {

#if UNITY_EDITOR

            if (!Application.isPlaying) {

                RefreshStyle ();
                RefreshColor ();
            }
#endif
        }

        void OnDestroy () {

            UIStyleSheetController.Default.UnregisterListener (this);
            UIColorPaletteController.Default.UnregisterListener (this);
        }

        #endregion

        public void RefreshStyle () {

            if (m_StyleId == UIStyleId.None) return;
            Element.SetStyle (UIStyleSheetController.Default.GetStyle (m_StyleId));
        }

        public void RefreshColor () {

            Element.SetColor (UIColorPaletteController.Default.GetColor (m_ColorId));
        }

        public void OnUIStyleChanged () {

            RefreshStyle ();
        }

        public void OnUIColorChanged () {

            RefreshColor ();
        }
    }
}