游戏框架Unity3D服务器Unity

快速的开发出一款有血有肉的Unity游戏

2017-03-14  本文已影响488人  大王12
没有服务端的app就是个空壳,但对个人开发者而言,服务端的搭建和部署一直是个问题,目前的方法通常就是借助云服务器,这里记录下如何快速使用后端云服务来实现所有后端的操作,无需后端工程的配合

首先说下Bmob ,他是移动云服务端加数据库。开发小游戏用这个很方便。可以实现用户的登入注册,聊天,计分。非常好用

这里是官网,是免费的 http://www.bmob.cn

这里是SDK下载页 http://www.bmob.cn/downloads

SDK下载页 C#SDK下载

记住这个必须绑定
然后我们在这个物体身上加入一个脚本 Bmob Helllo

代码如下:
using UnityEngine;
using System.Collections;
using cn.bmob.api;

public class BmobHelllo : MonoBehaviour
{
    private BmobUnity Bmob;
    void Awake ()
    {
        Bmob = this.GetComponent<BmobUnity> ();
    }
    void Update ()
    {
        if (Input.GetKeyDown (KeyCode.A)) {
            Add();
        }
    }
    //用来添加一条数据
    void Add ()
    {
        //创建数据对象
        var data = new BmobScore ();
        //设置值    
        int score = Random.Range (0, 100);
        data.score = score;
        data.playerName="huangqiaoping";
        //添加一行数据
        Bmob.Create ("Score", data, (resp, exception) => {
            if (exception != null) {
                print ("保存失败, 失败原因为: " + exception.Message);
                return;
            }
            print ("保存成功, @" + resp.createdAt);
        });          
      //添加一个名字
    }
}
然后我们创建一个脚本BmobScore
代码如下:
using UnityEngine;
using System.Collections;
using cn.bmob.io;

public class BmobScore : BmobTable
{
    //score、playerName、cheatMode是后台数据表对应的字段名称
    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); 
    }
}
这里只是初级应用,大家可以去看开发文档,写的非常详细了。

本文出自
"酷酷小乔"博客,请务必保留此出处http://5152481.blog.51cto.com/5142481/1613183

上一篇 下一篇

猜你喜欢

热点阅读