unity上面调用andorid相册的插件-unity部分
来源:互联网 发布:unity3d遮罩shader 编辑:程序博客网 时间:2024/05/16 13:56
上一篇我们已经介绍了android上面相关代码的书写,这部分主要是介绍如何在unity里面调用andorid里面自己生成的jar包和相关资源,内容如下:
1.在U3d当中新建一个工程再将相关的文件拷入如下图:(程序相关的配置文件等)
2.在bin文件里面放入刚才我们从android工程里面导出的相关jar包
3.编写脚本添加图片和截屏的脚本并将其绑定在MainCammer里面
using UnityEngine;using System.Collections;public class AddPicture : MonoBehaviour {private Texture2D tex ; void OnGUI() { if (GUI.Button(new Rect(200, 20, 100, 100), "添加图片")) { //调用我们制作的Android插件打开手机相册 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("Main", "takeSave"); }if (tex) {GUI.DrawTexture(new Rect(100,100,300,300) , tex) ;} }void message(string str){StartCoroutine (LoadTexture (str));}IEnumerator LoadTexture(string fileName){string path = "file://" + Application.persistentDataPath + "/sourcefiles/" + fileName;WWW www = new WWW (path);while(!www.isDone){}yield return www;tex = www.texture;}}
using UnityEngine;using System.Collections;using System.Collections.Generic;using System.IO;public class Screenshot : MonoBehaviour { private bool isStart = false; void OnGUI() { if(GUI.Button(new Rect(100, 20, 100, 100), "截图")) { isStart = true; } } void OnPostRender() { if (isStart) { Rect rect = new Rect(0, 0, Screen.width, Screen.height); // 先创建一个的空纹理,大小可根据实现需要来设置 Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false); // 读取屏幕像素信息并存储为纹理数据, screenShot.ReadPixels(rect, 0, 0); screenShot.Apply(); // 然后将这些纹理数据,成一个png图片文件 byte[] bytes = screenShot.EncodeToPNG(); string path = Application.persistentDataPath + "/Screenshot"; if (!Directory.Exists(path)) { //创建文件夹 Directory.CreateDirectory(path); } string filename = path + "/Screenshot" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".png"; System.IO.File.WriteAllBytes(filename, bytes); print(string.Format(filename));#if(UNITY_ANDROID) AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("RefreshPic", filename);#endif#if(UNITY_IPHONE) <span style="white-space:pre"></span>SDK.SavePhoto(filename);#endif isStart = false; } }}4.在unity里面运行项目,需要注意的是(playerSettings.bundleIdentifier:要与android的包名一致)
5.build&run
0 0
- unity上面调用andorid相册的插件-unity部分
- unity上面调用andorid相册的插件-android部分
- 使用Andorid Studio 创建unity 插件
- Unity插件之Unity调用C#编译的DLL
- unity调用eclipse导出的jar插件
- Unity调用Android保存图片到相册
- Unity调用Android 相机和相册
- Unity 调用外部DLL插件
- unity的插件 TextFx
- unity的解压缩插件
- 【Unity】一些不错的unity插件
- Unity的动画调用
- Unity插件FinalIK部分说明书翻译
- Unity插件EasyButton中委托事件的注册和调用
- Unity相册滚动效果
- Unity与IOS交互,调用IOS系统相机和相册
- unity调用系统相册无法横屏解决方法(ios)
- unity和安卓通信_调用相册_AndroidStudio
- 关于引入pods的iOS项目使用shell脚本编译的问题与探索
- HDOJ 1596 find the safest road ((最短路变形) Dijkstra && SPFA)
- How to Build a RESTful API Web Service with PHP
- 嵌入式Qt实战教程——互动出版网
- HDU 1716 排列2【DPS】
- unity上面调用andorid相册的插件-unity部分
- 【Python学习笔记】-APP图标显示未读消息数目
- 【Java编程思想第4版错误记录】Collections.addAll()静态方法与Collection.addAll()成员方法
- 欢迎使用CSDN-markdown编辑器
- 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
- 亚马逊AWS学习——VPC里面几个概念的关系
- leetcode 149 —— Max Points on a Line
- Course Schedule II
- margin:0 auto 与 text-align:center 的区别