Advertisement
Guest User

Offset Code Unity

a guest
Mar 8th, 2025
52
0
83 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | Source Code | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. [RequireComponent(typeof(Image))]
  5. public class RarityBorderAnimator : MonoBehaviour
  6. {
  7.     [Tooltip("Assign a material configured for scrolling UVs.")]
  8.     [SerializeField] private Material borderMaterial;
  9.  
  10.     [Tooltip("Speed at which the texture will scroll horizontally.")]
  11.     [SerializeField] private float scrollSpeed = 0.5f;
  12.  
  13.     private Material runtimeMaterial;
  14.  
  15.     void Start()
  16.     {
  17.         // Double the scale so it obviously repeats
  18.         runtimeMaterial.mainTextureScale = new Vector2(5f, 5f);
  19.     }
  20.    
  21.     void Awake()
  22.     {
  23.         // Make a runtime copy of the material
  24.         runtimeMaterial = Instantiate(borderMaterial);
  25.  
  26.         // Assign that copy to the Image
  27.         Image image = GetComponent<Image>();
  28.         image.material = runtimeMaterial;
  29.     }
  30.  
  31.     void Update()
  32.         {
  33.             // Just apply the global offset from InventoryManager
  34.             float offset = InventoryManager.globalSwirlOffset;
  35.             runtimeMaterial.mainTextureOffset = new Vector2(offset, offset);
  36.             runtimeMaterial.mainTextureScale = new Vector2(5f, 5f);
  37.         }
  38.    
  39.     public void SetMaterial(Material newMat)
  40.     {
  41.         // If you want to update mid-runtime, you can reassign the runtime material
  42.         if (runtimeMaterial != null) Destroy(runtimeMaterial);
  43.  
  44.         runtimeMaterial = Instantiate(newMat);
  45.         GetComponent<Image>().material = runtimeMaterial;
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement