首页 > 代码库 > unity与android交互

unity与android交互

网上大多数都是把android的工程放到unity里来打包成.apk。但是我感觉那样不好,因为我延用了ios的思想,unity和ios交互是使用unity导出xcode工程进行二次开发,其实unity也可以导出eclipse进行二次开发,我用的版本是unity4.3,我记得之前我用4.0导出eclipse工程会生成三个.java脚本,现在只生成一个,UnityPlayerNativeActivity,不过这个类往上继承两层也是UnityPlayerActivity,都一样一样的,只能说4.3更简化了unity和android的交互,

我做了个测试完全无压力交互。

unity测试代码,

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {
	bool isSend = false;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	void OnGUI()
	{
		if(GUI.Button(new Rect(0,0,200,200),"one"))
		{
			using(AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
			{
				using(AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
				{
					AndroidJavaClass cls = new AndroidJavaClass("com.dilitechcompany.demotest.UnityPlayerNativeActivity");   
					
					//cls.CallStatic("_hideView", "one");   
					jo.Call("_hideView","two");
				}
			}
		
		
		}
		if(GUI.Button(new Rect(0,200,200,200),"two"))
		{
			using(AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
			{
				using(AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
				{
					AndroidJavaClass cls = new AndroidJavaClass("com.dilitechcompany.demotest.UnityPlayerNativeActivity");   
					
					//cls.CallStatic("_Display", "one");   
					jo.Call("_Display","two");
				}
			}
			
			
		}
		if(isSend)
		{
			GUI.Button(new Rect(200,0,200,200),"testbtn");
		}
	}
	void AndroidSendMessage(string name)
	{
		isSend = !isSend;
	}
}

注解:unity为我们提供了调用android特定的类,AndroidJavaClass、AndroidJavaObject,这个不懂的可以查文档,网上解释一大堆,不过com.unity3d.player.UnityPlayer这个我解释一下,这个写法是一种固定写法,会android一般可以理解,就是去根据这个包路径去找到这个UnityPlayer,currentActivity这个有歧义不是当前activity而是主activity,这个不知道的可以去AndroidManifest.xml去看主activity,

这是一种固定写法,参数是写死的

另外可以使用AndroidJavaClass cls_CompassActivity = new AndroidJavaClass("com.dilitechcompany.demotest.UnityPlayerNativeActivity");  

就是你导出的android工程包名+主activity,其实道理一样的也可以调用成功,

这样交互的代码只能写在主activity里了。

package com.dilitechcompany.demotest;

import com.unity3d.player.UnityPlayer;
import android.app.NativeActivity;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class UnityPlayerNativeActivity extends NativeActivity
{
	protected UnityPlayer mUnityPlayer;		// don't change the name of this variable; referenced from native code

	// UnityPlayer.init() should be called before attaching the view to a layout - it will load the native code.
	// UnityPlayer.quit() should be the last thing called - it will unload the native code.
	protected void onCreate (Bundle savedInstanceState)
	{
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		
		getWindow().takeSurface(null);
		setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
		getWindow().setFormat(PixelFormat.RGB_565);

		mUnityPlayer = new UnityPlayer(this);
		if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
			getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
			                       WindowManager.LayoutParams.FLAG_FULLSCREEN);

		int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
		boolean trueColor8888 = false;
		mUnityPlayer.init(glesMode, trueColor8888);

		View playerView = mUnityPlayer.getView();
		setContentView(playerView);
		playerView.requestFocus();		
//		View rootView=mUnityPlayer.getView();
//		
//		Toast.makeText(this, "class:"+rootView.getClass().getName(), Toast.LENGTH_LONG).show();
//		
	}
	
	public  void _hideView(String name) {
		UnityPlayer.UnitySendMessage("Main Camera", "AndroidSendMessage", "");
		Log.v("unity3d", "hide view");
	}
	public  void _Display(String name) {
		UnityPlayer.UnitySendMessage("Main Camera", "AndroidSendMessage", "");
		Log.v("unity3d", "display");
	}
	protected void onDestroy ()
	{
		mUnityPlayer.quit();
		super.onDestroy();
	}

	// onPause()/onResume() must be sent to UnityPlayer to enable pause and resource recreation on resume.
	protected void onPause()
	{
		super.onPause();
		mUnityPlayer.pause();
	}
	protected void onResume()
	{
		super.onResume();
		mUnityPlayer.resume();
	}
	public void onConfigurationChanged(Configuration newConfig)
	{
		super.onConfigurationChanged(newConfig);
		mUnityPlayer.configurationChanged(newConfig);
	}
	public void onWindowFocusChanged(boolean hasFocus)
	{
		super.onWindowFocusChanged(hasFocus);
		mUnityPlayer.windowFocusChanged(hasFocus);
	}
	public boolean dispatchKeyEvent(KeyEvent event)
	{
		if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
			return mUnityPlayer.onKeyMultiple(event.getKeyCode(), event.getRepeatCount(), event);
		return super.dispatchKeyEvent(event);
	}
	
}

最后java代码附上,这也是个3dview视图,操作方便,二次开发使用,大家可以看log信息,也可以反调unity看交互验证结果。