Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Assertions;
  5. using UnityEngine.UI;
  6.  
  7. [RequireComponent (typeof(Renderer))]
  8. public class GetSpriteInfo : MonoBehaviour
  9. {
  10. [SerializeField] Sprite sprite;
  11.  
  12. void Start ()
  13. {
  14. // UVが取得できるか確認
  15. Assert.IsTrue (sprite.packed);
  16. Assert.IsTrue (sprite.packingRotation == SpritePackingRotation.None);
  17. Assert.IsTrue (sprite.packingMode == SpritePackingMode.Rectangle);
  18.  
  19. // SpriteからTextureを取得してUVを取得する
  20. var tex = sprite.texture;
  21. var rect = sprite.textureRect;
  22.  
  23. MaterialPropertyBlock block = new MaterialPropertyBlock ();
  24. block.SetVector ("_MainTex_ST", new Vector4 (
  25. rect.width / tex.width, rect.height / tex.height,
  26. rect.xMin / tex.width, rect.yMin / tex.height));
  27. block.SetTexture ("_MainTex", tex);
  28. GetComponent<Renderer> ().SetPropertyBlock (block);
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement