Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Assertions;
- using UnityEngine.UI;
- [RequireComponent (typeof(Renderer))]
- public class GetSpriteInfo : MonoBehaviour
- {
- [SerializeField] Sprite sprite;
- void Start ()
- {
- // UVが取得できるか確認
- Assert.IsTrue (sprite.packed);
- Assert.IsTrue (sprite.packingRotation == SpritePackingRotation.None);
- Assert.IsTrue (sprite.packingMode == SpritePackingMode.Rectangle);
- // SpriteからTextureを取得してUVを取得する
- var tex = sprite.texture;
- var rect = sprite.textureRect;
- MaterialPropertyBlock block = new MaterialPropertyBlock ();
- block.SetVector ("_MainTex_ST", new Vector4 (
- rect.width / tex.width, rect.height / tex.height,
- rect.xMin / tex.width, rect.yMin / tex.height));
- block.SetTexture ("_MainTex", tex);
- GetComponent<Renderer> ().SetPropertyBlock (block);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement