写点什么

Unity3D 和 Android 交互

作者:沃德
  • 2022 年 7 月 20 日
  • 本文字数:4192 字

    阅读完需:约 14 分钟

unity 项目/Assets/Plugins/Android 目录下是一个 android 项目目录把资源文件和所依赖的库文件放到相应的文件夹中,ps:这个目录需要首先 run 一次项目自动生成。assets 目录,libs 目录,runtime 目录等,bin 下的 class.jar 文件是需要 android 功能导出主 jar 包。1,2,主 activity 继承 UnityPlayerActivity


package lisi.dktest;
import java.text.Format;import java.text.SimpleDateFormat;import java.util.Date;
import android.os.Bundle;import android.os.Message;import android.util.Log;import android.widget.Toast;
import com.unicom.xiaowo.Pay;import com.unicom.xiaowo.Pay.UnipayPayResultListener;import com.unity3d.player.UnityPlayer;import com.unity3d.player.UnityPlayerActivity;public class InitActivity extends UnityPlayerActivity { static String[] testStr = new String[]{ "渠道计费测试0.1元" }; private final UIActivityHandler uiHandler = new UIActivityHandler(this); public static class UIActivityHandler extends WeakHandler<InitActivity> {
public UIActivityHandler(InitActivity owner) { super(owner); }
public void handleMessage(Message msg) { InitActivity owner = getOwner(); if (owner == null) { return; } owner.handleUIMessage(msg); } } public void handleUIMessage(Message msg) { PayDto payDto = (PayDto)msg.obj; switch(msg.what){ case 1: Toast.makeText(InitActivity.this, "道具购买成功!\n金额:" + payDto.getStrPrice() + "元", 1).show(); String resultDesc1 = "道具购买成功!\n金额:" + payDto.getStrPrice() + "元"; UnityPlayer.UnitySendMessage(payDto.getU3dObjName(), "OnBuySuccess", "3010 " + resultDesc1); break; case 2: Toast.makeText(InitActivity.this, "购买失败", 1).show(); String resultDesc2 = "购买失败"; UnityPlayer.UnitySendMessage(payDto.getU3dObjName(), "OnBuyFailed", "3011 " + resultDesc2); Toast.makeText(InitActivity.this, "支付失败", Toast.LENGTH_LONG).show(); break; case 3: Toast.makeText(InitActivity.this, "玩家取消支付", 1).show(); String resultDesc3 = "玩家取消支付"; UnityPlayer.UnitySendMessage(payDto.getU3dObjName(), "OnBuyFailed", "3012 " + resultDesc3); break; case 5: pay(payDto); break; } } protected final void postMessage(int what, Object object) { Message.obtain(uiHandler, what, object).sendToTarget(); } Format format = new SimpleDateFormat("yyMMddHHmmss"); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public void Buy(String ItemID, String strPrice, String U3dObjName, String strCP, String strProductName){ Log.v("Buy","ItemID:"+ItemID); Log.v("Buy","strPrice:"+strPrice); Log.v("Buy","U3dObjName:"+U3dObjName); Log.v("Buy","strCP:"+strCP); Log.v("Buy","strProductName:"+strProductName); // String orderid = format.format(new Date());// StringBuffer sb = new StringBuffer(orderid);// for(int i =0;i<36-orderid.length();i++){// sb.append('0');// } String productName =""; productName = strProductName.replace("coins", "金币"); String strProductNameStr = productName.replace(" ", ""); PayDto payDto = new PayDto(); payDto.setItemID(ItemID); payDto.setStrPrice(strPrice); payDto.setU3dObjName(U3dObjName); payDto.setStrCP(strCP); payDto.setStrProductName(strProductNameStr); postMessage(5,payDto); } public void pay(final PayDto payDto){ StringBuffer priceBuffer = new StringBuffer(payDto.getStrPrice()); String priceYuan = payDto.getStrPrice(); priceBuffer.append("00"); Log.v("Buy","price:"+priceBuffer); String code = ""; String payTypeStr = ""; String price = priceBuffer.toString(); String strPrice=""; if("100".equals(price)){ code = "161226568158"; }else if("200".equals(price)){ code = "161226568159"; strPrice = "0200"; payTypeStr = PayType.PAY_TYPE_002; }else if("300".equals(price)){ code = "161226568160"; }else if("400".equals(price)){ code = "161226568161"; strPrice = "0400"; payTypeStr = PayType.PAY_TYPE_004; }else if("500".equals(price)){ code = "161226568162"; }else if("600".equals(price)){ code = "161226568163"; }else if("700".equals(price)){ code = "161226568164"; }else if("800".equals(price)){ code = "161226568165"; strPrice = "0800"; payTypeStr = PayType.PAY_TYPE_008; }else if("900".equals(price)){ code = "161226568166"; }else if("1000".equals(price)){ code = "161226568167"; strPrice = "1100"; payTypeStr = PayType.PAY_TYPE_010; }else if("1100".equals(price)){ code = "161226568168"; }else if("1200".equals(price)){ code = "161226568169"; }else if("1300".equals(price)){ code = "161226568170"; }else if("1400".equals(price)){ code = "161226568171"; }else if("1500".equals(price)){ code = "161226568172"; }else if("1600".equals(price)){ code = "161226568173"; }else if("1700".equals(price)){ code = "161226568174"; }else if("1800".equals(price)){ code = "161226568175"; }else if("1900".equals(price)){ code = "161226568176"; }else if("2000".equals(price)){ code = "161226568177"; strPrice = "2000"; //这里有可能是1200金币,也有可能是2200金币// payTypeStr = PayType.PAY_TYPE_020; }else if("2100".equals(price)){ code = "161226568178"; }else if("2200".equals(price)){ code = "161226568179"; }else if("2300".equals(price)){ code = "161226568180"; }else if("2400".equals(price)){ code = "161226568181"; }else if("2500".equals(price)){ code = "161226568182"; }else if("2600".equals(price)){ code = "161226568183"; }else if("2700".equals(price)){ code = "161226568184"; }else if("2800".equals(price)){ code = "161226568185"; }else if("2900".equals(price)){ code = "161226568186"; }else if("3000".equals(price)){ code = "161226568187"; } String productName = payDto.getStrProductName(); if(productName!=null){ if("1200金币".equals(productName)){ payTypeStr = PayType.PAY_TYPE_020; }else if ("2200金币".equals(productName)){ payTypeStr = PayType.PAY_TYPE_120; } } Log.v("tag","code:"+code); Log.v("tag","productName:"+payDto.getStrProductName()); Log.v("tag","priceYuan:"+priceYuan); Log.v("tag","payDto.getOrderId():"+payDto.getOrderId()); String orderid =new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+strPrice+"609"+payTypeStr;// Pay.getInstance().payChannel(ChannelPayActivity.this ,// "游戏测试", "小沃科技有限公司", "130201102727","沃VAC短代测试-0.1元","0.1", sb.toString(), new UnipayPayResultListener() { Log.v("tag Pay","code:"+code); Log.v("tag Pay","productName:"+productName); Log.v("tag Pay","priceYuan:"+priceYuan); Log.v("tagPay","orderid:"+orderid); if("2200金币".equals(productName)){ postMessage(1,payDto); }else{ Pay.getInstance().payChannel(InitActivity.this, "天天萌萌推", "北京凤凰互动娱乐网络技术有限公司",code,productName,priceYuan, orderid, new UnipayPayResultListener() { @Override public void PayResult(String arg0, int arg1, int arg2, String arg3) { Log.v("Buy","arg1:"+arg1); Log.v("Buy","arg3:"+arg3); if(arg1 == 1){ postMessage(1,payDto); }else if(arg1 == 2){ postMessage(2,payDto); }else if(arg1 == 3){ postMessage(3,payDto); } } }); } }}
复制代码


调用


 using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))        {            using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))            {                using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))                {                    pay(curActivity.GetRawObject(), strAppleShopItemID, "Main Camera", "PayResult");                    //TCPClient.AddLog("Andriod_YiJie OnBuy (2):" + Time.time);                }            }        }
复制代码


用户头像

沃德

关注

我就是个零 2018.06.14 加入

程序员而已

评论

发布
暂无评论
Unity3D和Android交互_程序员_沃德_InfoQ写作社区