【游戏技术群】959392658  【游戏出海群】12067810
游戏蛮牛 手机端
开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   UnityTerminator

开发技巧-小游戏的本地存储模块

个人分类: Unity-开发技巧 | 2018-8-10 13:29
标签:本地存储
自己写着玩的,感觉挺好用,就是代码比较Low 后续再优化,话不多说,上代码

[code]csharpcode:

using UnityEngine;

public static class SaveManager : object
{
    public static int diamondAmmound = 100;      //钻石数量
    public static int bestDistance;              //最远的距离
    public static string rankTime;               //排行榜的时间
    public static int SkinID = 1;                //当前的圆圈皮肤(0是默认皮肤)
    public static int audioEnable = 1;           //音效激活
    public static int selectLineID = 1;          //当前选择线的ID;  1为默认 


    public static int Numberofgames;             //游戏次数
    public static int Numberofskins=1;           //皮肤数量
    public static int NumberofLevels;            //关卡通过数量

    //圈拥有
    public static int[] circle =new int[16] { 0,1,1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    //关卡
    public static int[] levelUnlock = new int[20] { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    /// <summary>
    /// 加载玩家数据
    /// </summary>
    public static void LoadData()
    {
        if (!PlayerPrefs.HasKey("Diamond ammound"))
            SaveData();
        else
        {
            diamondAmmound = PlayerPrefs.GetInt("Diamond ammound");
            bestDistance   = PlayerPrefs.GetInt("Best Distance");
            audioEnable    = PlayerPrefs.GetInt("AudioEnabled");
            SkinID         = PlayerPrefs.GetInt("Skin ID");
            rankTime       = PlayerPrefs.GetString("RankTime");
            selectLineID   = PlayerPrefs.GetInt("SelectLineId");
            Numberofgames  = PlayerPrefs.GetInt("Numberofgames");
            Numberofskins  = PlayerPrefs.GetInt("Numberofskins");
            NumberofLevels = PlayerPrefs.GetInt("NumberofLevels");

            circle = new int[16] 
            {
                PlayerPrefs.GetInt("skin1"),
                PlayerPrefs.GetInt("skin2"),
                PlayerPrefs.GetInt("skin3"),
                PlayerPrefs.GetInt("skin4"),
                PlayerPrefs.GetInt("skin5"),
                PlayerPrefs.GetInt("skin6"),
                PlayerPrefs.GetInt("skin7"),
                PlayerPrefs.GetInt("skin8"),
                PlayerPrefs.GetInt("skin9"),
                PlayerPrefs.GetInt("skin10"),
                PlayerPrefs.GetInt("skin11"),
                PlayerPrefs.GetInt("skin12"),
                PlayerPrefs.GetInt("skin13"),
                PlayerPrefs.GetInt("skin14"),
                PlayerPrefs.GetInt("skin15"),
                PlayerPrefs.GetInt("skin16"),                                                   
             };

            levelUnlock = GetArray("level", 20);
        }
    }

    /// <summary>
    /// 保存玩家数据
    /// </summary>
    public static void SaveData()
    {
        //金币数量
        PlayerPrefs.SetInt("Diamond ammound", diamondAmmound);

        //排行榜时间
        PlayerPrefs.SetString("RankTime", rankTime);

        //最远的距离
        if (PlayerPrefs.GetInt("Best Distance") < bestDistance)
        {
            PlayerPrefs.SetInt("Best Distance", bestDistance);
            PlayerPrefs.SetString("RankTime", rankTime);
        }
        
        //保存当前的皮肤
        PlayerPrefs.SetInt("Skin ID", SkinID);

        //保存声音
        PlayerPrefs.SetInt("AudioEnabled", audioEnable);

        //游戏次数
        PlayerPrefs.SetInt("Numberofgames", Numberofgames);

        //皮肤数量
        PlayerPrefs.SetInt("Numberofskins", Numberofskins);

        //关卡通关数
        PlayerPrefs.SetInt("NumberofLevels", NumberofLevels);

        //选择线的id
        PlayerPrefs.SetInt("SelectLineId", selectLineID);

        //保存购买皮肤的数组
        PlayerPrefs.SetInt("skin1", circle[0]);
        PlayerPrefs.SetInt("skin2", circle[1]);
        PlayerPrefs.SetInt("skin3", circle[2]);
        PlayerPrefs.SetInt("skin4", circle[3]);
        PlayerPrefs.SetInt("skin5", circle[4]);
        PlayerPrefs.SetInt("skin6", circle[5]);
        PlayerPrefs.SetInt("skin7", circle[6]);
        PlayerPrefs.SetInt("skin8", circle[7]);
        PlayerPrefs.SetInt("skin9", circle[8]);
        PlayerPrefs.SetInt("skin10", circle[9]);
        PlayerPrefs.SetInt("skin11", circle[10]);
        PlayerPrefs.SetInt("skin12", circle[11]);
        PlayerPrefs.SetInt("skin13", circle[12]);
        PlayerPrefs.SetInt("skin14", circle[13]);
        PlayerPrefs.SetInt("skin15", circle[14]);
        PlayerPrefs.SetInt("skin16", circle[15]);

        //保存关卡的数组
        CreateArray("level", 20);

        PlayerPrefs.Save();
    }


    
    //创建的数组
    public static void CreateArray(string name,int length)
    {
        for (int i = 0; i < length; i++)
        {
            PlayerPrefs.SetInt(name + i, levelUnlock[i]);
        }
    }


    //获取保存数组
    public static int[] GetArray(string name,int length)
    {
        int[] array=new int[length];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = PlayerPrefs.GetInt(name+i);
        }
        return array;
    }
}
2,新版存储模块

[code]csharpcode:

using System;
using UnityEngine;
 
/// <summary>
/// 本地数据管理类 
/// </summary>
public class GameDataManager
{
 
    /// <summary>
    /// 储存Bool
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    public static void SetBool(string key, bool value)
    {
        PlayerPrefs.SetString(key + "Bool", value.ToString());
    }
 
    /// <summary>
    /// 取Bool
    /// </summary>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static bool GetBool(string key)
    {
        try
        {
            return bool.Parse(PlayerPrefs.GetString(key + "Bool"));
        }
        catch (Exception e)
        {
            return false;
        }
       
    }
 
 
    /// <summary>
    /// 储存String
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    public static void SetString(string key, string value)
    {
        PlayerPrefs.SetString(key, value);
    }
 
    /// <summary>
    /// 取String
    /// </summary>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static string GetString(string key)
    {
        return PlayerPrefs.GetString(key);
    }
 
    /// <summary>
    /// 储存Float
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    public static void SetFloat(string key, float value)
    {
        PlayerPrefs.SetFloat(key, value);
    }
 
    /// <summary>
    /// 取Float
    /// </summary>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static float GetFloat(string key)
    {
        return PlayerPrefs.GetFloat(key);
    }
 
    /// <summary>
    /// 储存Int
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    public static void SetInt(string key, int value)
    {
        PlayerPrefs.SetInt(key, value);
    }
 
 
    /// <summary>
    /// 取Int
    /// </summary>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static int GetInt(string key)
    {
        return PlayerPrefs.GetInt(key);
    }
 
 
 
    /// <summary>
    /// 储存IntArray
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    public static void SetIntArray(string key, int[] value)
    {
 
        for (int i = 0; i < value.Length; i++)
        {
            PlayerPrefs.SetInt(key + "IntArray" + i, value[i]);
        }
        PlayerPrefs.SetInt(key + "IntArray", value.Length);
    }
 
    /// <summary>
    /// 取IntArray
    /// </summary>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static int[] GetIntArray(string key)
    {
        int[] intArr = new int[1];
        if (PlayerPrefs.GetInt(key+ "IntArray")!=0)
        {
            intArr = new int[PlayerPrefs.GetInt(key + "IntArray")];
            for (int i = 0; i < intArr.Length; i++)
            {
                intArr[i] = PlayerPrefs.GetInt(key + "IntArray" + i);
            }
        }
        return intArr;
    }
 
    /// <summary>
    /// 储存FloatArray
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    public static void SetFloatArray(string key, float[] value)
    {
 
        for (int i = 0; i < value.Length; i++)
        {
            PlayerPrefs.SetFloat(key + "FloatArray" + i, value[i]);
        }
        PlayerPrefs.SetInt(key + "FloatArray", value.Length);
    }
 
    /// <summary>
    /// 取FloatArray
    /// </summary>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static float[] GetFloatArray(string key)
    {
        float[] floatArr = new float[1];
        if (PlayerPrefs.GetInt(key + "FloatArray") != 0)
        {
            floatArr = new float[PlayerPrefs.GetInt(key + "FloatArray")];
            for (int i = 0; i < floatArr.Length; i++)
            {
                floatArr[i] = PlayerPrefs.GetFloat(key + "FloatArray" + i);
            }
        }
        return floatArr;
    }
 
 
    /// <summary>
    /// 储存StringArray
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    public static void SetStringArray(string key, string[] value)
    {
 
        for (int i = 0; i < value.Length; i++)
        {
            PlayerPrefs.SetString(key + "StringArray" + i, value[i]);
        }
        PlayerPrefs.SetInt(key + "StringArray", value.Length);
    }
 
    /// <summary>
    /// 取StringArray
    /// </summary>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static string[] GetStringArray(string key)
    {
        string[] stringArr = new string[1];
        if (PlayerPrefs.GetInt(key + "StringArray") != 0)
        {
            stringArr = new string[PlayerPrefs.GetInt(key + "StringArray")];
            for (int i = 0; i < stringArr.Length; i++)
            {
                stringArr[i] = PlayerPrefs.GetString(key + "StringArray" + i);
            }
        }
        return stringArr;
    }
 
 
    /// <summary>
    /// 储存Vector2
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    public static void SetVector2(string key, Vector2 value)
    {
        PlayerPrefs.SetFloat(key + "Vector2X", value.x);
        PlayerPrefs.SetFloat(key + "Vector2Y", value.y);
 
    }
 
    /// <summary>
    /// 取Vector2
    /// </summary>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static Vector2 GetVector2(string key)
    {
        Vector2 Vector2;
        Vector2.x = PlayerPrefs.GetFloat(key + "Vector2X");
        Vector2.y = PlayerPrefs.GetFloat(key + "Vector2Y");
        return Vector2;
    }
 
 
    /// <summary>
    /// 储存Vector3
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    public static void SetVector3(string key,Vector3 value)
    {
        PlayerPrefs.SetFloat(key + "Vector3X", value.x);
        PlayerPrefs.SetFloat(key + "Vector3Y", value.y);
        PlayerPrefs.SetFloat(key + "Vector3Z", value.z);
    }
 
    /// <summary>
    /// 取Vector3
    /// </summary>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static Vector3 GetVector3(string key)
    {
        Vector3 vector3;
        vector3.x = PlayerPrefs.GetFloat(key + "Vector3X");
        vector3.y = PlayerPrefs.GetFloat(key + "Vector3Y");
        vector3.z = PlayerPrefs.GetFloat(key + "Vector3Z");
        return vector3;
    }
 
 
    /// <summary>
    /// 储存Quaternion
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    public static void SetQuaternion(string key, Quaternion value)
    {
        PlayerPrefs.SetFloat(key + "QuaternionX", value.x);
        PlayerPrefs.SetFloat(key + "QuaternionY", value.y);
        PlayerPrefs.SetFloat(key + "QuaternionZ", value.z);
        PlayerPrefs.SetFloat(key + "QuaternionW", value.w);
    }
 
    /// <summary>
    /// 取Quaternion
    /// </summary>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static Quaternion GetQuaternion(string key)
    {
        Quaternion quaternion;
        quaternion.x = PlayerPrefs.GetFloat(key + "QuaternionX");
        quaternion.y = PlayerPrefs.GetFloat(key + "QuaternionY");
        quaternion.z = PlayerPrefs.GetFloat(key + "QuaternionZ");
        quaternion.w = PlayerPrefs.GetFloat(key + "QuaternionW");
        return quaternion;
    }
}

0 0

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

个人分类

标签

阅读排行

评论排行

推荐博客

最新博客

返回顶部