Unity3D 中PlayerPrefs保存或读取字符串数组的脚本方法

/// 
    /// Stores a String Array or Multiple Parameters into a Key w/ specific char seperator
    /// 
    public static bool SetStringArray(string key, char separator, params string[] stringArray)
    {
        if (stringArray.Length == 0) return false;
        try
        { PlayerPrefs.SetString(key, String.Join(separator.ToString(), stringArray)); }
        catch (Exception e)
        { return false; }
        return true;
    }

    /// 
    /// Stores a Bool Array or Multiple Parameters into a Key
    /// 
    public static bool SetStringArray(string key, params string[] stringArray)
    {
        if (!SetStringArray(key, "\n"[0], stringArray))
            return false;
        return true;
    }

    /// 
    /// Returns a String Array from a key & char seperator
    /// 
    public static string[] GetStringArray(string key, char separator)
    {
        if (PlayerPrefs.HasKey(key))
            return PlayerPrefs.GetString(key).Split(separator);
        return new string[0];
    }

    /// 
    /// Returns a Bool Array from a key
    /// 
    public static string[] GetStringArray(string key)
    {
        if (PlayerPrefs.HasKey(key))
            return PlayerPrefs.GetString(key).Split("\n"[0]);
        return new string[0];
    }

    /// 
    /// Returns a String Array from a key & char seperator
    /// Note: Uses default values to initialize if no key was found
    /// 
    public static string[] GetStringArray(string key, char separator, string defaultValue, int defaultSize)
    {
        if (PlayerPrefs.HasKey(key))
            return PlayerPrefs.GetString(key).Split(separator);
        string[] stringArray = new string[defaultSize];
        for (int i = 0; i < defaultSize; i++)
            stringArray[i] = defaultValue;
        return stringArray;
    }

    /// 
    /// Returns a String Array from a key
    /// Note: Uses default values to initialize if no key was found
    /// 
    public static String[] GetStringArray(string key, string defaultValue, int defaultSize)
    {
        return GetStringArray(key, "\n"[0], defaultValue, defaultSize);
    }

    #endregion
}

你可能感兴趣的:(游戏开发)