unity插件使用--Unitybmob插件使用记录

原创 2016年08月30日 20:57:05

bmob是一个比较方便快捷的后台云服务,他可以提供数据库和底层通讯,只使用它的云服务,你只需要几步就可以做好后台,把自己的数据存放到数据库中,非常方便使用和部署。bmob使用流程;现在官网注册一个账号,在建立数据库,下载使用对应unity的包。创建一个场景,添加这个脚本并且进行添加id和key。


下面是bmob的全部基本操作,更新,查找,插入,删除等基本操作。

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.tools;
using System.Net;
using cn.bmob.json;
using cn.bmob.response;
using cn.bmob.Extensions;

public class HelloBmob : MonoBehaviour
{

		private static BmobUnity Bmob;

		// Use this for initialization
		void Start ()
		{
        // Bmob.initialize("4414150cb439afdf684d37dc184e0f9f", "e1deb317442129c125b228ddf78e5f22");
				BmobDebug.Register (print);
				BmobDebug.level = BmobDebug.Level.TRACE;
				Bmob = gameObject.GetComponent<BmobUnity> ();
		}

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

		static String TABLENAME = "AR_Project";

		public class BmobGameObject : BmobTable
		{
				public BmobInt score { get; set; }

				public String playerName { get; set; }

                public String username { get; set; }
                public string describe { 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");
                        this.username = input.getString("username");
                        this.describe = input.getString("describe");
				}

				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);
                        output.Put("username",this.username);
                        output.Put("describe",this.describe);
				}
		}

		void createOpCallback ()
		{
				var data = new BmobGameObject ();
        
				System.Random rnd = new System.Random ();
				data.score = rnd.Next (-50, 170);
				data.playerName = "123";
	
		}
    
		void create ()
		{
				var data = new BmobGameObject ();
		
// 				System.Random rnd = new System.Random ();
// 				data.score = rnd.Next (-50, 170);
				data.playerName = "用户名";
				//data.cheatMode = false;

				Bmob.Create (TABLENAME, data, 
				             (resp, exception) => {
								if (exception != null) {
									print ("保存失败, 失败原因为: " + exception.Message);
									return;
								}
								
								print ("保存成功, @" + resp.createdAt);
							}
				);
				//Bmob.CreateTaskAsync (TABLENAME, data);
		}

		void UpdateGame ()
		{
				BmobGameObject game = new BmobGameObject ();
				game.playerName = "pn_123";
        Bmob.Update (TABLENAME, "58b84a1e12", game, (resp, exception) =>
				{
						if (exception != null) {
								print ("保存失败, 失败原因为: " + exception.Message);
								return;
						}

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

		void DeleteGame ()
		{
        Bmob.Delete (TABLENAME, "58b84a1e12", (resp, exception) =>
				{
						if (exception != null) {
								print ("删除失败, 失败原因为: " + exception.Message);
								return;
						}

						print ("删除成功, @" + resp.msg);
				});
		}
    
		void GetGame ()
		{
        Bmob.Get<BmobGameObject> (TABLENAME, "58b84a1e12", (resp, exception) =>
				{
						if (exception != null) {
								print ("查询失败, 失败原因为: " + exception.Message);
								return;
						}

						BmobGameObject game = resp;
						print ("获取的对象为: " + toString (game));
				});
		}

		String toString (object obj)
		{ 
				//if (obj is IBmobWritable) {
				//		return JsonAdapter.JSON.ToJson ((IBmobWritable)obj); 
				//} else
						return JsonAdapter.JSON.ToDebugJsonString (obj);
		}

		void FindQuery ()
		{
				BmobQuery query = new BmobQuery ();
        //query.WhereEqualTo ("playerName", "123");
                query.WhereExists("describe");
                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(list.Count);
                        for (int i = 0; i < list.Count; i++)
                        {
                            print(game.describe);
                        }
								print ("获取的对象为: " + toString (game));
						}
				});
		}

		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 ("获取的对象为: " + toString (game));
						}
				});
		}

		// 如果程序需要为用户添加额外的字段,需要继承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 gotCurrentUser ()
		{
			print ("登录后用户: " + toString(BmobUser.CurrentUser));
		}

		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 findAllUser ()
	{

		BmobQuery query = new BmobQuery ();
		//query.WhereEqualTo ("playerName", "123");
		query.Count ();
		Bmob.Find<MyBmobUser> (MyBmobUser.TABLE, query, (resp, exception) =>
		                           {
			if (exception != null) {
				print ("查询失败, 失败原因为: " + exception.Message);
				return;
			}
			
			List<MyBmobUser> list = resp.results;
			BmobInt count = resp.count;
			print ("满足条件的对象个数为: " + count.Get ());
			foreach (var game in list) {
				print ("获取的对象为: " + toString (game));
			}
		});

	}

		void ResetPassword ()
		{
				Bmob.Reset ("support@bmob.cn", (resp, exception) => {
						if (exception != null) {
								print ("重置密码请求失败, 失败原因为: " + exception.Message);
								return;
						}
			
						print ("重置密码请求发送成功!");
				});
		}

		void FileUpload(){
		Bmob.FileUpload("E:\\winsegit\\bmob\\bmob-csharp\\bmob-demo-csharp\\examples\\bmob-unity-demo\\README.md", (resp, exception) => {
				if (exception != null) {
					print ("上传请求失败, 失败原因为: " + exception.Message);
					return;
				}
				
				print ("上传请求发送成功!" + toString (resp));
			});
		}

		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 ("获取的对象为: " + toString (user));
						}
				});
		}

		void endpoint ()
		{
				Bmob.Endpoint<Hashtable> ("test", (resp, exception) => {
						if (exception != null) {
								print ("查询失败, 失败原因为: " + exception.Message);
								return;
						}

						print ("返回对象为: " + resp);
				});
		}

		protected void WWWFormRequest ()
		{
				WWWForm form = new WWWForm ();
				form.AddBinaryData ("tab.text", new byte[]{0,1,2});
	
				if (form != null && form.headers.Count > 0) {
						var headers = new Hashtable (); // add content-type
						IDictionaryEnumerator formHeadersIterator = form.headers.GetEnumerator ();
						while (formHeadersIterator.MoveNext())
								headers.Add ((String)formHeadersIterator.Key, formHeadersIterator.Value);
				}
	
	    
		}

		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 ();
				//}
		if (GUI.Button (new Rect ((Screen.width - btnWidth) / 2 + btnWidth, btnTop, btnWidth, btnHeight), "FileUpload")) {
			FileUpload ();
		}

				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 ();
				}

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


				
		}

}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Kinect开发学习笔记之(一)Kinect介绍和应用

Kinect开发学习笔记之(一)Kinect介绍和应用 zouxy09@qq.com http://blog.csdn.net/zouxy09   一、Kinect简介       Kine...

MySQL 高级语句

接着上篇的记录。 mysql workbench快捷键小结执行当前行,ctrl+enter , 注释/取消注释, ctrl+/, 格式化sql语句(美化sql语句), ctrl+b , 自动补全,...

unity手游开发--Unity判断网络是否连接 以及 判断是否连接WiFi

在项目或者游戏中经常需要用到判断网络是否连接,是否是在wifi状态或者是在移动流量下,unity自己已经提供了api给我们使用了。 NetworkReachability 网络可达性 描述网络的可...

Kinect

Kinect 模式Near Mode: What it is (and isn’t) Kinect tutorialsKinect v1&v2 SDK C++

Unity游戏开发——unity接入多渠道sdk

最近游戏进入最后测试和接入sdk阶段,对接了多个sdk简单记录和分享一下在接入各个渠道sdk中遇到的问题和坑,在之后或其他人看到之后可以绕过,提高效率仅此而已。 我们使用的unity开发,在选择sd...

Unity--实现屏幕多屏分屏效果

最近在做一个项目用到了多屏效果,研究了一下分享一下,实现效果如下 主要用到摄像机的 ViewportRect属性 Camera属性        Clear Flags:很容易理解,清除一些元...

Vuforia SDK---- AR开发旋转缩放模型功能

var target : Transform; var distance = 10.0; var xSpeed = 250.0; var ySpeed = 120.0; var yMinLimit...

Unity+Vuforia SDKAR开发系列教程--1.3 Vuforia SDK 技术

在准备的时候还是高通的vuforia,现在高通已经把vuforia sdk卖给了PTC,现在就已经是PTC的vuforia sdk了。 1.3 Qualcomm vuforia 技术 美国高通公司...

unity手游开发--Navmesh自带寻路组件使用注意事项

Unity自带寻路组件在做游戏时候也是非常好,可以迅速的实现寻路功能,当然你可以去写代码或者使用其他的插件来实现。 寻路主要的表现形式为,鼠标点击,角色可以到达。两个物体不相邻如何过去,攀高效果。 ...

unity 滑动条控制动画播放代码

有时候需要用到滑动条来控制,动画的播放,这样比较方便。 //动画名称 public const string ANIM_NAME = "Take 001"; //模型对象 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)