Bmob后端雲之雲數據庫的使用


 把BmobUnity放到libs里面  下載連接:https://raw.githubusercontent.com/bmob/bmob-demo-csharp/master/examples/bmob-unity-demo/Assets/libs/Bmob-Unity.dll

1.把BmobUnity掛在游戲物體上更改上面兩個字符串屬性

把 id 和key改成自己的

 

 

 

 //下面步驟看代碼:

 

using UnityEngine;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using cn.bmob.api;
using cn.bmob.io;
using cn.bmob.common;
using cn.bmob.tools;
using System.Net;

public class HelloBmob : MonoBehaviour
{

    private BmobUnity Bmob;

    // Use this for initialization
    void Start()
    {
        BmobDebug.Register(print);
        Bmob = GetComponent<BmobUnity>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }
    }

    const String TABLENAME = "T_BMOB_API";

    public class BmobGameObject : BmobTable
    {
        public BmobInt score { get; set; }
        public String playerName { get; set; }
        public BmobBoolean cheatMode { get; set; }

        public override void readFields(BmobInput input)
        {
            base.readFields(input);

            this.score = input.getInt("score");
            this.cheatMode = input.getBoolean("cheatMode");
            this.playerName = input.getString("playerName");
        }

        public override void write(BmobOutput output, Boolean all)
        {
            base.write(output, all);

            output.Put("score", this.score);
            output.Put("cheatMode", this.cheatMode);
            output.Put("playerName", this.playerName);
        }
    }
    //數據插入
    void create()
    {
        var data = new BmobGameObject();
        
        System.Random rnd = new System.Random();
        data.score = rnd.Next(-50, 170);
        data.playerName = "123";
        data.cheatMode = false;

        Bmob.Create(TABLENAME, data, (resp, exception) =>
        {
            if(exception != null){
                print("保存失敗, 失敗原因為: " + exception.Message);
                return;
            }

            print("保存成功, @" + resp.createdAt);
        });
    }
    //數據更新
    void UpdateGame()
    {
        BmobGameObject game = new BmobGameObject();
        game.playerName = "pn_123";
        Bmob.Update(TABLENAME, "68ee8131ca", game, (resp, exception) =>
        {
            if (exception != null)
            {
                print("保存失敗, 失敗原因為: " + exception.Message);
                return;
            }

            print("保存成功, @" + resp.updatedAt);
        });
    }
    //刪除objectid="68ee8131ca"的數據
    void DeleteGame()
    {
        Bmob.Delete(TABLENAME, "68ee8131ca", (resp, exception) =>
        {
            if (exception != null)
            {
                print("刪除失敗, 失敗原因為: " + exception.Message);
                return;
            }

            print("刪除成功, @" + resp.msg);
        });
    }
    //查詢一行數據 objectId="68ee8131ca"
    void GetGame()
    {
        Bmob.Get<BmobGameObject>(TABLENAME, "68ee8131ca", (resp, exception) =>
        {
            if (exception != null)
            {
                print("查詢失敗, 失敗原因為: " + exception.Message);
                return;
            }

            BmobGameObject game = resp;
            print("獲取的對象為: " + game.ToString());
        });
    }
    //條件查詢數據 playerName="123" 的所有行數據一行就是一個BmobGameObject
    void FindQuery()
    {
        BmobQuery query = new BmobQuery();
        query.WhereEqualTo("playerName", "123");
        Bmob.Find<BmobGameObject>(TABLENAME, query, (resp, exception) =>
        {
            if (exception != null)
            {
                print("查詢失敗, 失敗原因為: " + exception.Message);
                return;
            }

            List<BmobGameObject> list = resp.results;
            foreach (var game in list)
            {
                print("獲取的對象為: " + game.ToString());
            }
        });
    }
    //條件查詢數據 playerName="123" 的個數
    void FindQueryWithCount()
    {
        BmobQuery query = new BmobQuery();
        query.WhereEqualTo("playerName", "123");
        query.Count ();
        Bmob.Find<BmobGameObject>(TABLENAME, query, (resp, exception) =>
        {
            if (exception != null)
            {
                print("查詢失敗, 失敗原因為: " + exception.Message);
                return;
            }

            List<BmobGameObject> list = resp.results;
            BmobInt count = resp.count;
            print("滿足條件的對象個數為: " + count.Get());
            foreach (var game in list)
            {
                print("獲取的對象為: " + game.ToString());
            }
        });
    }

    // 如果程序需要為用戶添加額外的字段,需要繼承BmobUser
    public class MyBmobUser : BmobUser
    {
        public BmobInt life { get; set; }
        public BmobInt attack { get; set; }

        public override void write(BmobOutput output, bool all)
        {
            base.write(output, all);

            output.Put("life", this.life);
            output.Put("attack", this.attack);
        }

        public override void readFields(BmobInput input)
        {
            base.readFields(input);

            this.life = input.getInt("life");
            this.attack = input.getInt("attack");
        }
    }
    //插入數據
    void Signup()
    {
        MyBmobUser user = new MyBmobUser();
        user.username = "test";
        user.password = "123456";
        user.email = "support@bmob.cn";
        user.life = 0;
        user.attack = 0;

        Bmob.Signup<MyBmobUser>(user, (resp, exception) =>
        {
            if (exception != null)
            {
                print("注冊失敗, 失敗原因為: " + exception.Message);
                return;
            }

            print("注冊成功");
        });
    }
    //登錄
    void Login(){
        Bmob.Login<MyBmobUser>("test", "123456", (resp, exception) => {
            if (exception != null)
            {
                print("登錄失敗, 失敗原因為: " + exception.Message);
                return;
            }
            
            print("登錄成功, @" + resp.username + "(" + resp.life + ")$[" + resp.sessionToken + "]");

            print("登錄成功, 當前用戶對象Session: " + BmobUser.CurrentUser.sessionToken);
        });
    }
    //更新
    void updateuser()
    {
        Bmob.Login<MyBmobUser>("test", "123456", (resp, ex) =>
                   {
            print(resp.sessionToken);
            MyBmobUser u = new MyBmobUser();
            u.attack = 1000;
            Bmob.UpdateUser(resp.objectId, u, resp.sessionToken, (updateResp, updateException) =>
                            {
                if (updateException != null)
                {
                    print("保存失敗, 失敗原因為: " + updateException.Message);
                    return;
                }
                
                print("保存成功, @" + updateResp.updatedAt);
            });
        });
    }
    //重置
    void ResetPassword(){
        Bmob.Reset("support@bmob.cn", (resp, exception) => {
            if (exception != null)
            {
                print("重置密碼請求失敗, 失敗原因為: " + exception.Message);
                return;
            }
            
            print("重置密碼請求發送成功!");
        });
    }
    //查詢用戶
    void FindUser()
    {
        BmobQuery query = new BmobQuery();
        query.WhereEqualTo("username", "test");
        Bmob.Find<MyBmobUser>(BmobUser.TABLE, query, (resp, exception) =>
                                  {
            if (exception != null)
            {
                print("查詢失敗, 失敗原因為: " + exception.Message);
                return;
            }
            
            List<MyBmobUser> list = resp.results;
            foreach (var user in list)
            {
                print("獲取的對象為: " + user.ToString());
            }
        });
    }
    
    void endpoint()
    {
        Bmob.Endpoint<Hashtable>("test", (resp, exception) => {
            if (exception != null)
            {
                print("查詢失敗, 失敗原因為: " + exception.Message);
                return;
            }

            print("返回對象為: " + resp);
        });
    }

    void OnGUI()
    {
        float scale = 2.0f;

        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            scale = Screen.width / 320;
        }

        float btnWidth = 100 * scale;
        float btnHeight = 25 * scale;
        float btnTop = 10 * scale;
        GUI.skin.button.fontSize = Convert.ToInt32(12 * scale);

        if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 - btnWidth, btnTop, btnWidth, btnHeight), "Create"))
        {
            create();
        }
        if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 + btnWidth, btnTop, btnWidth, btnHeight), "Update"))
        {
            UpdateGame();
        }

        btnTop += btnHeight + 10 * scale;
        if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 - btnWidth, btnTop, btnWidth, btnHeight), "QueryAll"))
        {
            FindQuery();
        }
        if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 + btnWidth, btnTop, btnWidth, btnHeight), "QueryWithCount"))
        {
            FindQueryWithCount();
        }

        btnTop += btnHeight + 10 * scale;
        if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 - btnWidth, btnTop, btnWidth, btnHeight), "Get"))
        {
            GetGame();
        }
        if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 + btnWidth, btnTop, btnWidth, btnHeight), "Delete"))
        {
            DeleteGame();
        }

        btnTop += btnHeight + 10 * scale;
        if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 - btnWidth, btnTop, btnWidth, btnHeight), "Signup"))
        {
            Signup();
        }
        if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 + btnWidth, btnTop, btnWidth, btnHeight), "Login"))
        {
            Login();
        }

        btnTop += btnHeight + 10 * scale;
        if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 - btnWidth, btnTop, btnWidth, btnHeight), "updateuser"))
        {
            updateuser();
        }
        if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 + btnWidth, btnTop, btnWidth, btnHeight), "ResetPassword"))
        {
            ResetPassword();
        }

        btnTop += btnHeight + 10 * scale;
        if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 - btnWidth, btnTop, btnWidth, btnHeight), "FindUser"))
        {
            FindUser();
        }
        if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 + btnWidth, btnTop, btnWidth, btnHeight), "endpoint"))
        {
            endpoint();
        }
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM