此篇博文解决的问题是:
unity3d如何与android传递复杂数据,本文以图片传递为例,实现效果为,android传递给unity3d一张图片,unity3d在页面中绘制出这张图片。在上篇文章中(点这里),我们已经实现了unity3d读取android SD卡的文件,本篇博客在上次的基础上继续实现。
一、unity3d与android 能传递什么样的数据?
基本的类型 int 、String、boolean、数组都是OK的,那如果我有复杂的数据怎么办呢?我们可以考虑用json进行序列化传递,然后unity3d再反序列化即可,因为unity3d中支持图片可以从bytes数组中读取数据,所以此处我们把android中的图片先转化为bytes数组,然后传给unity3d就可以直接用啦~
二、android端做的工作
这里以app的图标为例,在我们的MainActivity中继续添加如下代码(不明白的同学可以看之前的一篇文章(这里),也可以下载文章末尾的源码):
public class MainActivity extends UnityPlayerActivity { Context mContext = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = this; } public void StartActivity0(String name) { Intent intent = new Intent(mContext, TestActivity0.class); intent.putExtra("name", name); this.startActivity(intent); } // Unity中会调用这个方法,用于区分打开摄像机 开始本地相册 public void TakePhoto(String str) { Intent intent = new Intent(mContext, WebViewActivity.class); intent.putExtra("type", str); this.startActivity(intent); } public byte[] getIcon(String packageName) { Drawable icon = getAppIcon(packageName); Bitmap bitmap =FileUtils.DrawableToBitmap(icon); return FileUtils.BitmapToByte(bitmap); } /* * 获取程序 图标 */ public Drawable getAppIcon(String packname) { try { PackageManager pm = mContext.getPackageManager(); ApplicationInfo info = pm.getApplicationInfo(packname, 0); return info.loadIcon(pm); } catch (NameNotFoundException e) { e.printStackTrace(); } return null; } } 这里我们添加了两个方法,一个是将bitmap转化成bytes数组的方法,供unity3d调用,一个是根据包名读取app图标的方法。即可,同样的,我们clean一下,将bin目录下的jar包拷出来备用。
三、unity3d端处理
修改MainContrl这个c#文件,添加以下代码:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MainControl : MonoBehaviour { public GUISkin skin; Texture texture; Texture2D textureIcon; byte[] iconData; string path=""; // Use this for initialization void Start () { textureIcon = new Texture2D (300, 300); } // Update is called once per frame void Update () { //当用户按下手机的返回键或home键退出游戏 if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Home) ) { Application.Quit(); } } void OnGUI() { GUILayout.Button (path, GUILayout.Height (100)); if(GUILayout.Button("OPEN Activity01",GUILayout.Height(100))) { //注释1 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("StartActivity0","第一个Activity"); } if(GUILayout.Button("OPEN Activity02",GUILayout.Height(100))) { AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("StartActivity1","第二个Activit"); } GUI.skin = skin; if(GUILayout.Button("打开手机相册",GUILayout.Height(100))) { //调用我们制作的Android插件打开手机相册 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("TakePhoto","takeSave"); } if(GUILayout.Button("打开手机摄像机",GUILayout.Height(100))) { //调用我们制作的Android插件打开手机摄像机 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("TakePhoto","takePhoto"); } if(GUILayout.Button("加载app图标",GUILayout.Height(100))) { //调用我们制作的Android插件打开手机摄像机 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); iconData= jo.Call<byte[]>("getIcon","com.tencent.qqmusic"); } if(texture != null) { //注意! 我们在这里绘制Texture对象,该对象是通过 //我们制作的Android插件得到的,当这个对象不等于空的时候 //直接绘制。 GUI.DrawTexture(new Rect(300,300,600,600),texture); } if (iconData != null) { textureIcon.LoadImage (iconData); GUI.DrawTexture(new Rect(300,700,600,1000),textureIcon); } } void message(string str){ StartCoroutine(LoadTexture(str)); } IEnumerator LoadTexture(string name) { //注解1 path = "file://" + Application.persistentDataPath +"/" + name; WWW www = new WWW(path); while (!www.isDone) { } yield return www; //为贴图赋值 texture = www.texture; } }
核心代码是:
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); byte[] iconData= jo.Call<byte[]>("getIcon","com.tencent.qqmusic"); textureIcon.LoadImage (iconData)unity3d通过调用android中把bitmap转化为byte[] 的方法,拿到图片的byte数组形式信息,绘制在屏幕上就可以了。
源码:https://pan.baidu.com/s/1boX2Rqn