实现安卓、ios、Windows端存档功能,目前只在这三个平台测试可用。

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using UnityEngine;

public struct Storage
{
    public DataSlot[] dataSlot;//存档槽
    public GlobalData globalData;//全局数据
};
public struct DataSlot
{
    public int fraction;//分数
    public int blood;//血量
    public int attackPower;//攻击力
    public int defenseForce;//防御力
};
public struct GlobalData
{
    public int volume;//音量
    public int version;//版本号
    public int firstPlay;//第一次运行
    public int nowSlot;//当前存档下标
};

public class StorageDemo : MonoBehaviour {

    public static Storage storage;

	// Use this for initialization
	void Start () {
        InitStorageData();
        Load();
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    public void InitStorageData()
    {
        storage.dataSlot = new DataSlot[3];
        storage.globalData = new GlobalData();
        InitDataSlot();
        InitGlobalData();
    }
    public void InitDataSlot()
    {
        for (int i = 0; i < storage.dataSlot.Length; i++)
        {
            DataSlot dataSlot = new DataSlot();
            dataSlot.fraction = 0;
            dataSlot.blood = 0;
            dataSlot.attackPower = 0;
            dataSlot.defenseForce = 0;
            storage.dataSlot[i] = dataSlot;
        }
    }

    public void InitGlobalData()
    {
        GlobalData global = new GlobalData();
        global.firstPlay = -1;
        global.nowSlot = -1;
        global.version = 0;
        global.volume = 0;
        storage.globalData = global;
    }

    public void Save()
    {
        var serializer = new XmlSerializer(typeof(Storage));
        var stream = new FileStream(Application.persistentDataPath + "/StorageDemo", FileMode.Create);



        using (stream)
        {
            serializer.Serialize(stream, storage);
        }
    }

    public void Load()
    {
        var serializer = new XmlSerializer(typeof(Storage));
        var stream = new FileStream(Application.persistentDataPath + "/StorageDemo", FileMode.Open);


        using (stream)
        {
            storage = (Storage)serializer.Deserialize(stream);
        }
    }

    public void DebugStorage()
    {
        for (int i = 0; i < storage.dataSlot.Length; i++)
        {
            Debug.Log("血槽" + i + "数值为:");
            DataSlot dataSlot = storage.dataSlot[i];
            Debug.Log("分数:" + dataSlot.fraction);
            Debug.Log("血量:" + dataSlot.blood);
            Debug.Log("攻击力:" + dataSlot.attackPower);
            Debug.Log("防御力:" + dataSlot.defenseForce);
        }
        GlobalData globalData = storage.globalData;
        Debug.Log("第一次运行游戏:"+ globalData.firstPlay);
        Debug.Log("音量:" + globalData.volume);
        Debug.Log("版本号:" + globalData.version);
        Debug.Log("当前存档:" + globalData.nowSlot);
    }
    public void PlayGame()
    {

        for (int i = 0; i < storage.dataSlot.Length; i++)
        {
            DataSlot dataSlot = new DataSlot();
            dataSlot.fraction = i + 1;
            dataSlot.blood = i + 1;
            dataSlot.attackPower = i + 1;
            dataSlot.defenseForce = i + 1;
            storage.dataSlot[i] = dataSlot;
        }
        GlobalData global = new GlobalData();
        global.firstPlay = 1;
        global.nowSlot = 1;
        global.version = 0;
        global.volume = 1;
        storage.globalData = global;

    }
    public void OnGUI()
    {
        if (GUI.Button(new Rect(10, 0, 100, 50), "Save"))
        {
            Save();
        }
        if (GUI.Button(new Rect(10, 100, 100, 50), "Load"))
        {
            Load();
        }
        if (GUI.Button(new Rect(10, 200, 100, 50), "PlayGame"))
        {
            PlayGame();
        }
        if (GUI.Button(new Rect(10, 300, 100, 50), "DebugStorage"))
        {
            DebugStorage();
        }
    }

}


用法:

创建脚本StorageDemo.cs将上面代码粘贴到脚本中,脚本挂在游戏物体上,运行项目即可。

运行效果