- Home /
Unity 4.6: How to fit center byte array png in the Image UI?
I'm creating a dynamic IAB panel that all IAB data come form server, image data receive as byte array that is png file and I set it into the Image UI but the problem is my image cropped in the Image control. The code I'm using is:
public Image buyItemImage;
// the RectTrasform of Image control
public RectTransform buyItemImageRectTrasform;
Sprite sprite = getImageAsset((int)buyItemImageRectTrasform.rect.width, (int)buyItemImageRectTrasform.rect.height);
buyItemImage.sprite = sprite;
public Sprite getImageAsset(int width, int height)
{
Texture2D texture2D = new Texture2D(width, height);
texture2D.LoadImage(imageAsset);
Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
return sprite;
}
It could be that your image size is different. LoadImage will replace the texture size with the size of the image from the data. Are they both the same?
@karljj1 Yes, I printed the width and height of texture in console that has same size as my image.
What's the image type set to in your image after you add your sprite? Having it set to tiled can cause your issue.
@karljj1 I've never changed the image type. I just added above code. How can I find what image type is?
@karljj1 I found the solution and that was because of texture format
Answer by misaghDroid · Apr 14, 2015 at 05:27 PM
After a couple of days searching I found the solution is that I must set the TextureFormat inside the constructor of Texture2D class.
Texture2D texture2D = new Texture2D(2, 2, TextureFormat.Alpha8, false);
texture2D.LoadImage(ImageAsseet);
Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width,texture2D.height), new Vector2(0.5f, 0.5f));
return sprite;
Your answer
Follow this Question
Related Questions
compress photos on import 0 Answers
Sprite Becoming Blurry 2 Answers
9 Sliced Image Is Bleeding Edges 0 Answers
Can't get various different sprites to change on a UI *Image*. 0 Answers
Draw a Sprite Round 1 Answer