コード例 #1
0
    void SaveFish()
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string          path      = Application.persistentDataPath + FISH_SUB + SceneManager.GetActiveScene().buildIndex;
        string          countPath = Application.persistentDataPath + FISH_COUNT_SUB + SceneManager.GetActiveScene().buildIndex;

        FileStream countStream = new FileStream(countPath, FileMode.Create);

        formatter.Serialize(countStream, fishesNarwhal.Count);
        countStream.Close();

        for (int i = 0; i < fishesNarwhal.Count; i++)
        {
            FileStream  stream = new FileStream(path + i, FileMode.Create);
            NarwhalData data   = new NarwhalData(fishesNarwhal[i]);

            formatter.Serialize(stream, data);
            stream.Close();
        }
    }
コード例 #2
0
    void LoadFish()
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string          path      = Application.persistentDataPath + FISH_SUB + SceneManager.GetActiveScene().buildIndex;
        string          countPath = Application.persistentDataPath + FISH_COUNT_SUB + SceneManager.GetActiveScene().buildIndex;
        int             fishCount = 0;

        if (File.Exists(countPath))
        {
            FileStream countStream = new FileStream(countPath, FileMode.Open);

            fishCount = (int)formatter.Deserialize(countStream);
            countStream.Close();
        }
        else
        {
            Debug.LogError("Path not found in " + countPath);
        }

        for (int i = 0; i < fishCount; i++)
        {
            if (File.Exists(path + i))
            {
                FileStream  stream = new FileStream(path + i, FileMode.Open);
                NarwhalData data   = formatter.Deserialize(stream) as NarwhalData;

                stream.Close();

                Vector3 position = new Vector3(data.position[0], data.position[1], data.position[2]);

                AINarwhal fish = Instantiate(fishPrefab, position, Quaternion.identity);
            }
            else
            {
                Debug.LogError("Path not found in " + path + i);
            }
        }
    }