SHADE_Y3/Assets/Scripts/UI/SC_ButtonFX.cs

133 lines
3.8 KiB
C#

using SHADE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SHADE_Scripting.UI
{
public class ButtonFX:Script
{
public string onHoverEnterSound = "event:/UI/botton_hover";
public string onHoverExitSound = "Empty";
public string onClickSound = "event:/UI/button_success";
public string onReleaseSound = "event:/UI/button_fail";
[NonSerialized]
private AudioClipHandler onHoverEnterACHandler;
[NonSerialized]
private AudioClipHandler onHoverExitACHandler;
[NonSerialized]
private AudioClipHandler onClickACHandler;
[NonSerialized]
private AudioClipHandler onReleaseACHandler;
[NonSerialized]
private Vector3 defaultScale;
public float hoverScale = 1.1f;
public float clickScale = 0.9f;
[NonSerialized]
private TweenThread thread;
[NonSerialized]
private bool tweening = false;
[NonSerialized]
private float currentScale = 1.0f;
protected override void awake()
{
if(onHoverEnterSound != "Empty")
onHoverEnterACHandler = SHADE.Audio.CreateAudioClip(onHoverEnterSound);
if(onHoverExitSound != "Empty")
onHoverExitACHandler = SHADE.Audio.CreateAudioClip(onHoverExitSound);
if (onClickSound != "Empty")
onClickACHandler = SHADE.Audio.CreateAudioClip(onClickSound);
if (onReleaseSound != "Empty")
onReleaseACHandler = SHADE.Audio.CreateAudioClip(onReleaseSound);
}
protected override void start()
{
thread = TweenManager.CreateTweenThread(0.1f, 0.9f, 1.0f, EASING_METHOD.EASE_OUT_BACK);
Transform transform = GetComponent<Transform>();
if (transform == null)
return;
defaultScale = transform.LocalScale;
UIElement ui = GetComponent<UIElement>();
if (ui == null)
return;
ui.OnClick.RegisterAction(() =>
{
if (onClickSound != "Empty")
onClickACHandler.Play();
tweening = true;
if(thread != null)
thread.Reset(currentScale,clickScale);
});
ui.OnRelease.RegisterAction(() =>
{
if (onReleaseSound != "Empty")
onReleaseACHandler.Play();
tweening = true;
if (thread != null)
thread.Reset(currentScale, 1.0f);
});
ui.OnHoverEnter.RegisterAction(() =>
{
if (onHoverEnterSound!="Empty")
{
onHoverEnterACHandler.Play();
}
tweening = true;
if (thread != null)
thread.Reset(currentScale, hoverScale);
});
ui.OnHoverExit.RegisterAction(() =>
{
if (onHoverExitSound != "Empty")
onHoverExitACHandler.Play();
tweening = true;
if (thread != null)
thread.Reset(currentScale, 1.0f);
});
}
protected override void update()
{
Transform transform = GetComponent<Transform>();
if (transform == null)
return;
if (tweening == true && thread != null)
{
transform.LocalScale = defaultScale * thread.GetValue();
currentScale = thread.GetValue();
if (thread.IsCompleted())
tweening = false;
}
}
}
}