=======================
=======================
=======================
*코드하기전에 주의*
읽어야할 리소스가 이메세지가 나온다면
"the texture memory can not be accessed from scripts"
해당 리소스의 속성 옵션에서
그림의 1번 그어진부분에 체크박스에 읽기/쓰기 권한을 체크했는지 확인해보자.
=======================
=======================
=======================
Sprite를 Texture로 바꾸어주는 코드들을 보는중 문제점이 있어 다시 여기저기서 보고 편집하였다.
대강 이거보고 안되면 다른 코드를 찾아서 참조하면 될 것 같다.
public static Texture ConvertSpriteToTexture(Sprite sprite) { try { if (sprite.rect.width != sprite.texture.width) { int x = Mathf.FloorToInt(sprite.textureRect.x); int y = Mathf.FloorToInt(sprite.textureRect.y); int width = Mathf.FloorToInt(sprite.textureRect.width); int height = Mathf.FloorToInt(sprite.textureRect.height); Texture2D newText = new Texture2D(width, height); Color[] newColors = sprite.texture.GetPixels(x, y, width, height); newText.SetPixels(newColors); newText.Apply(); return newText; } else return sprite.texture; } catch { return sprite.texture; } } |
=======================
=======================
=======================
출처: https://answers.unity.com/questions/651984/convert-sprite-image-to-texture.html
am rendering a GUI.Button which normally takes a Texture but I'd like to load the image for the button from a sprite sheet instead.
I've been digging in code for a while but I can't find a way to cast/convert a sprite to a texture.
---------------------------------------------------------------------------------------------------------------------------------------------------
So if anyone is looking for the answer: here it is.
First, you have to open your sprite texture in the inspector and change the type from "Sprite" to "Advanced". Then make sure that Read/Write Enabled is checked. Click Apply and now you can read the texture and set it into a new texture like so:
// assume "sprite" is your Sprite object var croppedTexture = new Texture2D( (int)sprite.rect.width, (int)sprite.rect.height ); var pixels = sprite.texture.GetPixels( (int)sprite.textureRect.x, (int)sprite.textureRect.y, (int)sprite.textureRect.width, (int)sprite.textureRect.height ); croppedTexture.SetPixels( pixels ); croppedTexture.Apply(); |
---------------------------------------------------------------------------------------------------------------------------------------------------
Here is a static helper method that will give you a texture2D from a sprite. It will even determine if the sprite is part of a single texture or if it is just a single sprite automatically. (basically the same as codeimpossibles answer)
public static Texture2D textureFromSprite(Sprite sprite) { if(sprite.rect.width != sprite.texture.width){ Texture2D newText = new Texture2D((int)sprite.rect.width,(int)sprite.rect.height); Color[] newColors = sprite.texture.GetPixels((int)sprite.textureRect.x, (int)sprite.textureRect.y, (int)sprite.textureRect.width, (int)sprite.textureRect.height ); newText.SetPixels(newColors); newText.Apply(); return newText; } else return sprite.texture; } |
----------------------------------------------------------------------------------------------------------------------------------------------------
Your solution is very good, but the code returns a error of pixel count.
resolved using the System.Math.Ceiling for to round the number for up.
Texture2D ConvertSpriteToTexture(Sprite sprite) { try { if (sprite.rect.width != sprite.texture.width) { Texture2D newText = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height); Color[] colors = newText.GetPixels(); Color[] newColors = sprite.texture.GetPixels((int)System.Math.Ceiling(sprite.textureRect.x), (int)System.Math.Ceiling(sprite.textureRect.y), (int)System.Math.Ceiling(sprite.textureRect.width), (int)System.Math.Ceiling(sprite.textureRect.height)); Debug.Log(colors.Length+"_"+ newColors.Length); newText.SetPixels(newColors); newText.Apply(); return newText; } else return sprite.texture; }catch { return sprite.texture; } } |
=======================
=======================
=======================
'게임엔진관련 > 유니티 엔진' 카테고리의 다른 글
[Unity] 유니티 Visual Studio(비쥬얼스튜디오) 설치, 설정, 셋팅 관련 (0) | 2020.03.12 |
---|---|
[Unity] 유니티 마우스 클릭 canvas UI랑 Terrain이 겹칠때 또는 canvas외의 오브젝트 겹칠때 관련 (0) | 2020.02.03 |
[Unity] 유니티 텍스트 TextUI - TextMesh Pro 관련 (0) | 2020.01.20 |
[Unity] 유니티 배열 리스트 등등 정렬 관련 (0) | 2020.01.14 |
[Unity] 유니티 파일 생성, 삭제, 체크 함수 관련 (0) | 2020.01.14 |