- Home /
How to load a sprite from streamingassets instead of resources?
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?
Thanks
I would like to know the answer to that question as well.
In my case I want the users to be able to add new portraits for their characters, so loading the images from Resources is not possible and doing so from Strea$$anonymous$$gAssets should be the way to go.
Answer by Ladis · Sep 30, 2015 at 09:48 AM
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);
}
I hope that's useful for your case too ;-)
Hi, I've been trying to do similar but failing.
Scenario:
GameObject assigned a material with attached image in editor Load from strea$$anonymous$$gassets at runtime but the image on GameObject remains unchanged however the image assigned to the material in editor changes to a purple square
?? any ideas?
Take a look at how I did this in my project. I assign the sprite at the end of the coroutine. If you do it outside there is a chance that the image is still loading in the coroutine at the time of the assignment (thus the purple image).
Unfortunately this is not available anymore.
So you assign the sprite to a local variable inside of the IEnumerator function but it would make much more sense to set the sprite also in an object where you want the result to be stored in later maybe an asset.
https://forum.unity.com/threads/passing-ref-variable-to-coroutine.379640/#post-3003076
This here uses Action keyword to achieve this!
public static IEnumerator myCoroutine(Action<float> myVariableResult)
{
float time = 0;
float timeLimit = 1;
while (time < timeLimit)
{
// Do calculations
myVariableResult (newValue);
time += Time.deltaTime;
yield return null;
}
}
float varToBeChangedInCoroutine;
StartCoroutine (myCoroutine (result => varToBeChangedInCoroutine = result));
Answer by elpuerco63 · Jan 22, 2016 at 10:14 AM
I do this which seems the same way you are?
public IEnumerator LoadStreamingAsset(string path) {
WWW www = new WWW("file://" + System.IO.Path.Combine(Application.streamingAssetsPath, "IMG_3020.JPG"));
while (!www.isDone) {
yield return null;
}
if (!string.IsNullOrEmpty(www.error)) {
Debug.Log (www.error);
yield break;
}
else {
photoSphere.GetComponent<Renderer> ().material.mainTexture = www.texture;
}
yield return 0;
}
$$anonymous$$ake sure the file exist, and try to change the 'shared$$anonymous$$aterial' ins$$anonymous$$d of 'material'. Try with other image formats too. It is working for me that way :-S
O$$anonymous$$ so I have now narrowed it down to the image as if I choose others in strea$$anonymous$$gassests it does work, but cant understand if I drag image from resources onto material when app running it displays correctly?
The images I am trying to user are 360 images from a Ricoh Theta s which display O$$anonymous$$ when manually added to photosphere?
See an example here: http://s21.postimg.org/x0j4pde07/Q$$anonymous$$NO2875.jpg
Your answer
![](https://koobas.hobune.stream/wayback/20220613172306im_/https://answers.unity.com/themes/thub/images/avi.jpg)