Bmob使用一

Bmob使用默认的_User表创建一个管理类,提供注册和登录请求

// Made by LCX && ZY
// This FrameWork includes some tools and modules which are used usually. Feel free to contact with us at ant time.Thanks
// QQ: 594721121

using System;
using System.Collections;
using System.Collections.Generic;
using cn.bmob.api;
using cn.bmob.http;
using cn.bmob.io;
using cn.bmob.tools;
using JetBrains.Annotations;
using UnityEngine;

[RequireComponent(typeof (BmobUnity))]
public class BmobManager : MonoBehaviour
{
    public static BmobManager Instance { get; private set; }
    private BmobUnity _bmobUnity;
    // Use this for initialization
    private BmobManager()
    {
        Instance = this;
    }
    #region  event

    public event Action OnLoginSucess; 
    public event Action OnLoginFail;
    public event Action OnRegisterSuccess;
    public event Action OnRegisterFail;
    #endregion

    private void Start()
    {
        _bmobUnity = GetComponent<BmobUnity>();
        _bmobUnity.ApplicationId = 填上申请的应用ID";
        _bmobUnity.RestKey = "填上申请的应用RESTKEY";
        BmobDebug.Register(print);
        BmobDebug.level = BmobDebug.Level.TRACE;
    }
    #region Bmob方法封装
    /// <summary>
    /// 注册方法,注册成功,则调用 OnLoginSucess,注册失败则调用OnLoginFail。
    /// summary>
    /// <param name="username">用户名param>
    /// <param name="password">密码param>
    public void Login(string username,string password)
    {
       _bmobUnity.Login(username,password,((response, exception) =>
       {
           if (exception!=null)
           {
               if (OnLoginFail!=null)
               {
                   OnLoginFail();
               }
               Debug.Log("Loginfaild:"+exception.Message);
           }
           else
           {
               if (OnLoginSucess != null)
               {
                   OnLoginSucess();
               }
               Debug.Log("LoginSuccess:"+BmobUser.CurrentUser.username);
           }
       }));
    }
    /// <summary>
    /// 登录方法,自定义callback
    /// summary>
    /// <param name="username">用户名param>
    /// <param name="password">密码param>
    /// <param name="callback">回调函数param>
    public void Login(string username, string password, BmobCallback<BmobUser> callback)
    {
        _bmobUnity.Login(username, password, callback);
    }
    /// <summary>
    /// 注册方法,注册成功则调用OnRegisterSuccess,注册失败则调用OnRegisterFail
    /// summary>
    /// <param name="username">用户名param>
    /// <param name="password">密码param>
    public void Register(string username, string password)
    {
        var data = new BmobUser
        {
            username = username,
            password = password
        };
        _bmobUnity.Signup(data,((response, exception) =>
        {
            if (exception != null)
            {
                if (OnRegisterFail != null)
                {
                    OnRegisterFail();
                }
                Debug.Log("OnRegisterFail:" + exception.Message);
            }
            else
            {
                if (OnRegisterSuccess != null)
                {
                    OnRegisterSuccess();
                }
                Debug.Log("OnRegisterSuccess:" + BmobUser.CurrentUser.username);
            }
        }));
    }
    /// <summary>
    /// 注册方法,注册成功则调用OnRegisterSuccess,自定义callback
    /// summary>
    /// <param name="username">用户名param>
    /// <param name="password">密码param>
    /// <param name="callback">回调函数param>
    public void Register(string username, string password, BmobCallback<BmobUser> callback)
    {
        var data = new BmobUser
        {
            username = username,
            password = password
        };
        _bmobUnity.Signup(data, callback);
    }
#endregion
    /// <summary>
    /// 测试脚本
    /// summary>
    private void Update()
    {
        if (Input.anyKeyDown)
        {
          Register("LeoLi","li1993");
        }
    }

}

你可能感兴趣的:(Unity)