查看: 661|回复: 0

[其他] 开发技巧-小游戏的本地存储模块

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53448
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

发表于 2018-8-16 01:04:22 |显示全部楼层
  1. using UnityEngine;

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


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

  13.     //圈拥有
  14.     public static int[] circle =new int[16] { 0,1,1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  15.     //关卡
  16.     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 };
  17.     /// <summary>
  18.     /// 加载玩家数据
  19.     /// </summary>
  20.     public static void LoadData()
  21.     {
  22.         if (!PlayerPrefs.HasKey("Diamond ammound"))
  23.             SaveData();
  24.         else
  25.         {
  26.             diamondAmmound = PlayerPrefs.GetInt("Diamond ammound");
  27.             bestDistance   = PlayerPrefs.GetInt("Best Distance");
  28.             audioEnable    = PlayerPrefs.GetInt("AudioEnabled");
  29.             SkinID         = PlayerPrefs.GetInt("Skin ID");
  30.             rankTime       = PlayerPrefs.GetString("RankTime");
  31.             selectLineID   = PlayerPrefs.GetInt("SelectLineId");
  32.             Numberofgames  = PlayerPrefs.GetInt("Numberofgames");
  33.             Numberofskins  = PlayerPrefs.GetInt("Numberofskins");
  34.             NumberofLevels = PlayerPrefs.GetInt("NumberofLevels");

  35.             circle = new int[16]
  36.             {
  37.                 PlayerPrefs.GetInt("skin1"),
  38.                 PlayerPrefs.GetInt("skin2"),
  39.                 PlayerPrefs.GetInt("skin3"),
  40.                 PlayerPrefs.GetInt("skin4"),
  41.                 PlayerPrefs.GetInt("skin5"),
  42.                 PlayerPrefs.GetInt("skin6"),
  43.                 PlayerPrefs.GetInt("skin7"),
  44.                 PlayerPrefs.GetInt("skin8"),
  45.                 PlayerPrefs.GetInt("skin9"),
  46.                 PlayerPrefs.GetInt("skin10"),
  47.                 PlayerPrefs.GetInt("skin11"),
  48.                 PlayerPrefs.GetInt("skin12"),
  49.                 PlayerPrefs.GetInt("skin13"),
  50.                 PlayerPrefs.GetInt("skin14"),
  51.                 PlayerPrefs.GetInt("skin15"),
  52.                 PlayerPrefs.GetInt("skin16"),                                                   
  53.              };

  54.             levelUnlock = GetArray("level", 20);
  55.         }
  56.     }

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

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

  66.         //最远的距离
  67.         if (PlayerPrefs.GetInt("Best Distance") < bestDistance)
  68.         {
  69.             PlayerPrefs.SetInt("Best Distance", bestDistance);
  70.             PlayerPrefs.SetString("RankTime", rankTime);
  71.         }
  72.         
  73.         //保存当前的皮肤
  74.         PlayerPrefs.SetInt("Skin ID", SkinID);

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

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

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

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

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

  85.         //保存购买皮肤的数组
  86.         PlayerPrefs.SetInt("skin1", circle[0]);
  87.         PlayerPrefs.SetInt("skin2", circle[1]);
  88.         PlayerPrefs.SetInt("skin3", circle[2]);
  89.         PlayerPrefs.SetInt("skin4", circle[3]);
  90.         PlayerPrefs.SetInt("skin5", circle[4]);
  91.         PlayerPrefs.SetInt("skin6", circle[5]);
  92.         PlayerPrefs.SetInt("skin7", circle[6]);
  93.         PlayerPrefs.SetInt("skin8", circle[7]);
  94.         PlayerPrefs.SetInt("skin9", circle[8]);
  95.         PlayerPrefs.SetInt("skin10", circle[9]);
  96.         PlayerPrefs.SetInt("skin11", circle[10]);
  97.         PlayerPrefs.SetInt("skin12", circle[11]);
  98.         PlayerPrefs.SetInt("skin13", circle[12]);
  99.         PlayerPrefs.SetInt("skin14", circle[13]);
  100.         PlayerPrefs.SetInt("skin15", circle[14]);
  101.         PlayerPrefs.SetInt("skin16", circle[15]);

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

  104.         PlayerPrefs.Save();
  105.     }


  106.    
  107.     //创建的数组
  108.     public static void CreateArray(string name,int length)
  109.     {
  110.         for (int i = 0; i < length; i++)
  111.         {
  112.             PlayerPrefs.SetInt(name + i, levelUnlock[i]);
  113.         }
  114.     }


  115.     //获取保存数组
  116.     public static int[] GetArray(string name,int length)
  117.     {
  118.         int[] array=new int[length];
  119.         for (int i = 0; i < array.Length; i++)
  120.         {
  121.             array[i] = PlayerPrefs.GetInt(name+i);
  122.         }
  123.         return array;
  124.     }
  125. }
复制代码
来自:UnityTerminator
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-3-29 04:59 , Processed in 0.088463 second(s), 30 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部