unity与android交互

来源:互联网 发布:北京数据分析师 编辑:程序博客网 时间:2024/04/30 19:34

转自:http://blog.csdn.net/itolfn/article/details/38293627?utm_source=tuicool&utm_medium=referral

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

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

unity测试代码,

[csharp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Test : MonoBehaviour {  
  5.     bool isSend = false;  
  6.     // Use this for initialization  
  7.     void Start () {  
  8.       
  9.     }  
  10.       
  11.     // Update is called once per frame  
  12.     void Update () {  
  13.       
  14.     }  
  15.     void OnGUI()  
  16.     {  
  17.         if(GUI.Button(new Rect(0,0,200,200),"one"))  
  18.         {  
  19.             using(AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))  
  20.             {  
  21.                 using(AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))  
  22.                 {  
  23.                     AndroidJavaClass cls = new AndroidJavaClass("com.dilitechcompany.demotest.UnityPlayerNativeActivity");     
  24.                       
  25.                     //cls.CallStatic("_hideView", "one");     
  26.                     jo.Call("_hideView","two");  
  27.                 }  
  28.             }  
  29.           
  30.           
  31.         }  
  32.         if(GUI.Button(new Rect(0,200,200,200),"two"))  
  33.         {  
  34.             using(AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))  
  35.             {  
  36.                 using(AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))  
  37.                 {  
  38.                     AndroidJavaClass cls = new AndroidJavaClass("com.dilitechcompany.demotest.UnityPlayerNativeActivity");     
  39.                       
  40.                     //cls.CallStatic("_Display", "one");     
  41.                     jo.Call("_Display","two");  
  42.                 }  
  43.             }  
  44.               
  45.               
  46.         }  
  47.         if(isSend)  
  48.         {  
  49.             GUI.Button(new Rect(200,0,200,200),"testbtn");  
  50.         }  
  51.     }  
  52.     void AndroidSendMessage(string name)  
  53.     {  
  54.         isSend = !isSend;  
  55.     }  
  56. }  

注解: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里了。

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. package com.dilitechcompany.demotest;  
  2.   
  3. import com.unity3d.player.UnityPlayer;  
  4. import android.app.NativeActivity;  
  5. import android.content.res.Configuration;  
  6. import android.graphics.PixelFormat;  
  7. import android.os.Bundle;  
  8. import android.util.Log;  
  9. import android.view.KeyEvent;  
  10. import android.view.View;  
  11. import android.view.Window;  
  12. import android.view.WindowManager;  
  13.   
  14. public class UnityPlayerNativeActivity extends NativeActivity  
  15. {  
  16.     protected UnityPlayer mUnityPlayer;     // don't change the name of this variable; referenced from native code  
  17.   
  18.     // UnityPlayer.init() should be called before attaching the view to a layout - it will load the native code.  
  19.     // UnityPlayer.quit() should be the last thing called - it will unload the native code.  
  20.     protected void onCreate (Bundle savedInstanceState)  
  21.     {  
  22.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  23.         super.onCreate(savedInstanceState);  
  24.           
  25.         getWindow().takeSurface(null);  
  26.         setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);  
  27.         getWindow().setFormat(PixelFormat.RGB_565);  
  28.   
  29.         mUnityPlayer = new UnityPlayer(this);  
  30.         if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar"true))  
  31.             getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  32.                                    WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  33.   
  34.         int glesMode = mUnityPlayer.getSettings().getInt("gles_mode"1);  
  35.         boolean trueColor8888 = false;  
  36.         mUnityPlayer.init(glesMode, trueColor8888);  
  37.   
  38.         View playerView = mUnityPlayer.getView();  
  39.         setContentView(playerView);  
  40.         playerView.requestFocus();        
  41. //      View rootView=mUnityPlayer.getView();  
  42. //        
  43. //      Toast.makeText(this, "class:"+rootView.getClass().getName(), Toast.LENGTH_LONG).show();  
  44. //        
  45.     }  
  46.       
  47.     public  void _hideView(String name) {  
  48.         UnityPlayer.UnitySendMessage("Main Camera""AndroidSendMessage""");  
  49.         Log.v("unity3d""hide view");  
  50.     }  
  51.     public  void _Display(String name) {  
  52.         UnityPlayer.UnitySendMessage("Main Camera""AndroidSendMessage""");  
  53.         Log.v("unity3d""display");  
  54.     }  
  55.     protected void onDestroy ()  
  56.     {  
  57.         mUnityPlayer.quit();  
  58.         super.onDestroy();  
  59.     }  
  60.   
  61.     // onPause()/onResume() must be sent to UnityPlayer to enable pause and resource recreation on resume.  
  62.     protected void onPause()  
  63.     {  
  64.         super.onPause();  
  65.         mUnityPlayer.pause();  
  66.     }  
  67.     protected void onResume()  
  68.     {  
  69.         super.onResume();  
  70.         mUnityPlayer.resume();  
  71.     }  
  72.     public void onConfigurationChanged(Configuration newConfig)  
  73.     {  
  74.         super.onConfigurationChanged(newConfig);  
  75.         mUnityPlayer.configurationChanged(newConfig);  
  76.     }  
  77.     public void onWindowFocusChanged(boolean hasFocus)  
  78.     {  
  79.         super.onWindowFocusChanged(hasFocus);  
  80.         mUnityPlayer.windowFocusChanged(hasFocus);  
  81.     }  
  82.     public boolean dispatchKeyEvent(KeyEvent event)  
  83.     {  
  84.         if (event.getAction() == KeyEvent.ACTION_MULTIPLE)  
  85.             return mUnityPlayer.onKeyMultiple(event.getKeyCode(), event.getRepeatCount(), event);  
  86.         return super.dispatchKeyEvent(event);  
  87.     }  
  88.       
  89. }  

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