- Home /
Application.persistentDataPath not working on ios but working on Mac Editor and Android
Hello , I am working on ios 8 and Unity 4.6.2f1 Pro
This piece of code is working on Android flawlessly. I am writing a texture from another method and reading it here. But on ios this doesn't seem to work. After this output :
Started Loading image from : /var/mobile/Containers/Data/Application/XXXXXXX-XXXXX-XXXX-XXXX-XXXXX/Documents/Portrait/playerPortrait2.jpg
It gets stuck on the while loop waiting for the www request endlessly.
string portraitPath = Path.Combine(Application.persistentDataPath,"Portrait");
string PlayerPortraitImageBigPth = Path.Combine(portraitPath,"playerPortrait.jpg");
WWW fileReq = new WWW("file://" + PlayerPortraitImageBigPth);
playerPortraitBig = new Texture2D(400,400);
if(File.Exists(PlayerPortraitImageBigPth))
{
Texture2D image = new Texture2D(400,400);
Debug.Log ("Started Loading image from : " + PlayerPortraitImageBigPth);
while(!fileReq.isDone)
{
//IT GETS STUCK HERE WHILE WWW LOADING
}
Debug.Log ("Done Loading image from : " + PlayerPortraitImageBigPth);
if(fileReq.isDone){
fileReq.LoadImageIntoTexture(image);
playerPortraitBig = image;
}
else{
Debug.Log ("File read failed. the path looked : " + PlayerPortraitImageBigPth);
playerPortraitBig = null;
}
}
else{
Debug.Log ("Folder failed. the path looked : " + PlayerPortraitImageBigPth);
playerPortraitBig = null;
}
Any help would be great. BTW i have tried file:// and file:/// with no success. also tried hardcoding the file path directly like :
Application.persistentDataPath + "/Portrait/playerPortrait2.jpg";
I suspect your while loop is eating all the CPU time.
I would love to check that option. Will think of a way. But a really cheap android tablet is working in this matter? (www.bountyhunt.net this is the game and the android version has this on the multiplayer screen. You can take a photo and use it as your avatar in the game. )
First thing is, you start your WWW file-loading request and THEN check if the file exists. Surely that should be the other way round?
Secondly, I think Graham's right. What you're doing is testing if fileReq has returned an error 'while' you know that it hasn't finished yet. Let it finish, then see if there's an error.
To do this, try putting the file loading into a coroutine. After constructing the WWW object you'd have
yield return fileReq;
and only after that do you have the code to test & process the response to the WWW request.
That was right. I haven't made it a coroutine in the first place because i wanted it to be a static method. But i made it a coroutine now and it works. I would be glad if you can write your comments as answer and i can select it as answered. Thank you very much. :)
Answer by bariscigal · Feb 06, 2015 at 01:23 PM
Ok as #Bonfire Boy and #Graham Dunnett pointed out it was a problem of cpu load and bad coding (my bad) Here is the working solution. All credits goes to names mentioned
IEnumerator LoadImageFromPath(){
portraitDirectory = Path.Combine(Application.persistentDataPath,"Portrait");
PlayerPortraitImage = Path.Combine(portraitDirectory,"playerPortrait.jpg");
playerPortraitBig = new Texture2D(400,400);
if(File.Exists(PlayerPortraitImage))
{
yield return StartCoroutine(RequestImage());
if(fileReq.bytes.Length > 0){
Debug.Log ("Done Loading image from : " + PlayerPortraitImage);
fileReq.LoadImageIntoTexture(playerPortraitBig);
//do anything with the acquired image as texture
}
else{
Debug.Log ("File read failed. the path looked : " + PlayerPortraitImage);
GeneralTools.playerPortraitBig = null;
}
}
else{
Debug.Log ("Folder failed. the path looked : " + PlayerPortraitImage);
GeneralTools.playerPortraitBig = null;
}
}
IEnumerator RequestImage(){
Debug.Log ("Started Loading image from : " + PlayerPortraitImage);
fileReq = new WWW("file://" + PlayerPortraitImage);
while(!fileReq.isDone){
yield return null;
}
yield break;
}
Your answer
Follow this Question
Related Questions
file saved to iOS persistent directory exists during session but is gone next session 0 Answers
How do you create a subfolder inside persistentDataPath in iOS? 0 Answers
How to download asset bundles and storing it locally in ios 0 Answers
persistentDataPath not working on iOS. UNITY 4.PRO. 3 Answers
Delete screenshots from Gallery 0 Answers