You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
3.8 KiB
165 lines
3.8 KiB
using System; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.Events; |
|
using UnityEngine.Serialization; |
|
using UnityEngine.UI; |
|
|
|
//[ExecuteInEditMode] |
|
public class ShaderFade : MonoBehaviour |
|
{ |
|
public string shader = "2DxFX_Extra_Shaders/AlphaIntensity"; |
|
public string nameValue = "_AlphaIntensity_Fade_1"; |
|
|
|
public string nameOn = string.Empty; |
|
public List<ShaderValue> values_Init; |
|
|
|
[Space(10)] |
|
public bool isPlay = false; |
|
public bool LOOP = false; |
|
[Space(10)] |
|
[Range(0, 5)] |
|
public float duration = 3f; |
|
public AnimationCurve curve; |
|
[Range(0, 100)] |
|
public float valueMax = 6; |
|
|
|
float durationSum = 0; |
|
float timeCurve; |
|
|
|
Image CanvasImage; |
|
|
|
[Space(10)] |
|
public Material material; |
|
public List<Image> listShared; |
|
|
|
private void Start() |
|
{ |
|
Init(); |
|
} |
|
void Init() |
|
{ |
|
if (CanvasImage == null) |
|
{ |
|
CanvasImage = this.gameObject.GetComponent<Image>(); |
|
if (material == null) |
|
{ |
|
material = new Material(Shader.Find($"{shader}")); |
|
|
|
if (material != null) |
|
{ |
|
if (nameOn != string.Empty) |
|
material.EnableKeyword(nameOn); |
|
|
|
for(int i = 0; i < values_Init.Count; i++) |
|
material.SetFloat($"{values_Init[i].name}", values_Init[i].value); |
|
} |
|
} |
|
} |
|
} |
|
void Update() |
|
{ |
|
if (isPlay && material != null) |
|
{ |
|
durationSum += Time.deltaTime; |
|
timeCurve = durationSum / duration; |
|
|
|
if (CanvasImage.material != material) |
|
MaterialApply(material); |
|
|
|
material.SetFloat($"{nameValue}", valueMax * curve.Evaluate(timeCurve)); |
|
|
|
if (isPlay && duration < durationSum) |
|
{ |
|
durationSum = 0; |
|
|
|
if (!LOOP) |
|
{ |
|
if (OnEnd != null) |
|
OnEnd(); |
|
|
|
if (KEEP) |
|
{ |
|
isPlay = false; |
|
enabled = isPlay; |
|
} |
|
else |
|
{ |
|
Play(false, false, null); |
|
|
|
OnEndPlay(); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
System.Action OnEnd; |
|
bool KEEP = false; |
|
public void Play(bool isPlay, bool LOOP, System.Action OnEnd, bool KEEP = false) |
|
{ |
|
Init(); |
|
|
|
MaterialApply(isPlay ? material : null); |
|
|
|
this.isPlay = isPlay; |
|
this.LOOP = LOOP; |
|
this.OnEnd = OnEnd; |
|
this.KEEP = KEEP; |
|
|
|
enabled = isPlay; |
|
} |
|
public void Play() |
|
{ |
|
Init(); |
|
|
|
MaterialApply(isPlay ? material : null); |
|
|
|
isPlay = true; |
|
|
|
enabled = isPlay; |
|
} |
|
void MaterialApply(Material material) |
|
{ |
|
CanvasImage.material = material; |
|
|
|
for (int i = 0; i < listShared.Count; i++) |
|
listShared[i].material = material; |
|
} |
|
public void ValueSet(float value) |
|
{ |
|
Init(); |
|
|
|
MaterialApply(material); |
|
if (material != null) |
|
material.SetFloat($"{nameValue}", value); |
|
|
|
gameObject.SetActive(true); |
|
} |
|
public void ValueSet(string nameValue, float value) |
|
{ |
|
Init(); |
|
|
|
MaterialApply(material); |
|
if (material != null) |
|
material.SetFloat($"{nameValue}", value); |
|
|
|
gameObject.SetActive(true); |
|
} |
|
|
|
[Serializable] |
|
public class EndEvent : UnityEvent { } |
|
[FormerlySerializedAs("onEnd")] |
|
[SerializeField] |
|
private EndEvent m_OnEnd = new EndEvent(); |
|
public EndEvent OnPlayEnd |
|
{ |
|
get { return m_OnEnd; } |
|
set { m_OnEnd = value; } |
|
} |
|
public void OnEndPlay() |
|
{ |
|
OnPlayEnd.Invoke(); |
|
} |
|
}
|
|
|