0
\$\begingroup\$

I have loaded a 2D sprite from the filesystem and created a sprite from it, which I then attach to a SpriteRenderer. The game object appears in the scene but for some reason it doesn't seem to be using the sprite I loaded, and is instead using the Standard Assets/2D/Materials/SpriteLift sprite. I have attached my sprite creation code and some screenshots:

public static Sprite LoadSprite(string relPath) {
    var fileUrl = "file://D:/Unity Projects/2D-Game/mod/base/textures/" + relPath + ".png";

    // Download texture from file location and resize it
    var www = new WWW (fileUrl);
    var texture = www.texture;
    texture.Resize (textureSize, textureSize);
    texture.name = relPath;
    Debug.Log (texture);

    // Make the sprite
    var sprite = Sprite.Create (texture, spriteRect, spritePivot, textureSize);
    sprite.name = relPath;
    return sprite;
}

The Unity scene with the incorrect sprite: enter image description here

The desired texture: enter image description here

The code that creates the game object and attaches the loaded sprite to the SpriteRenderer:

GameObject go = new GameObject ();
go.name = "Tile_ " + tile.location.ToString();
go.transform.position = tile.location.ToVector3 ();
go.transform.SetParent (this.transform, true);
SpriteRenderer sr = go.AddComponent<SpriteRenderer> ();
sr.sprite = tile.Type.sprite;
sr.enabled = true;

Note that the screenshot contains many game objects all with the same sprite, that's why it looks big in the scene.

EDIT: The problem may be due to materials.

EDIT 2: Screenshot of a selected tile:

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ Did you make sure that LoadSprite is executed before sr.sprite = tile.Type.sprite; is called? Sometimes you will need to make use of Script Execution Order to make sure things happen correctly. \$\endgroup\$
    – Allen
    May 16 '16 at 12:10
  • \$\begingroup\$ @Allen Yep, LoadSprite is in a class that isn't attached to a game object, and is called by a script that is attached to a master game object, before the tile game objects are made. I have a hunch that the problem may be due to materials. \$\endgroup\$
    – SamTebbs33
    May 16 '16 at 12:20
  • \$\begingroup\$ With that hunch, can you give us a shot with one of tiles selected so that we can see the SpriteRender component and Shader? \$\endgroup\$
    – Allen
    May 16 '16 at 13:15
  • \$\begingroup\$ @Allen I've added a screenshot! \$\endgroup\$
    – SamTebbs33
    May 16 '16 at 15:33
1
\$\begingroup\$

My apologies, I should have picked up on this quicker. Instead I made you get a screenshot that wasn't needed.

Your not waiting for your WWW to complete before creating the sprite. So basically the www.texture is empty at the time you use it. Check www.isDone before working with the texture to see what I mean.

If you do not want to do this in a Coroutine (yield return www;), you can block until it is done, but this is not recommended. By blocking I mean something like "while(!www.isDone){ // Do nothing }".

See the WWW documention for more information, looking at the example.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.