天天看點

用Bmob搭建簡單的伺服器步驟

1、官方文檔

http://docs.bmob.cn/unity/faststart/index.html?menukey=fast_start&key=start_unity

2、官方文檔2

https://docs.bmob.cn/data/CSharp/a_faststart/doc/index.html

3、類庫

http://docs.bmob.cn/document/unity/index.html

步驟

1、注冊bmob賬号www.bmob.cn

2、背景建立應用,然後點選應用建立表

   建立一張表,如表名MyTable

   然後在表裡建立字段列,如:

   姓名:playername,分數:score

3、擷取密鑰Application ID和REST API key,下載下傳SDK

   SDK下載下傳:

   https://github.com/bmob/BmobSharp/releases

   Demo下載下傳:

   https://github.com/bmob/bmob-demo-csharp/tree/master/examples/bmob-unity-demo

4、在Unity導入bmob

在你的項目Assets根目錄下建立"libs"目錄,将下載下傳的BmobSDK(也就是Bmob-Unity.dll)檔案放入該目錄下

5、初始化BmobSDK

   選中錄影機,把BmobUnity對象拖拽到錄影機上,然後在屬性視窗中設定 ApplicationId 和 RestKey 。

6、建立表的對應模型類

建立一個腳本類,讓它與你在bmob背景建立的表相對應,以了解為表的一個模型,該類需要繼承自BmobTable,并實作字段的讀寫方法。(其實就是用來操作表的)

using UnityEngine;
using System.Collections;
using cn.bmob.io;
public class MyGameTable : BmobTable
{
    //以下對應雲端表的字段名稱
    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);
    }
}
           

7、操作資料

這裡隻列舉了插入資料,查詢資料,更改資料的方法,其它方法參照官方文檔

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using cn.bmob.api;
using cn.bmob.io;
using cn.bmob.tools;
using System.Net;
using cn.bmob.json;
using cn.bmob.response;
using cn.bmob.Extensions;

public class HelloBomb : MonoBehaviour {

    private BmobUnity bmob;
	void Start () {
        //注冊調試列印對象,這樣才能用print方法
        BmobDebug.Register(print);
        //下面這句不知道什麼意思,沒有也可以
        BmobDebug.level = BmobDebug.Level.TRACE;
        //擷取到元件
        bmob = GetComponent<BmobUnity>();
    }
	void Update () {
        if (Input.GetKeyDown(KeyCode.C))
        {
            Create();
        }
        if (Input.GetKeyDown(KeyCode.G))
        {
            Get();
        }
	}
    //雲端表的名字
    string tableName = "MyGameTable";
    //添加資料
    void Create()
    {
        //建立資料對象
        MyGameTable data = new MyGameTable();
        //設定值    
        data.score = Random.Range(0, 100);
        data.playername = "zhangsan";

        //往表“MyGameTable”裡添加一行資料data,
        bmob.Create(tableName, data, (resp, exception) =>
        {
            if (exception != null)
            {
                //如果添加失敗,輸出錯誤原因
                print("儲存失敗, 失敗原因為: " + exception.Message);
                return;
            }
            else
            {
                //如果添加成功,輸出建立的是時間
                print("儲存成功, @" + resp.createdAt);
            }
        });
    }
    //擷取資料
    void Get()
    {
        bmob.Get<MyGameTable>(tableName, "6312fd1463", (resp, exception) =>
        {
            if (exception != null)
            {
                print("查詢失敗, 失敗原因為: " + exception.Message);
                return;
            }
            MyGameTable game = resp;
            print("擷取的對象為: " + game.ToString());
            print(game.playername);
        });
    }
    //更改資料
    void ChangeTable()
    {
        MyGameTable game = new MyGameTable();
        game.playername = "pn_123";
        bmob.Update(tableName, "6312fd1463", game, (resp, exception) =>
        {
            if (exception != null)
            {
                print("儲存失敗, 失敗原因為: " + exception.Message);
                return;
            }

            print("儲存成功, @" + resp.updatedAt);
        });
    }
}
           

備注:其它一些搭建伺服器手段

1、大型伺服器:用node.js+MySql搭建

2、輕量級伺服器wamp,可以把自己電腦用作伺服器,內建了各種伺服器需要的環境,安裝完成就可以使用,但是如果需要外網(不是區域網路)通路,就需要用其它軟體提供域名映射

3、如果是做Unity區域網路對戰用Unet,如果是做網絡對戰用Photon實時伺服器

4、用作臨時處理HTTP協定的簡單資料處理,HttpListener

5、簡單存儲資料:redis和mongodb

6、簡單的讓外網通路自己的html,用iis或者tomcat搭建站點

7、Tomcat另外一種輕量級伺服器

總之,Bmob可以說是一種非常簡單伺服器,基本上可以滿足個人調試開發。也可以在網絡搜尋其它Baas服務産品。

繼續閱讀