Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************
- | SubtextureChanger.cs
- |
- | Ian Wexler
- |
- | Changes the scale and offset of a material's
- | texture to show a specific section.
- ************************************************/
- using UnityEngine;
- public class SubtextureChanger : MonoBehaviour
- {
- [SerializeField]
- private Material material = null;
- [SerializeField]
- private int columns = 1;
- [SerializeField]
- private int rows = 1;
- [SerializeField]
- private int index = 0;
- private void OnValidate()
- {
- SetSubtexture(index);
- }
- public void SetSubtexture(int i)
- {
- // Loop around and filter out negative values.
- int count = columns * rows;
- index = (i % count + count) % count;
- Vector2 offset = new Vector2();
- Vector2 scale = new Vector2();
- // Set the scale based on rows and columns.
- scale.x = 1 / (float)columns;
- scale.y = 1 / (float)rows;
- // Set the offset so the index increases on x and then y.
- offset.x = (index % columns) * scale.x;
- offset.y = (index / columns) * scale.y;
- // Apply the scale and offset.
- material.SetTextureScale("_MainTex", scale);
- material.SetTextureOffset("_MainTex", offset);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement