Unity マルチプルなスプライトから指定のテクスチャを取得。


このあたりをsprite editor(32x32)で切り分けて。

Quadなどにセットする。

using UnityEngine;

public class SpriteToTexture : MonoBehaviour
{
    [SerializeField] Sprite spriteone;
    void Start()
    {
        //ex) gameObject Quad
        gameObject.GetComponent<Renderer>().material.mainTexture
         = spriteone.ToTexture2D();
    }
}//class

static class spriteToTextureExtension
{
    public static Texture2D ToTexture2D(this Sprite sprite)
    {
        //if (sprite.texture.isReadable == false) Debug.LogWarning("Need Read/Write Enabaled");

        //original
        //https://kan-kikuchi.hatenablog.com/entry/GetTextureSameSizeAsSprite
        int x = (int)sprite.textureRect.x, y = (int)sprite.textureRect.y;
        int width = (int)sprite.textureRect.width, height = (int)sprite.textureRect.height;
        Texture2D newTexture = new Texture2D(width, height);
        newTexture.filterMode = sprite.texture.filterMode;//same filter

        newTexture.SetPixels(sprite.texture.GetPixels(x, y, width, height));//

        newTexture.Apply();
        return newTexture;
    }
}//class