Unity关于easySave2 easySave3保存数据的操作;包含EasySave3运行报错的解决

81 篇文章 1 订阅

 关于easySave2 easySave3保存数据的操作;包含EasySave3运行报错的解决

 /// 数据存储路径(Easy Save的默认储存位置为:Application.persistentDataPath,为了方便我们可以给它指定储存路径) 

    #region 存储数据
    /*
    /// 
    /// 存储数据
    /// 
    private void SaveData()
    {


        ES2.Save(123, dataPath + "IntData");

        ES2.Save(1.23f, dataPath + "FloatData");

        ES2.Save(true, dataPath + "BoolData");

        ES2.Save("abc", dataPath + "StringData");

        ES2.Save(new Vector3(10, 20, 30), dataPath + "Vector3Data");

        ///< 存储transform 
        GameObject go = new GameObject();
        go.transform.localPosition = new Vector3(10, 20, 30);
        go.transform.localScale = new Vector3(3, 3, 3);
        ES2.Save(go.transform, dataPath + "TransformData");

        ///< 存储数组
        int[] intArray = new int[3] { 3, 2, 1 };
        ES2.Save(intArray, dataPath + "IntArrayData");

        ///< 存储集合
        List<string> stringList = new List<string>();
        stringList.Add("stringlist1");
        stringList.Add("stringlist2");
        stringList.Add("stringlist3");
        ES2.Save(stringList, dataPath + "StringListData");

        ///< 存储字典
        Dictionary<int, string> stringDict = new Dictionary<int, string>();
        stringDict.Add(1, "a");
        stringDict.Add(2, "b");
        ES2.Save(stringDict, dataPath + "StringDictData");

        ///< 存储栈
        Stack<string> stringStack = new Stack<string>();
        stringStack.Push("aaa");
        stringStack.Push("bbb");
        ES2.Save(stringStack, dataPath + "StringStackData");

        //保存图片 注意:该图片原文件属性“Advanced: Read/WriteEnable[*]”勾选可读写的
        // ES2.SaveImage(image.sprite.texture, "MyImage.png");
    }
    */
    #endregion


    #region 加载数据
    /*
    /// 
    /// 加载数据
    /// 
    private void LoadData()
    {
        int loadInt = ES2.Load<int>(dataPath + "IntData");
        Debug.Log("读取的int:" + loadInt);

        float loadFloat = ES2.Load<float>(dataPath + "FloatData");
        Debug.Log("读取的float:" + loadFloat);

        bool loadBool = ES2.Load<bool>(dataPath + "BoolData");
        Debug.Log("读取的bool:" + loadBool);

        string loadString = ES2.Load<string>(dataPath + "StringData");
        Debug.Log("读取的string:" + loadString);

        Vector3 loadVector3 = ES2.Load<Vector3>(dataPath + "Vector3Data");
        Debug.Log("读取的vector3:" + loadVector3);

        Transform loadTransform = ES2.Load<Transform>(dataPath + "TransformData");
        Debug.Log("读取的transform: Position++" + loadTransform.localPosition + " +++Scale++" + loadTransform.localScale);

        ///< 读取数组格式存储
        int[] loadIntArray = ES2.LoadArray<int>(dataPath + "IntArrayData");
        foreach (int i in loadIntArray)
        {
            Debug.Log("读取的数组:" + i);
        }

        ///< 读取集合格式存储
        List<string> loadStringList = ES2.LoadList<string>(dataPath + "StringListData");
        foreach (string s in loadStringList)
        {
            Debug.Log("读取的集合数据:" + s);
        }

        ///< 读取字典格式存储
        Dictionary<int, string> loadStringDict = ES2.LoadDictionary<int, string>(dataPath + "StringDictData");
        foreach (var item in loadStringDict)
        {
            Debug.Log("读取的字典数据: key" + item.Key + " value" + item.Value);
        }

        Stack<string> loadStringStack = ES2.LoadStack<string>(dataPath + "StringStackData");
        foreach (string ss in loadStringStack)
        {
            Debug.Log("读取的栈内数据:" + ss);
        }

        ///< 读取纹理 注意:该图片原文件属性“Advanced: Read/WriteEnable[*]”勾选可读写的
        Texture2D tex = ES2.LoadImage("MyImage.png");
        Sprite temp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0));
        //  showImage.sprite = temp;
    }
    */
    #endregion


    #region 删除数据
    /*
    /// 
    /// 删除数据
    /// 
    private void DeleteData()
    {
        ///< 判断是否有该存储key
        if (ES2.Exists(dataPath + "IntData"))
        {
            Debug.Log(ES2.Exists(dataPath + "IntData"));
            ///< 删除存储key
            ES2.Delete(dataPath + "IntData");
        }
    }
    */
    #endregion


    #region GUI测试用的 UI按钮
    /*
    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 100, 100), "储存数据"))
        {
            SaveData();
        }

        if (GUI.Button(new Rect(0, 100, 100, 100), "读取数据"))
        {
            LoadData();
        }

        if (GUI.Button(new Rect(0, 200, 100, 100), "删除数据"))
        {
            DeleteData();
        }
    }
    */

    #endregion


    #region  保存到本地/保存到web:

    /*


public IEnumerator UploadMesh(Mesh mesh, string tag)
{
    // Create a URL and add parameters to the end of it.
    string myURL = "http://www.server.com/ES2.php";
    myURL += "?webfilename=myFile.txt&webusername=user&webpassword=pass";

    // Create our ES2Web object.
    ES2Web web = new ES2Web(myURL + "&tag=" + tag);

    // Start uploading our data and wait for it to finish.
    yield return StartCoroutine(web.Upload(mesh));

    if (web.isError)
    {
        // Enter your own code to handle errors here.
        Debug.LogError(web.errorCode + ":" + web.error);
    }
}

public IEnumerator DownloadMesh(string tag)
{
    // Create a URL and add parameters to the end of it.
    string myURL = "http://www.server.com/ES2.php";
    myURL += "?webfilename=myFile.txt&webusername=user&webpassword=pass";

    // Create our ES2Web object.
    ES2Web web = new ES2Web(myURL + "&tag=" + tag);

    // Start downloading our data and wait for it to finish.
    yield return StartCoroutine(web.Download());

    if (web.isError)
    {
        // Enter your own code to handle errors here.
        Debug.LogError(web.errorCode + ":" + web.error);
    }
    else
    {
        // We could save our data to a local file and load from that.
        web.SaveToFile("myFile.txt");

        // Or we could just load directly from the ES2Web object.
        this.GetComponent<MeshFilter>().mesh = web.Load<Mesh>(tag);
    }
}


*/
    #endregion


    #region 最新版的easySave3运行会报错,按照以下修改即可:

    /*
     * 
    private void Start()
    {
        if (LoadEvent==LoadEvent.OnStart&&settings!=null)
        {
            Load();
        }
    }

    */
    #endregion


    #region 读取/保存 音频

    /*
    // Get the AudioSource we want to use to play our AudioClip.
    var source = this.GetComponent<AudioSource>();
    // Load an AudioClip from the streaming assets folder into our source.
    source.clip = ES3.LoadAudio(Application.streamingAssetsPath + "/AudioFile.wav");
    // Play the AudioClip we just loaded using our AudioSource.
    source.Play();

    // Get the AudioSource containing our AudioClip.
    var source = this.GetComponent<AudioSource>();
    // Save an AudioClip in Easy Save's uncompressed format.
    ES3.Save<AudioClip>("myAudio", source.clip);
    // Load the AudioClip back into the AudioSource and play it.
    source.clip = ES3.Load<AudioClip>("myAudio");
    source.Play();

        */
    #endregion


    #region 从Resource加载

    /*
    文件必须具有扩展名 例如:.bytes,以便能够从参考资料中加载它
     
    // Create an ES3Settings object to set the storage location to Resources.
    var settings = new ES3Settings();
    settings.location = ES3.Location.Resources;
 
    // Load from a file called "myFile.bytes" in Resources.
    var myValue = ES3.Load<Vector3>("myFile.bytes", settings);

    // Load from a file called "myFile.bytes" in a subfolder of Resources.
    var myValue = ES3.Load<Vector3>("myFolder/myFile.bytes");
    */

    #endregion


    #region 把 一堆键值数据 保存为string/byte[]
    /*
        // Create a new ES3File, providing a false parameter.
        var es3file = new ES3File(false);
 
        // Save your data to the ES3File.
        es3File.Save<Transform>("myTransform", this.transform);
        es3File.Save<string>("myName", myScript.name);
        // etc ...
 
        //保存为字符串
        string fileAsString = es3File.LoadRawString();
        //保存为 字节数组
        byte[] fileAsByteArray = es3File.LoadRawBytes().

    */
    #endregion

    #region 从 字符串/byte[] 读取

    /*
     * 
    //把字节数组转换成参数
    // If we're loading from a byte array, simply provide it as a parameter.
    var es3file = new ES3File(fileAsByteArray, false);

    // 把字符串转换为参数
    // 如果我们以字符串的形式加载,首先需要将其转换为字节数组,再把字节数组转换为参数。
    var es3file = new ES3File((new ES3Settings()).encoding.GetBytes(fileAsString), false);

    //再对应取出响应的值
    // Load the data from the ES3File.
    es3File.LoadInto<Transform>("myTransform", this.transform);//取出该值赋值到自身
    myScript.name = es3File.Load<string>("myName");   //取出 name
    // etc ...

     */

    #endregion


    #region  电子表格

    /*
        使用ES3Spreadsheet, Easy Save能够创建电子表格并以CSV格式存储,所有流行的电子表格软件都支持这种格式,包括      
       Excel、OSX数字和OpenOffice。

        保存:
        var sheet = new ES3Spreadsheet();
        // Add data to cells in the spreadsheet.
        for(int col=0; col<10; col++){
            for(int row=0; row<8; row++){
                sheet.SetCell<string>(col, row, "someData");
            }
        }
        sheet.Save("mySheet.csv");

    */

    /*
       如果要将数据追加到现有的电子表格,请将电子表格的追加变量设置为true。电子表格中的任何行都将被添加到保存到的行末尾。
       读取:
       // Create a blank ES3Spreadsheet.
        var sheet = new ES3Spreadsheet();
        sheet.Load("spreadsheet.csv");
        // Output the first row of the spreadsheet to console.
        for(int col=0; col<sheet.ColumnCount; col++)
            Debug.Log(sheet.GetCell<int>(col, 0));

       */

    #endregion

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用 Unity Easysave3 插件保存和读取 LuaTable 类型数据,你需要将 LuaTable 转换为字符串类型数据,然后将其保存到本地或云端。 以下是一个保存和读取 LuaTable 数据的示例代码: ```csharp using UnityEngine; using LuaInterface; using PathologicalGames; public class SaveLuaTable : MonoBehaviour { LuaTable luaTable; void Start() { // 创建 LuaTable 数据 luaTable = LuaScriptMgr.Instance.DoFile("TestTable.lua") as LuaTable; // 将 LuaTable 转换为字符串类型数据 string luaTableStr = ESSerializer.SerializeLuaTable(luaTable); // 保存字符串类型数据到本地 ES3.Save<string>("luaTable", luaTableStr); // 读取本地保存的字符串类型数据 string loadedLuaTableStr = ES3.Load<string>("luaTable"); // 将字符串类型数据转换回 LuaTable 类型数据 LuaTable loadedLuaTable = ESUtility.LuaTableFromString(loadedLuaTableStr); } } ``` 在上述代码中,我们首先创建了一个 LuaTable 数据,然后使用 `ESSerializer.SerializeLuaTable()` 方法将其转换为字符串类型数据。接着,我们使用 `ES3.Save()` 方法将字符串类型数据保存到本地。在读取本地保存数据时,我们使用 `ES3.Load()` 方法读取保存的字符串类型数据。最后,我们使用 `ESUtility.LuaTableFromString()` 方法将读取的字符串类型数据转换回 LuaTable 类型数据。 需要注意的是,Unity Easysave3 插件不支持直接保存和读取 LuaTable 类型数据,因此我们需要将其转换为字符串类型数据进行保存和读取。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AD_喵了个咪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值