Unity3D中的循环功能(0 + 1 = 2)? [英] Looping function in Unity3D (0+1 = 2)?

查看:122
本文介绍了Unity3D中的循环功能(0 + 1 = 2)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会遇到一点麻烦,我认为这很简单 错误,但我找不到更好的解决方案,更糟糕的是, 现在我要做的是通过循环播放直到它达到最大数量来加载文件夹中的所有文件

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

现在,在产生收益之后,一切都按预期运行 数字从0跳到2 这种情况是我的持久数据路径中确实有data[0]data[1]文件

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

问题

推荐答案

我想做的是通过循环播放来加载文件夹中的所有文件,直到 达到最大数量.

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

您的代码看起来一点也不好.大量的内存分配和不必要的重复协程函数调用.只需使用System.IO.Directory.GetFiles()获取目录中文件的所有路径,然后将其存储在数组中即可.

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.

我不知道APD是什么,但是根据您的操作,您必须使用for循环之前返回的System.IO.Directory.GetFiles()项数来初始化大小.

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.

然后可以使用for循环遍历数组并读取文件.只需调用下面的函数一次即可.

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

如果您只想读取扩展名为.octa的文件从1numOfSaveFile_O,而不是文件夹"中的所有文件,那么下面是您的代码外观.假设您的文件命名为data[0].octadata[1].octadata[2].octadata[3].octa....

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

这篇关于Unity3D中的循环功能(0 + 1 = 2)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆