Neither of these do anything. Nothing is added to the hierarchy. I’m sure the code is being run. New to 2d and the documentation seems to be limited right now, so I hope someone can help.
public Texture2D[] heads;
public Texture2D head1;
void Awake ()
{
//trial 1
Sprite.Create(heads[Random.Range(0,2)],new Rect(0,0,400,400), new Vector2(0,0), 0.1f);
//trial 2
Sprite.Create(head1,new Rect(0,0,400,400), new Vector2(0,0), 0.1f);
print ("head created");
}
Sprite.Create doesn’t add anything to the hierarchy, it just creates a Sprite (which is a logical object only). You have to assign the sprite to a SpriteRenderer.sprite
I am having the exact same problem as PAHeartBeat mentioned above. My sprite comes out exactly the same way as his attached files, and I am using Sprite.Create with the same parameters.
I first encountered this at Unity 5.0.2f. When I upgraded to 5.2, it disappeared in the Editor, but not in my iOS Build. As the unity version influenced on the result, I can only assume this is a bug. Has anyone encountered this problem?
you have to give exact size of image to create sprite from it. other wise it will create nearest PowerOf2(POT) size sprite and it will create wrong sprite. The issue will only occur with NPOT size image.
edit: forget the add actual trick which hidden when I load texture from disk.
Here is method of load Texture from image. The trick is here in this code to get proper texture. First load texture from file bytes array then read image height and width and then reload it with identified size value and provide High Quality Image Setting via TextureFormat.RGBA31
Thanks for the trick, it’s always working on Unity 4.6.8f1.
When using Sprite.Create we have to load a texture at the exact size of the texture loaded or it does not work as expected !
I just came upon this issue again and managed to solve it quite easily. Previously, all the solutions, including loading the texture at the exact size did not work for me.
My solution was to disable mipmaps for the new texture, which is what @PAHeartBeat did in his code, so maybe that’s why it worked…?
Anyway, mipmap false.