Generating a texture and using as Source Image
I've written a method to generate a texture for my minimap. All non-obstacle tiles are blue, and the obstacles are white. This is very easy, and I've saved the texture to a file to make sure this part is working.
However, when I want to add this as a source image in my gui image, all I get is a grey square. See code below.
Texture2D minimapBackdrop;
public void Init( bool[,] obstacleMap ){
this.obstacleMap = obstacleMap;
minimapBackdrop = GenerateMinimapTexture ( obstacleMap );
Rect rect = new Rect (-40f, -240f, minimapBackdrop.width, minimapBackdrop.height);
Sprite s = Sprite.Create (minimapBackdrop, rect, Vector2.zero );
s.name = "Minimap Sprite";
Image im = gameObject.GetComponent<Image> ();
im.sprite = s;
im.overrideSprite = s;
initialised = true;
}
Based on my googling on the topic, I have to create a sprite and assign this to the Image element, which is what I'm trying to do here.
It works if I save the texture as a .png and, via the editor, drop it on the image, so maybe the issue is with the created sprite, but I can't find what it is.. The name of the Source Image changes to "Minimap Sprite".
I found this question where someone seems to have the same problem, but it's not answerred. http://answers.unity3d.com/questions/942758/problems-with-spritecreate-in-current-version.html
Thanks!
You should categorize this as "bug" I've seen this around a few times now
Thanks for your comment and excuse the late answer - how exactly do I categorize it as a bug? Does adding the "bug-report" topic suffice?
Your answer
Follow this Question
Related Questions
Unity display image from assets folder 0 Answers
Trouble getting created sprite to set. 0 Answers
Display downloaded image as new Sprite 0 Answers
Switch Texture Type To Sprite. 0 Answers
problem with arrays and image replacment 0 Answers