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.
130 lines
4.5 KiB
130 lines
4.5 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class SetChoiceEffect : MonoBehaviour
|
|
{
|
|
|
|
[SerializeField]
|
|
public RawImage rawImage;
|
|
|
|
Material material;
|
|
|
|
public float FlashDuration = 0.2f; // Duration for the flash effect
|
|
public float FadeDuration = 1.0f; // Duration for the fade effect
|
|
|
|
public float MaxBloomAmount = 500.0f; // Maximum bloom amount
|
|
public float MinBloomAmount = 1.0f; // Minimum bloom amount
|
|
public float MaxBloomThreshold = 0.5f; // Maximum bloom threshold
|
|
public float MinBloomThreshold = 0.0f; // Minimum bloom threshold
|
|
|
|
public float ImageOffsetY = 0.05f; // Offset for the image in the shader
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
if (rawImage == null)
|
|
{
|
|
rawImage = GetComponentInChildren<RawImage>();
|
|
}
|
|
|
|
if (rawImage != null)
|
|
{
|
|
material = rawImage.material;
|
|
// material = rawImage.GetComponent<Renderer>().material;
|
|
setChoice("reset"); // Set default effect
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("RawImage component not found on this GameObject.");
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
material.SetFloat("_OffsetY", ImageOffsetY);
|
|
|
|
}
|
|
|
|
|
|
private IEnumerator FadeCoroutine(string name, float from, float to, float duration, Action onComplete = null, float delay = 0f)
|
|
{
|
|
if (delay > 0f)
|
|
yield return new WaitForSeconds(delay);
|
|
|
|
float elapsedTime = 0f;
|
|
while (elapsedTime < duration)
|
|
{
|
|
float t = elapsedTime / duration;
|
|
// Ease in-out (smoothstep)
|
|
float easeT = t * t * (3f - 2f * t);
|
|
float value = Mathf.Lerp(from, to, easeT);
|
|
|
|
material.SetFloat(name, value);
|
|
elapsedTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
material.SetFloat(name, to); // Ensure final value is set
|
|
|
|
onComplete?.Invoke(); // Invoke the callback if provided
|
|
}
|
|
public void Reset()
|
|
{
|
|
if (material != null)
|
|
{
|
|
material.SetFloat("_BloomIntensity", MinBloomAmount);
|
|
material.SetFloat("_BloomThreshold", MaxBloomThreshold);
|
|
material.SetFloat("_FadeAmount", 1.0f);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Material is not assigned.");
|
|
}
|
|
}
|
|
public void setChoice(string type)
|
|
{
|
|
switch (type)
|
|
{
|
|
case "save":
|
|
// material.SetFloat("_BloomAmount", 1.0f);
|
|
// material.SetFloat("_BloomThreshold", 0.0f);
|
|
material.SetFloat("_FadeAmount", 1.0f);
|
|
|
|
// StartCoroutine(FadeCoroutine("_FadeAmount", 0.0f, 1.0f, FlashDuration));
|
|
StartCoroutine(FadeCoroutine("_BloomIntensity", material.GetFloat("_BloomIntensity"), MaxBloomAmount, FlashDuration / 2));
|
|
StartCoroutine(FadeCoroutine("_BloomThreshold", material.GetFloat("_BloomThreshold"), MinBloomThreshold, FlashDuration / 2,
|
|
() =>
|
|
{
|
|
// After the fade is complete, reset the bloom amount to 1.0f
|
|
StartCoroutine(FadeCoroutine("_BloomIntensity", material.GetFloat("_BloomIntensity"), MinBloomAmount, FlashDuration / 2, null, FlashDuration));
|
|
StartCoroutine(FadeCoroutine("_BloomThreshold", material.GetFloat("_BloomThreshold"), MaxBloomThreshold, FlashDuration / 2, () =>
|
|
{
|
|
StartCoroutine(FadeCoroutine("_FadeAmount", material.GetFloat("_FadeAmount"), 0.0f, FadeDuration));
|
|
}, FlashDuration));
|
|
|
|
}
|
|
));
|
|
|
|
|
|
break;
|
|
case "discard":
|
|
// material.SetFloat("_BloomAmount", 1.0f);
|
|
// material.SetFloat("_BloomThreshold", 0.5f);
|
|
// material.SetFloat("_FadeAmount", 0.0f);
|
|
StartCoroutine(FadeCoroutine("_FadeAmount", material.GetFloat("_FadeAmount"), 0.0f, FadeDuration));
|
|
StartCoroutine(FadeCoroutine("_BloomIntensity", material.GetFloat("_BloomIntensity"), MinBloomAmount, FadeDuration));
|
|
break;
|
|
case "reset":
|
|
Reset();
|
|
break;
|
|
default:
|
|
Debug.LogWarning("Unknown effect type: " + type);
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|