Sprite.Create not working.

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

GetComponent.sprite = Sprite.Create(…)

You probably just want to use a public Sprite variable, and assign that to a GameObject which has a SpriteRenderer component.

–Eric

Can you show me the code to do that Eric?

public Sprite spriteImage;

void Start () {
    new GameObject("MySprite").AddComponent<SpriteRenderer>().sprite = spriteImage;
}

–Eric

Hi Guys

When I create Sprite using Sprite.Create() it’s genarte wrong sprite from Image.
LiveIcon is Texture2D object and read texture from Game Cache path

GameIcon.sprite=Sprite.Create(LiveIcon,newRect(0,0,LiveIcon.width,LiveIcon.height),newVector2(0f,1f),100);

Actual Texture is like “Maths_Mania_2_1436427298.png” and result of the code is “Screen Shot 2015-07-10 at 5.00.39 pm.png

2197975--145906--Maths_Mania_2_1436427298.png
2197975--145907--Screen Shot 2015-07-10 at 5.00.39 pm.png

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?

@PAHeartBeat , did you manage to fix it?

yes I find the solution or that one. it was little tricky in my case. Here is the solution for that.

    public Texture2D DefaultIcon = null;
    private Texture2D LiveIcon = null;
    public void ChangeIcon() {
            if(IO.File.FileExists(<<PATH_OF_IMAGE>>)) {
                LiveIcon = LoadTextrure(<<PATH_OF_IMAGE>>);

            }
            if(LiveIcon != null) {
                GameIcon.maskable = true;
                GameIcon.sprite = Sprite.Create(LiveIcon, new Rect(0, 0, LiveIcon.width, LiveIcon.height),
                    new Vector2(0.5f, 0.5f), 100, (uint)0);
            } else {
                GameIcon.sprite = DefaultIcon;
            }
        }
    }

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

   protected internal static Texture2D LoadTextrure(string fileName) {
            return LoadTextrure(string.Empty, fileName);
        }
        protected internal static Texture2D LoadTextrure(string path, string fileName) {
            Texture2D tex = new Texture2D(0, 0);
            //texTexture2D finalTex;

            string dataPath = path + fileName;
            byte[] imageData;
            try {
                if(System.IO.File.Exists(dataPath)) {
                    imageData = System.IO.File.ReadAllBytes(dataPath);
                    tex.LoadImage(imageData);
                    tex = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false);
                    tex.LoadImage(imageData);
                } else {
                    tex = null;
                }
            } catch(System.Exception ex) {
                Debug.LogError(ex.Message);
                tex = null;
            }
            return tex;
        }

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 !

Thanks again

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.