Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9086136
  • 博文数量: 1732
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19830
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1732)

文章存档

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: Android平台

2016-04-15 09:57:10

AndroidJavaProxy

AndroidJavaProxy

class in UnityEngine

Description

This class can be used to implement any java interface. Any java vm method invocation matching the interface on the proxy object will automatically be passed to the c# implementation.

// Opens an android date picker dialog and grabs the result using a callback.
using UnityEngine;
using System;

class ExampleClass : MonoBehaviour {
	private static DateTime selectedDate = DateTime.Now;

	class DateCallback : AndroidJavaProxy {
		public DateCallback() : base("android.app.DatePickerDialog$OnDateSetListener") { }
		void onDateSet(AndroidJavaObject view, int year, int monthOfYear, int dayOfMonth)
		{
			selectedDate = new DateTime(year, monthOfYear+1, dayOfMonth);
		}
	}

	void OnGUI ()
	{
		if (GUI.Button(new Rect (15, 15, 450, 75), string.Format("{0:yyyy-MM-dd}", selectedDate)))
		{
			var activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity");
			activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
			{
				new AndroidJavaObject("android.app.DatePickerDialog", activity, new DateCallback(), selectedDate.Year, selectedDate.Month-1, selectedDate.Day).Call("show");
			}));
		}
	}
}

Variables

javaInterface Java interface implemented by the proxy.

Constructors

AndroidJavaProxy

Public Functions

Invoke Called by the java vm whenever a method is invoked on the java proxy interface. You can override this to run special code on method invokation, or you can leave the implementation as is, and leave the default behavior which is to look for c# methods matching the signature of the java method.



http://blog.csdn.net/n5/article/details/50705741

1) C#调用java方法
使用AndroidJavaClass和 AndroidJavaObject可以很方便的调用java方法。最常用的是AndroidJavaObject的Call方法,文档:
这个Call是支持多参数的,第一个参数必须是方法名,第二个开始则是各种参数。如果有返回值则需要使用泛型版本Call。这个东西确实比JNI方便多了。

使用示例:

首先获取Java Object对象:


[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #if UNITY_ANDROID && !UNITY_EDITOR  
  2.         private static AndroidJavaClass androidJavaClass =  null;  
  3.         private static AndroidJavaObject androidJavaObject = null;  
  4.   
  5.         androidJavaClass=new AndroidJavaClass("com.example.MyClass");    
  6.         androidJavaObject = androidJavaClass.GetStatic("Instance");          
  7.        #endif  

androidJavaObject上调用方法:



[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #if UNITY_ANDROID && !UNITY_EDITOR  
  2.             if(androidJavaObject!=null)  
  3.             {  
  4.                 androidJavaObject.Call("myMethod",strParam,intParam);  
  5.             }  
  6.             #endif   

2) Java回调C#
也很简单,直接上代码:



[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public void callbackToUnity(int msgCode, Object jsonData)  
  2.     {  
  3.         JSONObject root = new JSONObject();   
  4.            
  5.         try {  
  6.             root.put("msg", msgCode);  
  7.             root.put("data", jsonData);  
  8.         } catch (JSONException e) {  
  9.             e.printStackTrace();  
  10.         }  
  11.          
  12.         String jsonStr = root.toString();  
  13.         UnityPlayer.UnitySendMessage(m_callbackGameObject,m_callbackFunc, jsonStr);  
  14.         logd("callbackToUnity:"+jsonStr);  
  15.     }  
  16.   
  17.       JSONObject data = new JSONObject();  
  18.         try {  
  19.             data.put("receipt", receipt);  
  20.         } catch (JSONException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.         callbackToUnity(MSG_PAY_SUCCESS, data);  

这里还是使用了json字符串将参数回调到Unity c#中。注意为了能使用UnityPlayer,必须import com.unity3d.player.UnityPlayer; 而这个类在Unity的AndroidPlayer的classes.jar中,这个jar不同的Unity版本位置不一样,Unity5.3在/Applications/Unity/PlaybackEngines/AndroidPlayer/Variations/mono/Release/Classes/中,当然这个是mono的版本,还有il2cpp的版本。将这个jar拷贝到你的java工程中即可。
另外,为了让Unity找到你的java代码,如果不是导出工程的方式做版本,可以将相关的java文件export出jar,然后把这个jar拷贝到Plugin/Android里面。
阅读(8684) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~