ソースコード
using UnityEngine; public static class Texture2DExt { public static Sprite ToSprite( this Texture2D self ) { var rect = new Rect( 0, 0, self.width, self.height ); var pivot = Vector2.one * 0.5f; var newSprite = Sprite.Create( self, rect, pivot ); return newSprite; } }
使用例
using UnityEngine; public class Example : MonoBehaviour { public Texture2D m_texture; public SpriteRenderer m_renderer; private void Awake() { m_renderer.sprite = m_texture.ToSprite(); } }