所需软件:
相信你看到这篇文章前已经看过类似的文章 , 但是可能你对 androidstudio 知之甚少 所以导致你无法前进 ,
废话不多说跟着我一步一步操作吧
首先是androidstudio
1: 创建一个空的安卓工程
2: 此处的名字和包名不重要也不会用到
3:创建一个安卓库用于和Unity 交互
4: 在libs 文件夹添加classes,jar 文件的路径 D:\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes
右键点击jar
创建一个javaclass
注意此处的包名 是unity多出需要使用的
package com.las.mylibrary;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class LoadingActivity extends UnityPlayerActivity {
private static final int PHOTO_REQUEST_CODE = 1;// 相册
public static final int PHOTORESOULT = 3;// 结果
private Uri cropImageUri;
private String gameObjectName;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
}
/**
* Unity传值 Android Jar 接收并显示
*
* @param unityText
*/
public void setUnityText( String gameObjectName, String unityText)
{
UnityPlayer.UnitySendMessage(gameObjectName, "onImagePath", "Fail"+unityText);
}
public void OpenPhoto(String name) {
gameObjectName = name;
Intent intent = new Intent(Intent.ACTION_PICK, null);
// Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
// intent.setType("image/*");
// intent.putExtra("crop", "true");
// intent.putExtra("aspectX", 1);
// intent.putExtra("aspectY", 1);
// intent.putExtra("outputX", 100);
// intent.putExtra("outputY", 100);
// intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_REQUEST_CODE);
}
// 读取相册缩放图片
public void startPhotoZoom(Uri uri) {
if (null == uri) {
Log.i("Unity", "读取相册缩放图片 ==>> uri为Null");
}
File CropPhoto = new File(getExternalCacheDir(), "crop_image.jpg");
try {
if (CropPhoto.exists()) {
CropPhoto.delete();
}
CropPhoto.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
cropImageUri = Uri.fromFile(CropPhoto);
// cropImageUri = Uri.parse("file://" + "/" + Environment.getExternalStorageDirectory().getPath()
// + "/Android/data/com.YouthGamer.WenZhou/cache/" + "crop_image.jpg");
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 添加这一句表示对目标应用临时授权该Uri所代表的文件
}
// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop", "true");
intent.putExtra("scale", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropImageUri);
// intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
Log.i("Unity", "开始缩放");
try {
startActivityForResult(intent, PHOTORESOULT);
} catch (Exception e) {
// 解决截取后部分机型秒退
Log.i("Unity", e.getMessage());
e.printStackTrace();
UnityPlayer.UnitySendMessage(gameObjectName, "onImagePath", "Fail" );
}
}
public void SaveBitmap(Bitmap bitmap) throws IOException {
if(null == bitmap) {
Log.i("Unity", "SaveBitmap=>bitmap为Null");
}
FileOutputStream fOut = null;
// 注解1
String path = "/mnt/sdcard/Android/data/com.las.mylibrary/files";
//String path = "file://" + "/" + Environment.getExternalStorageDirectory().toString();
// + "/Android/data/com.YouthGamer.WenZhou/files";
try {
// 查看这个路径是否存在,
// 如果并没有这个路径,
// 创建这个路径
File destDir = new File(path);
if (!destDir.exists()) {
destDir.mkdirs();
}
fOut = new FileOutputStream(path + "/image.jpg");
} catch (FileNotFoundException e) {
Log.i("Unity", e.getMessage());
e.printStackTrace();
}
// 将Bitmap对象写入本地路径中,Unity在去相同的路径来读取这个文件
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
Log.i("Unity", e.getMessage());
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
Log.i("Unity", e.getMessage());
e.printStackTrace();
}
Log.i("Unity", gameObjectName);
UnityPlayer.UnitySendMessage(gameObjectName, "onImagePath", "image.jpg");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null)
return;
if (requestCode == PHOTO_REQUEST_CODE) {
Log.i("Unity", "选取相册图片完毕");
startPhotoZoom(data.getData());
} else if (requestCode == PHOTORESOULT) {
try {
Log.i("Unity", "缩放图片完毕,准备保存Bitmap");
Bitmap headShot = BitmapFactory.decodeStream(getContentResolver().openInputStream(cropImageUri));
SaveBitmap(headShot);
} catch (IOException e) {
Log.i("Unity", e.getMessage());
e.printStackTrace();
}
}
}
}
粘贴上面代码时需要注意3个地方
双击build.gradle 在文件中添加一下代码保存后 更新gradle
// 定义SDK包名称
def SDK_BASENAME = "AndroidPlugin"
// 定义SDK包版本
def SDK_VERSION = "_V1.0.0"
// SDK包生成地址
def SDK_PATH = "build/libs"
// 删除之前的Jar包 保证每一次生成的都是最新的
task deleteOldJar(type: Delete) {
delete SDK_PATH + SDK_BASENAME + SDK_VERSION + '.jar'
}
task exportJar(type: Copy) {
// 从源地址拷贝
from('build/intermediates/packaged-classes/release/')
// 存放
into(SDK_PATH)
// 导入
include('classes.jar')
// 重命名
rename('classes.jar', SDK_BASENAME + SDK_VERSION + '.jar')
}
// 执行脚本文件
exportJar.dependsOn(deleteOldJar, build)
找到 exportJar 双击即可打出Jar包
用压缩软件打开jar包,删除其他只剩你写的文件 如下所示 完成后 即可导入Unity 至此 AS方面大功告成
此脚本挂载在mainCamera 上
需要注意的是jar 包中定义了回调方法的名字 但是回调到拿个物体上是需要传进去的
个人建议 你要搞懂你写的买一句代码的大概意思
unity的其他基本操作就不写了,