欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity:利用PlayerPrefs储存List

程序员文章站 2022-03-02 11:44:42
...
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 利用PlayerPrefs能够储存string的原理来储存少量的int数组,直接放入项目中调用
/// </summary>
public static class IntsIOManager {
    /// <summary>
    /// 将能够储存的string转化为能够使用的string[]
    /// </summary>
    /// <param name="saveString"></param>asdasd
    /// <returns></returns>
    private static string[] SaveToUse (string saveString) {
        string[] useableString;
        useableString = saveString.Split (',');
        return useableString;
    }


    /// <summary>
    /// 将string[]转化为string
    /// </summary>
    /// <param name="useableString">需要转化的string[]</param>
    /// <returns></returns>
    private static string UseToSave (string[] useableString) {
        string saveString;
        int count = useableString.Length;
        string tempString;
        saveString = null;
        for (int i = 0; i < count; i++) {
            tempString = useableString[i];
            if (i == count - 1) {
                saveString += tempString;
            } else {
                saveString += tempString + ",";
            }
        }
        return saveString;
    }


    /// <summary>
    /// 将储存的string转化为list int
    /// </summary>
    /// <param name="saveString">储存的string</param>
    /// <returns></returns>
    private static List<int> SaveStringToInts (string saveString) {
        string[] useableString = SaveToUse (saveString);
        int count = useableString.Length;
        List<int> ints = new List<int> ();
        for (int i = 0; i < count; i++) {
            if (useableString[i] != ",") {
                ints.Add (int.Parse (useableString[i]));
            }
        }
        return ints;
    }


    /// <summary>
    /// 将list int 转换为能够储存的string
    /// </summary>
    /// <param name="ints">需要转化的string</param>
    /// <returns></returns>
    private static string IntsToSaveString (List<int> ints) {
        string saveString;
        saveString = UseToSave (ListIntToListString (ints).ToArray ());
        return saveString;
    }


    /// <summary>
    /// 将list int 转换为list string 
    /// </summary>
    /// <param name="ints">需要转换的list int </param>
    /// <returns></returns>
    private static List<string> ListIntToListString (List<int> ints) {
        int count = ints.Count;
        List<string> useableString = new List<string> ();
        for (int i = 0; i < count; i++) {
            useableString.Add (ints[i].ToString ());
        }
        return useableString;
    }


    /// <summary>
    /// 获取key下储存的int链表
    /// </summary>
    /// <param name="key">对应的标签</param>
    /// <returns></returns>
    public static List<int> GetInts (string key) {
        if (PlayerPrefs.HasKey (key)) {
            string saveString = PlayerPrefs.GetString (key);
            List<int> ints = SaveStringToInts (saveString);
            return ints;
        } else {
            return null;
        }
    }


    /// <summary>
    /// 储存List到key对应的string中
    /// </summary>
    /// <param name="key">对应的标签</param>
    /// <param name="ints">希望储存的List</param>
    public static void SetInts (string key, List<int> ints) {
        int count = ints.Count;
        // string saveString = UseToSave (ListIntToListString (ints).ToArray ());
        string saveString = IntsToSaveString(ints);
        PlayerPrefs.SetString (key, saveString);
    }
}

 

第一次发帖,希望能和大家一起交流。

相关标签: Unity