Bmob—移动后端云服务平台

对于个人或者小团队来说,开发一个有网络功能的游戏是一件不容易的事情,必须掌握一门诸如Java/.net/php这类的服务器开发语言。

Bmob云服务方便了开发者。Bmob可以给应用软件快速添加一个安全灵活的后台管理系统,方便浏览终端保存的各种信息,让开发者们可以不需要关注服务器后端的事情,只需要使用Bmob的Android/iOS/Cocos2d-x/Unity 等SDK就可以实现。

下面我们通过一个简单的实例来了解一下bmob的使用。

1.在Bmob官网上注册一个账号。(Bmob官网:http://www.bmob.cn/)

2.下载csharp sdk. 里面有三个文件:unity,Windows和WindowsPhone8,我们需要的是Unity下的Bmob-Unity.dll,将该文件放在项目中libs文件夹下。

3.在应用面板创建一个应用,获取该应用的密钥(Application ID)。在数据浏览下,创建所需要的表以及字段。(新建Score表,创建score和playerName两个字段)

4.将BmobUnity脚本挂在到Camera上,Application Id粘过来。

5.创建与Score表相对应的Model模型:BmobGameObject:BmobTable,必须实现BmobTable接口。

using UnityEngine;
using System.Collections;
using cn.bmob.io;

public class BmobGameObject : BmobTable
{
    //score、playerName是后台数据表对应的字段名称
    public BmobInt score { get; set; }
    public string playerName { get; set; }

    //从数据表中读取
    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);
    }
}

6.测试

using UnityEngine;
using System.Collections;
using cn.bmob.api;
using cn.bmob.io;
using System.Collections.Generic;

public class BmobTest : MonoBehaviour {
    private BmobUnity bmob;
    private string tableName;
    // Use this for initialization
    void Start () {
        bmob = this.GetComponent<BmobUnity>();
        tableName="Score";
    }
    
    // Update is called once per frame
    void Update ()
    {
        #region 添加一行数据 Space
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //创建数据对象
            var data = new BmobGameObject();
            //设置值    
            System.Random rnd = new System.Random();
            data.score = rnd.Next(0, 100);
            data.playerName = "player" + rnd.Next(1, 10); ;

            //添加一行数据,Score为新建的数据表名
            bmob.Create(tableName, data, (resp, exception) =>
            {
                if (exception != null)
                {
                    print("保存失败, 失败原因为: " + exception.Message);
                    return;
                }

                print("保存成功, @" + resp.createdAt);
                //resp.objectId
            });
        }
        #endregion


        #region 获取一行数据 A
        if (Input.GetKeyDown(KeyCode.A))
        {
            string objectId = "d95534876e";
            bmob.Get<BmobGameObject>(tableName, objectId, (resp, exception) =>
            {
                if (exception != null)
                {
                    print("查询失败, 失败原因为: " + exception.Message);
                    return;
                }

                BmobGameObject game = resp;
                print("获取的对象为: " + game.ToString());
                print("获取对象的分数为:"+game.score.ToString());
            });
        }       
        #endregion


        #region 修改一行数据 B
        if (Input.GetKeyDown(KeyCode.B))
        {
             BmobGameObject game = new BmobGameObject();
             game.playerName = "pn_123";
             string objectId = "d95534876e";
             bmob.Update(tableName, objectId, game, (resp, exception) =>
            {
                if (exception != null)
                {
                    print("修改失败, 失败原因为: " + exception.Message);
                    return;
                }

                print("修改成功, @" + resp.updatedAt);
            });
        }
        #endregion


        #region 删除一行数据 D
        if (Input.GetKeyDown(KeyCode.D))
        {
            string objectId = "d95534876e";
            bmob.Delete(tableName, objectId, (resp, exception) =>
            {
                if (exception != null)
                {
                    print("删除失败, 失败原因为: " + exception.Message);
                    return;
                }

                print("删除成功, @" + resp.msg);
            });
        }
        #endregion

        #region 查询所有数据 Q
        if (Input.GetKeyDown(KeyCode.Q))
        {
            //创建一个BmobQuery查询对象
            BmobQuery query = new BmobQuery();
            //查询playerName字段值为player1的记录
            query.WhereEqualTo("playerName", "player1");
            // 默认情况下,系统实际上并不会返回所有的数据,而是默认返回10条数据记录,你可以通过setLimit方法设置返回的记录数量
            //query.Limit("20");     query.Skip(20);
            //SQL中的条件查询query.Where...来判断
            bmob.Find<BmobGameObject>(tableName, query, (resp, exception) =>
            {
                if (exception != null)
                {
                    print("查询失败, 失败原因为: " + exception.Message);
                    return;
                }
                //List<T>的命名空间System.Collections.Generic.IList<T>
                //对返回结果进行处理  
                List<BmobGameObject> list = resp.results;
                foreach (var game in list)
                {
                    print("获取的对象为: " + game.ToString());
                }
            }); 
        }
        #endregion
    }
}

 bmob后台数据如下:

原文地址:https://www.cnblogs.com/greyhh/p/4713119.html