在这里介绍使用Android Studio调用Bmob SDK,接而实现Unity与Android通信达到游戏支付功能
下载Bmob 支付SDK,在这里我使用的是BmobPay_v3.0.3版本,可以到点击打开链接下载最新版本
1.将下载好的Bmob解压,找到BmobPay_v3.0.3_160621.jar文件,将其拖动到libs文件目录下,点击右键选择add in library
2.选择如下图的.xml文件添加一行:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3.在如下图文件下写入代码:
代码如下:
package com.liujunliang.bmobpay;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
import c.b.BP;
import c.b.PListener;
public class MainActivity extends UnityPlayerActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//初始化支付
BP.init(this,"0e6c7e349cf407d2754b914e1e1bb42e");
}
//unity将会调用addNumber函数
public int addNumber(int a,int b)
{
//与unity通信,调用untiy的方法
//参数一:挂件游戏对象
//参数二:挂件函数方法名
//参数三:函数方法参数
UnityPlayer.UnitySendMessage("Main Camera", "PrintText", "http://www.liujunliang.com.cn");
return a+b;
}
public void payByZhiFuBao()//发起支付宝支付
{
//发起支付
//参数一:商品名称
//参数二:商品描述
//参数三:商品价格
//参数四:回调商品支付状态
BP.pay("倚天剑", "道具", 0.02, true, new PListener() {
@Override
public void orderId(String s) {
}
@Override
public void succeed() {
androidSendMessageForZhiFuBao(0, "");
}
@Override
public void fail(int i, String s) {
androidSendMessageForZhiFuBao(1, s);
}
@Override
public void unknow() {
}
});
}
private void androidSendMessageForZhiFuBao(int a,String s)
{
UnityPlayer.UnitySendMessage("Main Camera","BmobPayByZhiFuBao","支付宝支付:"+a+s);
}
public void payByWeiXin()//发起微信支付
{
//发起支付
BP.pay("屠龙刀", "道具", 0.02, true, new PListener() {
@Override
public void orderId(String s) {
}
@Override
public void succeed() {
androidSendMessageForWeiXin(0,"");
}
@Override
public void fail(int i, String s) {
androidSendMessageForWeiXin(1,s);
}
@Override
public void unknow() {
}
});
}
private void androidSendMessageForWeiXin(int a,String s)
{
UnityPlayer.UnitySendMessage("Main Camera","BmobPayByWeiXin","微信支付:"+a+s);
}
}
4.android调用bmob sdk已经简单完成,接下来开启unity工程,按上一篇文章运行、classed.jar文件替换、如下图三个文件拷贝到unity中Android目录下点击打开链接
5.创建C#脚本组件调用Android工程
代码如下:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UnityCallAndroid : MonoBehaviour
{
public Text show;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
#region 支付宝支付
public void OnClickedZhiFuBao()
{
AndroidJavaClass AJC = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject AJO = AJC.GetStatic<AndroidJavaObject> ("currentActivity");
AJO.Call ("payByZhiFuBao");
}
#endregion
#region Android工程调用unity方法
public void BmobPayByZhiFuBao(string resultStr)
{
show.text = resultStr;
}
#endregion
#region 微信支付
public void OnClickedWeiXin()
{
AndroidJavaClass AJC = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject AJO = AJC.GetStatic<AndroidJavaObject> ("currentActivity");
AJO.Call ("payByWeiXin");
}
#endregion
#region Android工程调用unity方法
public void BmobPayByWeiXin(string resultStr)
{
show.text = resultStr;
}
#endregion
}
本文出自
即步的博客,请务必保留此出处
http://blog.csdn.net/qq_33747722/article/details/53408000