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.
157 lines
3.9 KiB
157 lines
3.9 KiB
using System; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.Events; |
|
using UnityEngine.Serialization; |
|
using UnityEngine.UI; |
|
|
|
[ExecuteInEditMode] |
|
public class ShaderControl : MonoBehaviour |
|
{ |
|
public bool KEEP_START = false; |
|
public Material origin = null; |
|
[Space(10)] |
|
public List<string> nameOn; |
|
public List<ShaderValue> values_Init; |
|
public List<ShaderValue> values_Play; |
|
[Space(10)] |
|
public bool isPlay = false; |
|
public bool LOOP = false; |
|
[Space(10)] |
|
[Range(0, 5)] |
|
public float duration = 3f; |
|
[Space(10)] |
|
public List<Image> listShared; |
|
|
|
Image CanvasImage; |
|
Material material; |
|
float durationSum = 0; |
|
float timeCurve; |
|
|
|
private void Awake() |
|
{ |
|
Init(); |
|
|
|
if(KEEP_START) |
|
MaterialApply(material); |
|
} |
|
void Init() |
|
{ |
|
if (CanvasImage == null) |
|
{ |
|
CanvasImage = this.gameObject.GetComponent<Image>(); |
|
if (material == null) |
|
{ |
|
if (origin != null) |
|
material = new Material(origin); |
|
|
|
if (material != null) |
|
{ |
|
for(int i = 0; i < nameOn.Count; i++) |
|
material.EnableKeyword(nameOn[i]); |
|
|
|
for (int i = 0; i < values_Init.Count; i++) |
|
{ |
|
if (values_Init[i].color == Color.clear) |
|
material.SetFloat($"{values_Init[i].name}", values_Init[i].value); |
|
else |
|
material.SetColor($"{values_Init[i].name}", values_Init[i].color); |
|
} |
|
} |
|
else |
|
{ |
|
Debug.LogWarning($"{name} : 쉐이더 없음"); |
|
} |
|
} |
|
} |
|
} |
|
void Update() |
|
{ |
|
if (isPlay && material != null) |
|
{ |
|
durationSum += Time.deltaTime; |
|
timeCurve = durationSum / duration; |
|
|
|
if (CanvasImage.material != material) |
|
MaterialApply(material); |
|
|
|
for(int i = 0; i < values_Play.Count; i++) |
|
material.SetFloat($"{values_Play[i].name}", values_Play[i].value * values_Play[i].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); |
|
|
|
durationSum = 0; |
|
isPlay = true; |
|
enabled = isPlay; |
|
} |
|
void MaterialApply(Material material) |
|
{ |
|
if (CanvasImage.material == material) |
|
return; |
|
|
|
CanvasImage.material = material; |
|
|
|
for (int i = 0; i < listShared.Count; i++) |
|
listShared[i].material = material; |
|
} |
|
|
|
[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(); |
|
} |
|
}
|
|
|