Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Cranke · Nov 24, 2014 at 03:35 AM · spritestreamingassets

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ladis · Sep 30, 2015 at 08:58 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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 ;-)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image elpuerco63 · Jan 22, 2016 at 09:39 AM 0
Share

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?

avatar image Ladis · Jan 22, 2016 at 09:49 AM 0
Share

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).

https://github.com/L4D15/$$anonymous$$agic_Society/blob/develop/Assets/Game/Scripts/View/UI/PersonalityView.cs

avatar image RChrispy Ladis · May 01 at 11:52 AM 0
Share

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));
avatar image LosKartoflos · Sep 26, 2018 at 01:16 PM 2
Share

shouldn't it be localFile = new WWW(finalPath); ?

avatar image
1

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;
     }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ladis · Jan 22, 2016 at 01:14 PM 0
Share

$$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

avatar image elpuerco63 · Jan 22, 2016 at 02:27 PM 0
Share

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

avatar image elpuerco63 elpuerco63 · Jan 23, 2016 at 02:45 PM 0
Share

link have finally resolved this, see here

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I'm trying to load a sprite from StreamingAssets. At the end of the import method, the sprite is null again. Why? 2 Answers

Changing sprites in game via StreamingAssets? 0 Answers

Load Spritesheet/Atlas through Streaming Assets 0 Answers

How can I reference an animator in another script so that it plays the desired animation when a button is clicked? 1 Answer

StreamingAssets + WWW url not found 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges