开发环境:
Android studio 3.5.2
Unity3d 2020.3.25f1c1
目录
Android Studio部分
1,创建New Project?
2,创建新的 Module ,命名test
3,删掉默认创建的多余的?Moudle app
4,导入Unity3d的 classes.jar
5,导入Unity3d的 UnityPlayerActivity.java
6,创建MainActivity
7,Java代码部分
8,AndroidMainFest 配置
9,安卓构建aar
10,导入Unity3d
Unity3d代码部分
最终展示
拓展
Android Studio部分
1,创建New Project?
创建New Project ,Empty Activity ,package? com.example.unity2photo
2,创建新的 Module ,命名test
切换到Project ,New – Module ,Android Library 点Next 。
3,删掉默认创建的多余的?Moudle app
Open Module Settings? 选择APP 点 – 号, Yes ,再Delete。
4,导入Unity3d的 classes.jar
Unity3d 安装路径下面对应的位置:
D:\Software\Unity\2020.3.25f1c1\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Development\Classes
复制class.jar 粘贴到libs目录下面,右键classer.jar? - Add As Library
5,导入Unity3d的 UnityPlayerActivity.java
D:\Software\Unity\2020.3.25f1c1\Editor\Data\PlaybackEngines\AndroidPlayer\Source\com\unity3d\player
?复制UnityPlayerActivity.java 粘贴到 src – main- java? - com.example.test 下面
6,创建MainActivity
右键com.example.test ,New –Activity – Empty Activity
7,Java代码部分
package com.example.test;
import android.Manifest;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.os.Bundle;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.unity_player.UnityPlayerActivity;
public class MainActivity extends UnityPlayerActivity{
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
// }
// @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 获取存储权限,不然的话无法获取图片
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
}
}
// unity点击按钮触发这个方法
public void startPhoto() {
Log.d("unity","打开相册");
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 111); // 第二个参数是请求码
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 111: // 请求码
Log.d("Unity", "相册返回");
UnityPlayer.UnitySendMessage("Main Camera", "message", parseUri(data));
Log.d("unity", parseUri(data));
break;
}
}
}
public String parseUri(Intent data) {
Uri uri = data.getData();
String imagePath;
// 第二个参数是想要获取的数据
Cursor cursor = getContentResolver()
.query(uri, new String[]{MediaStore.Images.ImageColumns.DATA},
null, null, null);
if (cursor == null) {
imagePath = uri.getPath();
} else {
cursor.moveToFirst();
// 获取数据所在的列下标
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
imagePath = cursor.getString(index); // 获取指定列的数据
cursor.close();
}
return imagePath; // 返回图片地址
}
}
8,AndroidMainFest 配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<application>
<activity android:name="com.example.test.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
9,安卓构建aar
10,导入Unity3d
打包构建出test_debug.arr,解压classes.jar 替换 libs的classes.jar ,再压缩成test_debug.arr
Plugins - Android -?AndroidMainfest.xml
Plugins - Android -?test_debug.arr
Unity3d代码部分
2种方式读取图片
1,UnityWebRequestTexture
2,使用文件流读取图片
void CallAndroid()
{
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("startPhoto");
Debug.Log("点击按钮");
}
public void message(string str)
{
ShowImage(str);
Debug.Log("安卓传来信息:" + str);
//StartCoroutine(LoadTexturePreview("file://" + str));
}
//使用UnityWebRequestTexture读取图片
IEnumerator LoadTexturePreview(string path)
{
UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(path);
yield return uwr.SendWebRequest();
Debug.Log("图片地址:" + path);
if (uwr.isNetworkError || uwr.isHttpError)
{
Debug.Log("错误" + uwr.error);
}
else
{
Texture2D texture = DownloadHandlerTexture.GetContent(uwr);
// 图片显示在按钮上
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(.5f, .5f));
ImageView.sprite = sprite;
}
}
//使用文件流读取图片
private void ShowImage(string path)
{
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
byte[] bye = new byte[fileStream.Length];
fileStream.Read(bye, 0, (int)bye.Length);
fileStream.Close();
fileStream.Dispose();
fileStream = null;
Texture2D texture = new Texture2D(100, 50);
texture.LoadImage(bye);
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
ImageView.sprite = sprite;
}
最终展示
拓展
打包APK 报错
解决办法:
?
build.gralde里的classpath 指的是Android Studio的gradle插件版本。
'com.android.tools.build:gradle:3.5.2'
而gradle-wrapper.properties里的 distributionUrl才是指定的gradle版本!
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
Gradle是开源的自动化构建工具,而Gradle插件是google开发的在Android Studio中使用Gradle的插件。
参考网址:
Unity与Android的简单交互,Unity打开Android相册并调用(Unity2019以后)_不求甚解20018的博客-CSDN博客_unity打开手机相册
Unity Gradle Build
Android Studio报错:Error:Could not find com.android.tools.build:gradle:4.1 记一次不长记性的坑_LittleFogCat的博客-CSDN博客
Unity Gradle Build - 简书
Intent.ACTION_PICK
Intent.ACTION_PICK - 时光正好, - 博客园
Unity自动打包-Android Studio - build.gradle详解
Unity自动打包-Android Studio - build.gradle详解 - 简书
|