Search Unity

iOS StreamingAssets Path

Discussion in 'Scripting' started by Clarified, Oct 14, 2016.

  1. Clarified

    Clarified

    Joined:
    Oct 6, 2016
    Posts:
    9
    We are successfully loading textures on Android by using:

    Code (CSharp):
    1. WWW request = new WWW("jar:file://" + Application.dataPath + "!/assets/" + path);
    According to the API, the correct path on iOS should be:

    Code (CSharp):
    1. Application.dataPath + "/Raw";
    We have tried to use this in various ways:

    Code (CSharp):
    1. WWW request = new WWW("file://" + Application.dataPath + "/Raw");
    2.  
    3. WWW request = new WWW("file://" + Application.dataPath + "/Raw/" + path);
    4.  
    5. WWW request = new WWW("file://" + Application.dataPath + "/Raw" + path);
    6.  
    7. WWW request = new WWW(Application.dataPath + "/Raw/" + path);
    And many others, but none of them have worked. Printing the folder path at runtime seems to show that the "/Raw/" + path, might be pointing to the correct folder, but it is still unable to load the necessary textures.

    Any ideas?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    ow3n likes this.
  3. Clarified

    Clarified

    Joined:
    Oct 6, 2016
    Posts:
    9
    Thanks for the reply. Although we were under the impression that Application.streamingAssetsPath did not point to the correct folder on iOS or Android, I'm pretty sure we tried that path as well (at this point I feel that we have tried every possible path in existence). I will still double check and try that path as well, but it would still be of great help if someone could confirm that Application.streamingAssetsPath will definitely point to the correct folder on iOS and that the problem is somewhere else.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I'd say double check what it prints out as the path. It should be the same as the application.dataPath + part, as that is all the doc is really trying to point out. I know we use the streamingAssetsPath for a few projects and it works fine, but the mac guy handles those builds, so I can't really confirm what it points to on iOS.

    I know on android it works just fine and have not had issues with it.
     
  5. Clarified

    Clarified

    Joined:
    Oct 6, 2016
    Posts:
    9
    So I went ahead and tried the StreamingAssetsPath but no luck. This is getting quite frustrating, to be honest. I'm going to post the full code here in case there's something I'm missing.

    Code (CSharp):
    1. public static Texture2D getTextureAtUrl (string url)
    2.     {
    3.         Texture2D texture = new Texture2D(2,2);
    4.         if (Application.platform == RuntimePlatform.WindowsEditor)
    5.         {
    6.             WWW request = new WWW("file://" + Application.streamingAssetsPath + "/" + url);
    7.             while (!request.isDone) { }
    8.             texture = request.textureNonReadable as Texture2D;
    9.             return texture;
    10.         } else if (Application.platform == RuntimePlatform.Android)
    11.         {
    12.             WWW request = new WWW("jar:file://" + Application.dataPath + "!/assets/" + url);
    13.             while (!request.isDone) { }
    14.             texture = request.textureNonReadable as Texture2D;
    15.             return texture;
    16.         } else if (Application.platform == RuntimePlatform.IPhonePlayer)
    17.         {
    18.             WWW request = new WWW("file://" + Application.streamingAssetsPath + "/" + url);
    19.             while (!request.isDone) { }
    20.             texture = request.textureNonReadable as Texture2D;
    21.             return texture;
    22.         }
    23.         return texture;
    24.     }
    Both the RuntimePlatform.WindowsEditor and RuntimePlatform.Android parts work fine, but not the IPhonePlayer one. We've tried with removing the "file://" as well, removing the "/" before url (and of course the aforementioned iterations of "Application.dataPath + /Raw" but nothing works. There has to be a solution to this. Help would be greatly appreciated.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    When you say nothing works, is it the path isn't pointing to the correct directory? Or it's pointing there, but the texture itself isn't loading?

    I might also suggest trying out log viewer
    https://www.assetstore.unity3d.com/en/#!/content/12047

    It's free and allows you to bring up the debug console on the tablet/phone, thus you can see debug.log, print, or any errors you might be getting.
     
  7. Clarified

    Clarified

    Joined:
    Oct 6, 2016
    Posts:
    9
    Thanks again for the reply. Debug prints in text components while using the Application.dataPath have always shown that url is pointing to the Data/Raw directory of the installed app, but whether that's correct on our iOS device is anyones guess. I'm definetly not a Mac guy, so I really don't know how one would check whether that path is correct on the device itself. I will do a similar print in some text component tomorrow with the StreamingAssetsPath, but I don't really see how I would get much wiser from that? (Or then I've just become totally jaded with the process). Well, if we'd assume that it's the textures that are not loading but that the path is correct, where could the problem then be?
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I am not a mac/iOS guy either. I have not run into issues though with any code. (At least our mac/iOS guy hasn't come to me to say my code is broken). The only reason I suggested the log viewer asset is once on the tablet, you can see errors on it that might occur just on the tablet itself. Since it seems your issue is related to iOS devices, it'd be nice if an iOS person would be able to add some insight.
     
  9. Clarified

    Clarified

    Joined:
    Oct 6, 2016
    Posts:
    9
    I will look into the log viewer. If someone at Unity could verify that our default paths (basically the "file://" + Application" part) should be correct and that the problem is somewhere else, that would be great.
     
  10. Clarified

    Clarified

    Joined:
    Oct 6, 2016
    Posts:
    9
    So I tried out the Log Viewer (great stuff btw), and the funny thing is that there are no errors at all - the get texture method we use returns a seemingly loaded texture (unlike throwing a www error as it would in the editor if we gave it an incorrect path), but what is shown is only the red question marks. Could this point to our app using some iOS incompatible rendering settings?
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    That is interesting. What format is the image you are downloading? Also looks like you aren't the only one with the trouble
    http://answers.unity3d.com/questions/959878/ios-wwwtexture-returns-question-mark.html.

    Though I'm not sure if any of that info is useful in your case.
    I'm guessing that is the next test. Seeing if another image will work or not. Maybe a different format on the same image. The only other bit was this http://answers.unity3d.com/questions/985440/ios-www-doesnt-work-on-local.html which suggest you don't use the file:// part for the path.

    I had a look at one of our projects. We have a few ways of reading in stuff. In one case we're not using www, but instead this

    Code (CSharp):
    1.         Texture2D tex = new Texture2D(859, 646, TextureFormat.RGB24, false);
    2.  
    3.         byte[] fileData = File.ReadAllBytes(Application.persistentDataPath + "/" + thumbnailData);
    4.         tex.LoadImage(fileData);
    5.         TextureScale.Bilinear(tex, (int)largerImage.GetComponent<RectTransform>().rect.width, (int)largerImage.GetComponent<RectTransform>().rect.height);
    6.      
    7.         Sprite newThumbnailSprite = new Sprite();
    8.         newThumbnailSprite = Sprite.Create(tex, new Rect(0, 0, (int)largerImage.GetComponent<RectTransform>().rect.width, (int)largerImage.GetComponent<RectTransform>().rect.height), new Vector2(.5f, .5f), 100f);
    9.         largerImage.sprite = newThumbnailSprite;
    10.         largerImageWindow.SetActive(true);
    11.  
    I'm using a the TextureScale script to resize the texture, but that's not the important part. The main thing is this may let you test if you can load the image up and have it display correctly. thumbnailData is just the name of the file. In this case I'm using the persistentDataPath, but if you swap that for streamingAssetsPath and have the image in your streamingAssets folder, it may still work. (This was made for a screenshot save system/gallery system, so it may be useless for your testing, just a thought)
     
  12. Clarified

    Clarified

    Joined:
    Oct 6, 2016
    Posts:
    9
    Thanks again. I'm going to try out File.ReadAllBytes, as I've used it successfully before. Hopefully it's not too slow as we need extremely fast texture loading.

    EDIT* oh and we are loading standard 720p JPEGs, so nothing special there.
     
  13. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Yeah, that's nothing odd.

    ReadAllBytes may at least tell you if it's an image that works on iOS, but then it may not. I'm not sure what the difference is behind that and using a www to read in a texture. I can say that File.ReadAllBytes seemed quick enough, but I'm not sure how much you are loading. We used it for a coloring page/gallery system where a player could save tons of screenshots and then load them in. I can't recall how many we did at once, but it seemed to work well.
     
  14. Clarified

    Clarified

    Joined:
    Oct 6, 2016
    Posts:
    9
    I tried it out and it works, but it's slow. Even with our buffer solution it takes too long to buffer new images and the app hangs for ca. 200-500ms every time something is done. Probably have to tinker with some yields - the thing is that we are loading textures into the buffer and destroying them after they have been used, and because the WWW reading proved to be so fast on Android, we didn't have to do it in any async way (the textures we read and loaded into the buffer had basically none or extremely minimal delay).
     
  15. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Interesting to know. I guess the good news is the image is fine on iOS and the path seems fine, but I'm not sure if it helps with the www issue you are experiencing. Which is still a confusing problem.