Example #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, fishesPiranha.Count);
        countStream.Close();

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

            formatter.Serialize(stream, data);
            stream.Close();
        }
    }
Example #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);
                PiranhaData data   = formatter.Deserialize(stream) as PiranhaData;

                stream.Close();

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

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