使用Unity连接Bmob后端云

来源:互联网 发布:故宫淘宝营销策略分析 编辑:程序博客网 时间:2024/05/19 14:50

前言:因为要使用Unity开发一个AR游戏,我们不得不接触到Unity连接数据库的问题,我们数据库采用Bmob后端云。
Bmob后端云网址:http://www.bmob.cn/
第一步:注册Bmob后端云
这里写图片描述

第二步:拥有Bmob后端云账号以后,登录后端云,点击创建应用
这里写图片描述

这里写图片描述
创建一个MyGameTable表,添加列,分别为
这里写图片描述

以上步骤做完以后,点击设置按钮,Application ID 和REST api key需要记住,后面有需要。
这里写图片描述

后端云方面在此结束,后面需要用到unity,打开unity,新建一个工程,我的工程命名为DbTest.unity
在资源文件Assets中需要用到两个脚本和Bmob的SDK,分别为MyGameTable 和TestBmob1(此处都是用C#写的),大致页面如下:
这里写图片描述

现在分别贴出代码:

MyGameTable代码如下:

using UnityEngine;using System.Collections;using cn.bmob.io;public class MyGameTable : BmobTable{    /// <summary>    /// Bmob服务器端我们定义的表名    /// </summary>    public const string TABLENAME = "MyGameTable";    /// <summary>    /// 玩家姓名    /// </summary>    public string playerName { get; set; }    /// <summary>    /// 玩家得分    /// </summary>    public BmobInt score { get; set; }    /// <summary>    /// 成员函数    /// </summary>    /// <param name="input"></param>    public override void readFields(BmobInput input)    {        base.readFields(input);        this.score = input.getInt("score");        this.playerName = input.getString("playerName");    }      public override void write(BmobOutput output, bool all)    {        base.write(output, all);        output.Put("score", this.score);        output.Put("playerName", this.playerName);    }}

TestBmob1代码如下:

//using testBmob;public class TestBmob1 : MonoBehaviour{    private BmobUnity Bmob;    // Use this for initialization    void Start()    {        BmobDebug.Register(print);        Bmob = gameObject.GetComponent<BmobUnity>();    }    void OnGUI()    {        if (GUILayout.Button("插入数据"))        {            InsertData();        }        if (GUILayout.Button("获取数据"))        {            getRecoard();        }        if (GUILayout.Button("更新数据"))        {            updateData();        }        if (GUILayout.Button("获取全部数据"))        {            getAllInfo();        }        if (GUILayout.Button("删除数据"))        {            deleteData();        }    }    #region    /// <summary>    /// 插入数据    /// </summary>    public void InsertData()    {        MyGameTable mg = new MyGameTable();        mg.score = 95;        mg.playerName = "";        Bmob.Create(MyGameTable.TABLENAME, mg, (resp, exception) =>            {                if (exception != null)                {                    Debug.Log("保存失败,原因: " + exception.Message);                }                else                {                    Debug.Log("保存成功" + resp.createdAt);                }            });    }    /// <summary>    /// 获取表所以信息    /// </summary>    public void getAllInfo()    {        Bmob.Delete(MyGameTable.TABLENAME, "b21f213d0d", (resp, exception) =>            {                if (exception != null)                {                    Debug.Log("删除失败, 失败原因为: " + exception.Message);                    return;                }                else                {                    Debug.Log("删除成功, @" + resp.msg);                }            });    }    /// <summary>    /// 查询数据    /// </summary>    ///     ///     public void getRecoard()    {        MyGameTable mg = new MyGameTable();        Bmob.Get<MyGameTable>(MyGameTable.TABLENAME, "b21f213d0d", (resp, exception) =>            {                if (exception != null)                {                    Debug.Log("查询失败, 失败原因为: " + exception.Message);                    return;                }                MyGameTable game = resp;                Debug.Log(game.playerName + "," + game.score + "," + game.objectId);                Debug.Log("获取的对象为: " + game.ToString());            });    }    /// <summary>    /// 更新数据    /// </summary>    public void updateData()    {        MyGameTable game = new MyGameTable();        game.playerName = "wuzhang";        Bmob.Update(MyGameTable.TABLENAME, "4d05c4cd58", game, (resp, exception) =>            {                if (exception != null)                {                    Debug.Log("保存失败, 失败原因为: " + exception.Message);                    return;                }                Debug.Log("保存成功, @" + resp.updatedAt);            });    }    /// <summary>    /// 删除数据    /// </summary>    public void deleteData()    {        Bmob.Delete(MyGameTable.TABLENAME, "4d05c4cd58", (resp, exception) =>            {                if (exception != null)                {                    Debug.Log("删除失败, 失败原因为: " + exception.Message);                    return;                }                else                {                    Debug.Log("删除成功, @" + resp.msg);                }            });    }    #endregion}

现在两个脚本已经完成。开始弄Bmob提供SDK,下载网址附上:http://www.bmob.cn/downloads
这里写图片描述
这里写图片描述

下载成功后,将zip文件解压
文件结构说明
core 全部源代码放在这个目录下。直接运行csproj就可以编辑运行代码了。
BmobTest 提供了基于desktop平台的测试用例,便于接口的调试。
源码中提供了build.simple.bat用于一键生成各个平台的dll文件。

解压成功后
把源代码core/src目录拷贝到Assets/classes/下。
打开Unity重新编译,把BmobUntiy对象拖拽到摄像机(maincamera)上,重新设置AppId和RestKey。
这里写图片描述

最后点击运行,页面如下:
这里写图片描述

1 0
原创粉丝点击