using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Coffee.UIExtensions
{
///
/// Abstract effect base for UI.
///
[DisallowMultipleComponent]
public abstract class UIEffectBase : BaseMeshEffect, IParameterTexture
#if UNITY_EDITOR
, ISerializationCallbackReceiver
#endif
{
protected static readonly Vector2[] splitedCharacterPosition = { Vector2.up, Vector2.one, Vector2.right, Vector2.zero };
protected static readonly List tempVerts = new List();
[HideInInspector]
[SerializeField] int m_Version;
[SerializeField] protected Material m_EffectMaterial;
///
/// Gets or sets the parameter index.
///
public int parameterIndex { get; set; }
///
/// Gets the parameter texture.
///
public virtual ParameterTexture ptex { get { return null; } }
///
/// Gets target graphic for effect.
///
public Graphic targetGraphic { get { return graphic; } }
///
/// Gets material for effect.
///
public Material effectMaterial { get { return m_EffectMaterial; } }
#if UNITY_EDITOR
protected override void Reset()
{
m_Version = 300;
OnValidate();
}
///
/// Raises the validate event.
///
protected override void OnValidate()
{
base.OnValidate();
var mat = GetMaterial();
if (m_EffectMaterial != mat)
{
m_EffectMaterial = mat;
UnityEditor.EditorUtility.SetDirty(this);
}
ModifyMaterial();
SetVerticesDirty();
SetDirty();
}
public void OnBeforeSerialize()
{
}
public void OnAfterDeserialize()
{
UnityEditor.EditorApplication.delayCall += UpgradeIfNeeded;
}
protected bool IsShouldUpgrade(int expectedVersion)
{
if (m_Version < expectedVersion)
{
Debug.LogFormat(gameObject, "{0}({1}) has been upgraded: version {2} -> {3}", name, GetType().Name, m_Version, expectedVersion);
m_Version = expectedVersion;
//UnityEditor.EditorApplication.delayCall += () =>
{
UnityEditor.EditorUtility.SetDirty(this);
if (!Application.isPlaying && gameObject && gameObject.scene.IsValid())
{
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(gameObject.scene);
}
}
;
return true;
}
return false;
}
protected virtual void UpgradeIfNeeded()
{
}
///
/// Gets the material.
///
/// The material.
protected virtual Material GetMaterial()
{
return null;
}
#endif
///
/// Modifies the material.
///
public virtual void ModifyMaterial()
{
if (targetGraphic == null)
return;
targetGraphic.material = isActiveAndEnabled ? m_EffectMaterial : null;
}
///
/// This function is called when the object becomes enabled and active.
///
protected override void OnEnable()
{
base.OnEnable();
if (ptex != null)
{
ptex.Register(this);
}
ModifyMaterial();
SetVerticesDirty();
SetDirty();
}
///
/// This function is called when the behaviour becomes disabled () or inactive.
///
protected override void OnDisable()
{
base.OnDisable();
ModifyMaterial();
SetVerticesDirty();
if (ptex != null)
{
ptex.Unregister(this);
}
}
///
/// Mark the UIEffect as dirty.
///
protected virtual void SetDirty()
{
SetVerticesDirty();
}
protected override void OnDidApplyAnimationProperties()
{
SetDirty();
}
}
}