UPDATE: No one has any ideas on this one? I would have thought this would be a fairly simple problem to solve. It is a particularly useful thing to be able to do if you want the end-user to be able to easily mod your application with new images …
I’d basically like to do the following:
SpriteRenderer sr = myGameObject.GetComponent<SpriteRenderer>();
sr.sprite = (Sprite)Resources.Load<Sprite>("mySprite");
… except, I’d like mySprite to come from the streamingassets directory instead of the Unity resources.
Is this possible?
Can anyone give me an example of how this is done?
I found the solution. You need to use the WWW class to load a local file. You can access that file as a Texture, and then you create a Sprite using that Texture.
public IEnumerator LoadSprite(string absoluteImagePath)
{
string finalPath;
WWW localFile;
Texture texture;
Sprite sprite;
finalPath = "file://" + absoluteImagePath;
localFile = new WWW(localFile);
yield return localFile;
texture = localFile.texture;
sprite = Sprite.Create(texture as Texture2D, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
}