Hi everyone, I have a problem witch creating a sprite (from texture).
I noticed that new rect (0,0,texture.width,texture.height) is not always correct way to create sprite, because sometimes texture is cutting.
here is code:
Texture2D texture = new Texture2D(0,0);
texture.LoadImage(buffer);
//texture.wrapMode = TextureWrapMode.Clamp;
Rect rect = new Rect();
rect.center = new Vector2(0,0);
rect.height = texture.height;
rect.width = texture.width;
Sprite sprite = Sprite.Create(texture,rect,new Vector2(0.5f,0.5f),100f);
imageHandler.GetComponent<Image>().sprite = sprite;
imageHandler.GetComponent<Image>().SetNativeSize();
texture variable is for shure ok (I check it), but sprite is only fragment of that texture.
I try all method of creating rect but it always works strange.
It is posible to get all texture but by creating stange rect (new rect(0,0,width1.5f,height1.5f)).
What is going on?
1 Like
The strangest thing is that - sometimes it works fine, sometimes not, for now on pc works fine, but on iPad no.
The width and height are about 1.7 smaller than correct values (new rect (0,0,width1.7,height1.7) - than i see all texture)
It seams to according to this problem: Sprite.Create() seems to be innacurate on iOS - Unity Engine - Unity Discussions
Any one have idea?
1 Like
It seams to be an issue with new UI system (UI.Image) - its forced to be in tiled mode i think…
Any one have idea what to do with this?
Problem solved - I’m not shure that is a problem with UI.image or texture (i figured out that importer round sizes to power of two, so when defining a rectangle basing on texture.widh/height it was cheated):
UIImage image = (e.info [UIImagePickerController.OriginalImage] as UIImage);
Texture2D texture = image.ToTexture2D(true,200f/(!(image.size.Height>image.size.Width)?image.size.Height:image.size.Width));
texture.wrapMode = TextureWrapMode.Clamp;
Rect rect = new Rect ((!(texture.width < texture.height) ? (texture.width - texture.height) / 2:0),
(!(texture.width > texture.height) ? (texture.height - texture.width) / 2:0),
(!(texture.width > texture.height)?texture.width:texture.height),
(!(texture.width > texture.height)?texture.width:texture.height));
Sprite sprite = Sprite.Create(texture,rect,new Vector2(0.5f,0.5f),100f);
imageHandler.GetComponent<Image>().sprite = sprite;
Ok soo - On iOS UI.Image is working different than on pc (for example PreserveAspect won’t work when do stuff with rect transform from scrip - on pc that works). Texture2D is also working different (or Sprite.Create) propably because of difrences in texture format or power of two preferences on iOS.
Is better solution for that but I’m still working with that.