最近,由于开发需要数据存储服务,就跑去Bmob看看,不看不要紧,发现自己以前创建的应用的数据存储服务居然变成非永久的了,只有一年的免费时间,而且还过期了。这对于我将要开发的软件时很不友好的;因此,我就只能去找与Bmob同类型的后端云服务,就是我接下来要说的leanclound。

需求分析:

1.注册登录功能

2.存储游戏数据

数据包含金币数量、钻石数量和背包道具信息,由于leancloud中不能自己设置主键,为了保证每个用户在数据表中只用唯一一条数据,所以每个用户需要在注册完成后,立即往数据表添加添加一条初始数据而且只能添加一次;

3.排行榜

记录并上传所有用户历史最佳成绩,排行显示

功能实现:

1.注册登录leanclound,新建应用

2.创建class,名字为Gamedate,添加行username(string)、diamond(string)、playeritemsmsg(any),playeritemsmsg是用于存储玩家拥有的道具信息的。

3.下载SDK,由于我们只需要存储功能,因此选择下载LeanCloud-SDK-Storage-Unity.ZIP就行了,将解压好的Plugins文件夹直接放置Unity里面就行。

4.创建脚本,添加引用,并且在Awake中添加以下代码:

    private void Awake(){LCApplication.Initialize("你的AppID", "你的AppKey", "你的MasterKey");}

这里面的在 AppID这设置里面

1.注册功能

    private void LeanCloundSign(string username, string password){if (username == string.Empty || password == string.Empty){return;}else{LCUser user = new LCUser();user["username"] = username;user["password"] = password;user.SignUp().ContinueWith(t =>{if (t.Exception != null){Debug.Log("用户名已存在");}else{Debug.Log("注册成功");}});}}

2.登录功能

    private void LeanCloundLogin(string username, string password){var res = LCUser.Login(username, password);res.ContinueWith(e =>{if (e.Exception!=null){Debug.Log("登录失败");}else{ Debug.Log("登录成功");}});}

3.添加玩家初始数据至云数据库,由于我们游戏过程中主要是数据的更新,因此,我们需要在注册完成后立即添加一条初始的数据。

 private void AddPlayerData(string username){Player player = new Player();string json = JsonUtility.ToJson(player);LCObject lc = new LCObject("Gamedate");lc["username"] = username;lc["diamond"] = 10;lc["playeritemsmsg"] = json;lc.Save().ContinueWith(e =>{if (e.Exception != null){Debug.Log(e.Exception);}else{Debug.Log("上传初始装备数据成功");}});}

4.获取玩家道具信息,在登录完成后,我们就从云数据库拉取一次玩家信息,并赋值给临时的对象来供我们调用,在玩家道具信息发生变更的时候,我们只需要直接更新这个对象,并且将这个更新好的对象传给云端数据库,就可以实现数据同步了。

    private void GetPalyerData(string username){LCQuery<LCObject> query = new LCQuery<LCObject>("Gamedate");query.WhereEqualTo("username", username);query.First().ContinueWith(e =>{if (e.Exception != null){Debug.Log(e.Exception.ToString());}else{var res = e.Result;JObject json = JObject.Parse(res.ToString());player = JsonUtility.FromJson<Player>(json["playeritemsmsg"].ToString());foreach (var item in player.ItemMsg){Debug.Log("道具序号:" + item.id + "  拥有数量:" + item.num);}}});}

5.更新本地和服务器数据

    public void  UpdatePlayerItemsMsg(int itemid,int count ){ //更新本地玩家数据var item = player.ItemMsg.Where(it => it.id == itemid).FirstOrDefault();if (item != null){if (count < 0&&item.num<-count)//不允许透支{return;}item.num += count;if (item.num==0){player.ItemMsg.Remove(item);}}else{if (count>0){player.ItemMsg.Add(new PlayItemMsg { id =itemid, num= count });}}foreach (var it in player.ItemMsg){Debug.Log("道具序号:" + it.id + "  拥有数量:" + it.num);}//上传至云数据库LCObject lc = LCObject.CreateWithoutData("Gamedate", playerobjectid);lc["playeritemsmsg"] = player;lc.Save();}

6.整合后的脚本大概是这样

using LC.Newtonsoft.Json.Linq;
using LeanCloud;
using LeanCloud.Storage;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;public class Login : MonoBehaviour
{/// <summary>/// 是否登录成功/// </summary>public static bool isLogin;/// <summary>/// 登录的账号/// </summary>public static LCUser _User;/// <summary>/// 玩家信息/// </summary>public static Player player;/// <summary>/// 玩家数据id/// </summary>public static string  playerobjectid;public InputField user;//账号public InputField pass;//密码public Button sigin;//注册/登录public Button tologin;//前往登录private bool isLogining;private void Awake(){LCApplication.Initialize("*******", "******", "*******");sigin.onClick.AddListener(()=> {if (isLogining){LeanCloundLogin(user.text,pass.text);Debug.Log("登录");}else{LeanCloundSign(user.text,pass.text);Debug.Log("注册");}});tologin.onClick.AddListener(() =>{isLogining = true;sigin.GetComponentInChildren<Text>().text = "登录";tologin.gameObject.SetActive(false);});}/// <summary>/// 注册账号/// </summary>/// <param name="username"></param>/// <param name="password"></param>private void LeanCloundSign(string username, string password){if (username == string.Empty || password == string.Empty){return;}else{LCUser user = new LCUser();user["username"] = username;user["password"] = password;user.SignUp().ContinueWith(t =>{if (t.Exception != null){Debug.Log("用户名已存在");///}else{Debug.Log("注册成功");//添加玩家初始数据至数据库AddPlayerData(username);}});}}/// <summary>/// 登录账号/// </summary>/// <param name="username"></param>/// <param name="password"></param>private void LeanCloundLogin(string username, string password){var res = LCUser.Login(username, password);res.ContinueWith(e =>{if (e.Exception!=null){Debug.Log("登录失败");}else{isLogin = true;_User = e.Result;GetPalyerData(e.Result.Username);Debug.Log("登录成功");}});}/// <summary>/// 添加玩家初始数据至云数据库/// </summary>/// <param name="username"></param>private void AddPlayerData(string username){Player player = new Player();string json = JsonUtility.ToJson(player);LCObject lc = new LCObject("Gamedate");lc["username"] = username;lc["diamond"] = 10;lc["playeritemsmsg"] = json;lc.Save().ContinueWith(e =>{if (e.Exception != null){Debug.Log(e.Exception);}else{Debug.Log("上传初始装备数据成功");}});}/// <summary>/// 获取玩家装备信息/// </summary>/// <param name="username"></param>private void GetPalyerData(string username){LCQuery<LCObject> query = new LCQuery<LCObject>("Gamedate");query.WhereEqualTo("username", username);query.First().ContinueWith(e =>{if (e.Exception != null){Debug.Log(e.Exception.ToString());}else{var res = e.Result;JObject json = JObject.Parse(res.ToString());player = JsonUtility.FromJson<Player>(json["playeritemsmsg"].ToString());foreach (var item in player.ItemMsg){Debug.Log("道具序号:" + item.id + "  拥有数量:" + item.num);}playerobjectid = json["objectId"].ToString();}});}/// <summary>/// 更新本地和服务器数据/// </summary>/// <param name="itemid"></param>/// <param name="count"></param>public void  UpdatePlayerItemsMsg(int itemid,int count ){ //更新本地玩家数据var item = player.ItemMsg.Where(it => it.id == itemid).FirstOrDefault();if (item != null){if (count < 0&&item.num<-count)//不允许透支{return;}item.num += count;if (item.num==0){player.ItemMsg.Remove(item);}}else{if (count>0){player.ItemMsg.Add(new PlayItemMsg { id =itemid, num= count });}}foreach (var it in player.ItemMsg){Debug.Log("道具序号:" + it.id + "  拥有数量:" + it.num);}//上传至云数据库LCObject lc = LCObject.CreateWithoutData("Gamedate", playerobjectid);lc["playeritemsmsg"] = player;lc.Save();}private void Update(){if (Input.GetKeyDown(KeyCode.Space)){int id = Random.Range(1, 15);int num = Random.Range(-5, 6);//随机增加或减少道具序号为id的数量numUpdatePlayerItemsMsg(id, num);}}}
[System.Serializable]
public class Player
{public string name;public List<PlayItemMsg> ItemMsg;public Player()//初始信息{name = "法外狂徒";ItemMsg = new List<PlayItemMsg>() {new PlayItemMsg() { id = 1, num = 1 },new PlayItemMsg() { id = 2, num = 1 },new PlayItemMsg() { id = 27, num = 1 },new PlayItemMsg() { id = 3, num = 10 },new PlayItemMsg() { id = 4, num = 20 }};}
}
//玩家拥有装备信息
[System.Serializable]
public class PlayItemMsg
{public int id;public int num;
}

运行结果:

改变道具数量:

至此,与云端数据库的通讯就算是完成了,接下来制作一个背包让道具信息更直观的显示出来了,还有就是排行榜的实现,以及AB包的制作、上传、下载和读取等,由于篇幅问题,这些将在下篇文章中介绍。

Unity使用leancloud开发弱数据联网游戏(注册、登录和云端数据存读)相关推荐

  1. 微信小程序-注册登录功能-本地数据保存-页面数据交替

    Title:微信小程序-注册登录功能-本地数据保存-页面数据交替 完美-小程序登录注册功能.rar-- 访问码:yqa5 1.主页面 主页面login.js代码 // pages/login/logi ...

  2. Unity开发弱数据联网游戏(背包系统)

    在上一篇文章中我们已经实现了与云端数据库的数据交互了,接下来我们要做的是制作一个背包,让我们的数据交互看起来更加的直观,有大坑,请放心阅读. https://blog.csdn.net/qq_4044 ...

  3. 使用Unity网络框架快速开发多人联网游戏(1)

    Net网络框架基于Socket网络库扩展而成的一款强大的多人在线网络游戏插件(框架),那么下面我就带领大家来学习一个这款网络插件(框架)的开发过程. 首先,你的安装unity,  只要unity支持. ...

  4. 《Python从入门到放弃》(Yanlz+Unity+SteamVR+云计算+5G+AI=VR云游戏=Python+PyCharm+人工智能+无人驾驶+数据可视化+人机交互+立钻哥哥+==)

    <Python从入门到放弃> <Python从入门到放弃> 版本 作者 参与者 完成日期 备注 YanlzAI_Python_V01_1.0 严立钻 2019.09.25 ## ...

  5. Unity 简单联网游戏(双人五子棋)开发(二)

    前言:之前我们尝试开发了一个两个比拼分数的不像游戏的超简单的弱数据联网游戏,主要是想让一些没开发过联网游戏的人了解一下最基础的流程:不过有人仍然有人私信我表示看不懂,所以这次我们再开发一个类似的游戏, ...

  6. Unity联网游戏基础原理与字节数组

    如果你要制作一个能联网的游戏,无论是网络游戏还是局域网游戏都离不开网络通信,网络游戏一般是一个服务器对应多个客户端,如图: 多个玩家访问一台服务器,每个玩家实际上是一个客户端,在这里我们列举两个小例子 ...

  7. Unity学习 — Unity与LeanCloud数据存储

    Unity 使用LeanCloud存取数据 一:LeanCloud 介绍 二:LeanCloud 特点 1:数据存储,替代传统数据库的高效云端存储 2:云引擎+云缓存 3:即时通讯 4:游戏解决方案 ...

  8. 放大招!!!落地成盒?教你开发自己的联网吃鸡游戏

    <绝地求生大逃杀>(下称PUBG)这款游戏已经发布一年了,获取了不少赞誉和奖项.然而由于神仙泛滥,让我等本来就夕阳红枪法的玩家成盒概率大大上升.虽然有大牛开发了仿PUBG的练枪游戏,但这些 ...

  9. 放大招!!!落地成盒?教你开发自己的联网吃鸡游戏 1

    <绝地求生大逃杀>(下称PUBG)这款游戏已经发布一年了,获取了不少赞誉和奖项.然而由于神仙泛滥,让我等本来就夕阳红枪法的玩家成盒概率大大上升.虽然有大牛开发了仿PUBG的练枪游戏,但这些 ...

最新文章

  1. css实现 textarea 高度自适应
  2. R语言命令行写linux,linux命令行下使用R语言绘图实例讲解
  3. 9月9日项目群管理活动讨论
  4. 【待继续研究】解析机器学习技术在反欺诈领域的应用
  5. 五分钟重温斐波那契数列
  6. linux ntptime(Network Time Protocol 网络时间协议)
  7. java锁_Java锁
  8. 与Maven 3,Failsafe和Cargo插件的集成测试
  9. 用matlab的ADC和DAC过程,谈谈我理解的ADC和DAC
  10. SpringMVC文件上传(一)
  11. Hadoop大数据环境搭建保姆级教程(完整版)
  12. 2、Scala下载、安装、环境搭建、及基本用法
  13. 外部碎片和内部碎片的区别
  14. 全国计算机等级考试——C语言二级 题库
  15. latex表格生成神器--教你如何将excel变成latex格式--教你如何做三线图
  16. EXCEL合并单元格内容并换行显示
  17. #{}和${}的使用
  18. 国内计算机类三大中文学报投稿体会(转载)
  19. python第三方库——xlrd和xlwt操作Excel文件学习
  20. KSImageNamed 安装后无效解决方法

热门文章

  1. 基于SpringBoot+MyBatis 五子棋双人对战
  2. ----不知道这是不是好友里的buge~~~
  3. 开题报告不知道怎么写?
  4. web JSP的动态交互 cs与bs结构的区别, bs结构的超详细解释,jsp的表单验证
  5. 转载百度百科 python
  6. 如何让你的blynk服务器随ubuntu系统启动?
  7. Document TitleHow to troubleshoot Funds Check Hold Issues on Payables Invoices
  8. 经常用到的Eclipse快捷键(windows版)
  9. 测试开发人才稀缺,2018测试之旅来袭
  10. 北邮信通导论第三单元数字温度计