Home > OS >  Application.persistenDataPath not working anymore
Application.persistenDataPath not working anymore

Time:10-30

Since yesterday evening my Application.persistenDataPath is not working anymore on one place in code. I am using it 2 times. On the first place it works on the second it doesnt work anymore...

Maybe a hint for you is, that I saved json very often yesterday. Also I had infinite loops what maybe caused the Application Settings to Crash. On Android Build it is working fine. (I get the right Path on both usages)

What I have tried below the code

  • Both in the same Class.
  • Unity 2021.3.2f
  • Using Google Firebase
  • Visual Studio 2022 Community Edition

This the code where it works: I am saving a Photo to the given Path.

//then Save To Disk as PNG
            byte[] bytes = avatarTexture.EncodeToPNG();
            var dirPath = Application.persistentDataPath   "/Resources/User/";
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
            if (File.Exists(dirPath   "avatar"   ".png"))
            {
                File.Delete(dirPath   "avatar"   ".png");
            }
            File.WriteAllBytes(dirPath   "avatar"   ".png", bytes);

            saveUserDataAsJson(getUserToken(user.DisplayName, user.Email));
            _warningLoginText.text = "";
            _confirmLoginText.text = Translation.authManager_login_notification_loginSucceeded;

This is the code where it does not work: I am saving a JSON File with User Information.

DataSnapshot snapshotYear = taskYear.Result;
_year = Convert.ToInt32(snapshotYear.Value);
FirebaseDatabaseUser tempUser = new FirebaseDatabaseUser(_token, _day, _month, _year);
var json = JsonConvert.SerializeObject(tempUser);


Debug.Log("Can see it in Console");

//After pressing Play Button in Unity
//Editor i get no Debug outprint here
Debug.Log(Application.persistentDataPath);

var folderPath = Application.persistentDataPath   "/Resources/User/";

Debug.Log("Can not see it in Console");
//HERE THE CODE STOPS AND NOTHING HAPPEN
//NO ERROR NO LOG NO FURTHER CODE WORKING
//HERE NOT WORKING ANYMORE



if (!Directory.Exists(folderPath))
{
    Directory.CreateDirectory(folderPath);
}
if (File.Exists(folderPath   "userdata"   ".json"))
{
    File.Delete(folderPath   "userdata"   ".json");
}
File.WriteAllText(folderPath   "userdata"   ".json", json);

I have tried:

  • rebuild Project Settings
  • Print the Path in Console it was empty ""
  • Cleaned GI Cache
  • Restarted my PC
  • Cloned the project new
  • Tried using Application.dataPath (only for debugging tests, not to use it in my code) also not working in Unity Play Mode but on Android it points right to the APK.

First usage (Place in Code where it works) I get Path: Computer: C:\Users\{user}\AppData\LocalLow\{company}\{app}\Resources\User\ Android: .\Android\data\com.{company}.{app}\...\Resources\User\

Second usage (Place in Code where does not work) I get Path: Computer: "" Android: ".\Android\data\com.{company}.{app}\...\Resources\User\"

I hope someone can explain the issue. I cannot solve the problem from myself. Need someone who has more background knowledge.

CodePudding user response:

so the anwer was based on the Help of @derHugo :

Some of the Unity API is using only the Unity main thread. Application.persistentDataPath is one piece of this Unity API elements. So the problem was, that in the first code snippet i was using the Unity Main Thread, but in the second code snippet i used a Background Thread. Was really easy to fix. Instead of using another thread, i used:

`<myasyncfunction>.ContinueWithOnMainThread(task => {....
    //second code snippet
})`

Refactoring tips of this Code:

  • File.Exists can be removed, it is totally redundant. WriteAllText creates or overwrites the file by default anyway.

  • The Construction of data paths over and over again makes no sense. Better way is to create public static readonly paths and use them instead.

Hope the Solution of @derHugo can help someone else.

Have a nice Day if you read this :)

  • Related