游戏蛮牛学习群(纯技术交流,不闲聊):159852603
游戏蛮牛 手机端
开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   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;
    }
}

0 0

评论 (0 个评论)

facelist doodle 涂鸦板

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

个人分类

标签

阅读排行

评论排行

推荐博客

最新博客

返回顶部