SHADE_Y3/Assets/Scripts/UI/SC_ButtonFX.cs

133 lines
3.8 KiB
C#
Raw Normal View History

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
{
2023-03-05 10:47:16 +08:00
public string onHoverEnterSound = "Empty";
public string onHoverExitSound = "Empty";
public string onClickSound = "event:/UI/mouse_down_element";
public string onReleaseSound = "Empty";
[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;
2023-02-27 15:16:48 +08:00
[NonSerialized]
private TweenThread thread;
[NonSerialized]
private bool tweening = false;
[NonSerialized]
private float currentScale = 1.0f;
protected override void awake()
{
2023-03-05 10:47:16 +08:00
if(onHoverEnterSound != "Empty")
onHoverEnterACHandler = SHADE.Audio.CreateAudioClip(onHoverEnterSound);
if(onHoverExitSound != "Empty")
onHoverExitACHandler = SHADE.Audio.CreateAudioClip(onHoverExitSound);
2023-02-27 15:16:48 +08:00
2023-03-05 10:47:16 +08:00
if (onClickSound != "Empty")
onClickACHandler = SHADE.Audio.CreateAudioClip(onClickSound);
if (onReleaseSound != "Empty")
onReleaseACHandler = SHADE.Audio.CreateAudioClip(onReleaseSound);
}
protected override void start()
{
2023-02-27 15:16:48 +08:00
thread = TweenManager.CreateTweenThread(0.1f, 0.9f, 1.0f, EASING_METHOD.EASE_IN_SINE);
Transform transform = GetComponent<Transform>();
if (transform == null)
return;
defaultScale = transform.LocalScale;
UIElement ui = GetComponent<UIElement>();
if (ui == null)
return;
ui.OnClick.RegisterAction(() =>
{
2023-03-05 10:47:16 +08:00
if (onClickSound != "Empty")
2023-02-27 09:55:54 +08:00
onClickACHandler.Play();
2023-02-27 15:16:48 +08:00
tweening = true;
if(thread != null)
thread.Reset(currentScale,clickScale);
});
ui.OnRelease.RegisterAction(() =>
{
2023-03-05 10:47:16 +08:00
if (onReleaseSound != "Empty")
2023-02-27 09:55:54 +08:00
onReleaseACHandler.Play();
2023-03-05 10:47:16 +08:00
2023-02-27 15:16:48 +08:00
tweening = true;
if (thread != null)
thread.Reset(currentScale, 1.0f);
});
ui.OnHoverEnter.RegisterAction(() =>
{
2023-03-05 10:47:16 +08:00
if (onHoverEnterSound!="Empty")
{
2023-02-27 09:55:54 +08:00
onHoverEnterACHandler.Play();
2023-03-05 10:47:16 +08:00
}
2023-02-27 15:16:48 +08:00
tweening = true;
if (thread != null)
thread.Reset(currentScale, hoverScale);
});
ui.OnHoverExit.RegisterAction(() =>
{
2023-03-05 10:47:16 +08:00
if (onHoverExitSound != "Empty")
2023-02-27 09:55:54 +08:00
onHoverExitACHandler.Play();
2023-02-27 15:16:48 +08:00
tweening = true;
if (thread != null)
thread.Reset(currentScale, 1.0f);
});
}
protected override void update()
{
2023-02-27 15:16:48 +08:00
Transform transform = GetComponent<Transform>();
if (transform == null)
return;
if (tweening == true && thread != null)
{
2023-02-27 15:16:48 +08:00
transform.LocalScale = defaultScale * thread.GetValue();
currentScale = thread.GetValue();
if (thread.IsCompleted())
tweening = false;
}
}
}
}