Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I might have a little bit trouble I am assuming just a simple mistake but yet I can not find the the solution well even worse the mistake, now what I am trying to do is to load all file in my folder by looping it until the max number reached

IEnumerator loadData()
{
    Debug.Log (loadNum);
    if (File.Exists (Application.persistentDataPath + "/data[" + loadNum + "].octa")) {
        BinaryFormatter loadData = new BinaryFormatter ();
        FileStream dataFile = File.Open (Application.persistentDataPath + "/data[" + loadNum + "].octa", FileMode.Open);
        playerData pData = (playerData)loadData.Deserialize (dataFile);
        dataFile.Close ();

        name_O = pData.name;
        job_O = pData.job;
        difficulty_O = pData.difficulty;

        rawAPD = name_O + "/" + job_O.ToString() + "/" + difficulty_O.ToString();
        APD = new string[loadNum += 1];

        for (var i = 0; i <= loadNum && i < loadNum; i++) 
        {
            if (APD [i] == null)
            {
                APD [i] = rawAPD;
            }

            break;
        }

        yield return new WaitForSeconds (1);
        if (loadNum != numOfSaveFile_O) 
        {
            Debug.Log ("meh");
            loadNum += 1;
            reLoop ();
        }
    }
    else 
    {
        if (loadNum != numOfSaveFile_O) 
        {
            loadNum += 1;
            reLoop ();  
        }
    }
}

void reLoop()
{
    StartCoroutine (loadData ());
}

now everything work great as expected after the yield thing the number jumps from 0 to 2 the scenario here is I do have data[0] and data[1] file in my persistent data path

Problem

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
198 views
Welcome To Ask or Share your Answers For Others

1 Answer

I am trying to do is to load all file in my folder by looping it until the max number reached.

Your code doesn't look good at-all. Lot's of memory allocation and unnecessary repeated coroutine function calls. Simply use System.IO.Directory.GetFiles() to get all path of the files in the directory then store it in an array.

I don't know what APD is but from what you are doing, you have to initialize the size with the number of items System.IO.Directory.GetFiles() returned before the for loop.

You can then use for loop to go over arrays and Read the files. Simply call the function below once.

IEnumerator loadData()
{
    string fileDir = Application.persistentDataPath;

    //Get All Files Path in in the Directory
    string[] filePath = System.IO.Directory.GetFiles(fileDir);

    //Initialize APD with the number of items in filePath
    APD = new string[filePath.Length];

    //Loop through them and read what's in there
    for (int i = 0; i < filePath.Length; i++)
    {
        if ((i + 1) % 5 == 0)
        {
            yield return null;// Wait every after 5 file read
        }

        BinaryFormatter loadData = new BinaryFormatter();
        FileStream dataFile = File.Open(filePath[i], FileMode.Open); //Open File
        playerData pData = (playerData)loadData.Deserialize(dataFile);
        dataFile.Close();

        name_O = pData.name;
        job_O = pData.job;
        difficulty_O = pData.difficulty;

        rawAPD = name_O + "/" + job_O.ToString() + "/" + difficulty_O.ToString();
        APD[i] = rawAPD;
    }
}

If you only want to read files from 1 to numOfSaveFile_O with .octa extension instead of all files in the Folder, then below is what your code should look like. This assumes that your files are named like data[0].octa, data[1].octa,data[2].octa,data[3].octa....

IEnumerator loadData2()
{
    string fileDir = Application.persistentDataPath;
    int APDIndex = 0; //Only incremented if file exist

    APD = new string[numOfSaveFile_O];
    loadNum = 0; //File starts at 0

    while (loadNum < numOfSaveFile_O)
    {
        string filePath = fileDir + "/data[" + loadNum + "].octa";

        if (File.Exists(filePath))
        {
            Debug.Log(loadNum);

            BinaryFormatter loadData = new BinaryFormatter();
            FileStream dataFile = File.Open(filePath, FileMode.Open); //Open File
            playerData pData = (playerData)loadData.Deserialize(dataFile);
            dataFile.Close();

            name_O = pData.name;
            job_O = pData.job;
            difficulty_O = pData.difficulty;

            rawAPD = name_O + "/" + job_O.ToString() + "/" + difficulty_O.ToString();
            APD[APDIndex] = rawAPD;
            APDIndex++;
        }
        loadNum++;
        yield return null; //Don't freeze Unity
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...