You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/AnimateShine.cs

71 lines
2.1 KiB
C#

1 week ago
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AnimateShine : MonoBehaviour
1 week ago
{
public Material shineMaterial; // Assign the original material (a reference from Inspector)
public Image targetImage; // Assign the UI Image component
public float shineSpeed = 1f; // Speed of shine animation
public float shinePauseDuration = 0.5f; // Half-second pause after a shine cycle
private Material instanceMaterial; // Unique instance of the material
private float shineLocation = 0f;
private bool isPaused = false;
private void Awake()
{
targetImage = GetComponent<Image>();
}
void Start()
{
if (shineMaterial != null && targetImage != null)
{
// Create a new instance of the material so each image has its own copy
instanceMaterial = new Material(shineMaterial);
// Assign the instance material to the UI Image
targetImage.material = instanceMaterial;
}
else
{
Debug.LogError("ShineEffect: Material or Target Image is missing!");
}
}
void Update()
{
if (instanceMaterial != null && !isPaused)
{
// Animate the "Shine Location" property
shineLocation += Time.deltaTime * shineSpeed;
if (shineLocation > 1f)
{
shineLocation = 1f; // Cap at 1 before pausing
StartCoroutine(PauseShine()); // Start the pause coroutine
}
// Update material property
instanceMaterial.SetFloat("_ShineLocation", shineLocation);
}
}
IEnumerator PauseShine()
{
isPaused = true; // Stop animation
yield return new WaitForSeconds(shinePauseDuration); // Wait for half a second
shineLocation = 0f; // Reset shine location
isPaused = false; // Resume animation
}
void OnDestroy()
{
// Cleanup to prevent memory leaks
if (instanceMaterial != null)
{
Destroy(instanceMaterial);
}
}
}