LoginSignup
0
1

More than 1 year has passed since last update.

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

Posted at

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

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
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1