using UnityEngine; using UnityEngine.UI; namespace BulletHellTemplate { /// /// Scrolls a texture on a RawImage by modifying its UV rect, creating an infinite scrolling effect. /// Automatically resets UV values to prevent floating-point precision errors over long periods. /// [RequireComponent(typeof(RawImage))] public class RawImageScroller : MonoBehaviour { [Header("Scroll Speed")] [Tooltip("Horizontal scroll speed (units per second). Positive = scroll right, Negative = scroll left.")] public float scrollSpeedX = 0.1f; [Tooltip("Vertical scroll speed (units per second). Positive = scroll up, Negative = scroll down.")] public float scrollSpeedY = 0.0f; [Header("UV Reset Settings")] [Tooltip("Threshold at which the UV coordinates will be reset to prevent precision loss.")] public float uvResetThreshold = 100.0f; private RawImage rawImage; private Rect currentUVRect; /// /// Caches the RawImage component and initial UV rect. /// private void Awake() { rawImage = GetComponent(); currentUVRect = rawImage.uvRect; } /// /// Updates the UV rect every frame to produce a seamless scrolling effect. /// private void Update() { float offsetX = scrollSpeedX * Time.deltaTime; float offsetY = scrollSpeedY * Time.deltaTime; currentUVRect.x += offsetX; currentUVRect.y += offsetY; // Reset UV coordinates if they exceed the threshold if (Mathf.Abs(currentUVRect.x) > uvResetThreshold) { currentUVRect.x = Mathf.Repeat(currentUVRect.x, 1.0f); } if (Mathf.Abs(currentUVRect.y) > uvResetThreshold) { currentUVRect.y = Mathf.Repeat(currentUVRect.y, 1.0f); } rawImage.uvRect = currentUVRect; } } }