我们自己建一个类继承它 :大家记得回去修改跳转的类因为我们在调用选择图片时因该先判断是否获取了权限
package com.jing.pickpicture.permission; import android.Manifest; import android.content.Intent; import android.os.Build; import android.os.Bundle; import androidx.annotation.RequiresApi; import com.jing.pickpicture.WebViewActivity; public class TestActivity extends BasePermission implements BasePermission.PermissionCallback{ @RequiresApi(api = Build.VERSION_CODES.M) protected void onCreate(Bundle savedInstanceState) {//Activity方法 只有Activity(界面)被创建时会被调用比如被跳转时 super.onCreate(savedInstanceState); performCodeWithPermission("确认权限",this,new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}); } @Override public void hasPermission() {//BasePermission中接口PermissionCallback中的方法 System.out.println("已获取权限"); Intent intent = new Intent(this, WebViewActivity.class); //getIntent().getStringExtra("path") 通过键获取上文的值 这里是CallAndroid那里存入的值 intent.putExtra("path",getIntent().getStringExtra("path"));//继续把unity那边路径传入下个跳转的Activity startActivity(intent); finish();//销毁当前Activity } @Override public void noPermission() {//BasePermission中接口PermissionCallback中的方法 System.out.println("取消权限,返回游戏"); this.finish();//销毁当前Activity } @Override public void onResume() {//Activity方法 只有Activity(界面)被显示时调用 super.onResume(); System.out.println("返回到TestActivity"); } } `` 改一下 WebViewActivity ```java package com.jing.pickpicture; import android.app.Activity; import android.content.ContentResolver; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.util.Log; import com.unity3d.player.UnityPlayer; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class WebViewActivity extends Activity { public static final int PHOTORESOULT = 3; public static final String IMAGE_UNSPECIFIED = "image/*"; private String FILE_NAME = "Photo.png"; private String phtoPath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); phtoPath = getIntent().getStringExtra("path");//获取路径 Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTORESOULT);//以该函数跳转Activity会以onActivityResult来接受跳转Activity的结果 } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {//requestCode是我们在上方startActivityForResult的参数2(这里没起作用) resultCode是上一个Activity也就是选择图片的那个Activty(页面)的结果 if (resultCode == RESULT_CANCELED)//如果接收结果为返回 { finish(); return;} if (data == null)//上下文为空 { finish(); return;} ContentResolver resolver = getContentResolver(); Bitmap photo=null; Uri originalUri = data.getData(); try { photo = MediaStore.Images.Media.getBitmap(resolver, originalUri); } catch (IOException e) { e.printStackTrace(); } System.out.println("=="+photo); String fullPath = phtoPath+ "/" + FILE_NAME; FileOutputStream out = null; try { out = new FileOutputStream(new File(fullPath)); } catch (FileNotFoundException e) { e.printStackTrace(); } photo.compress(Bitmap.CompressFormat.PNG, 90, out);//压缩文件到指定路径 try { out.flush(); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("保存图片"); FileOutputStream fOut = null; System.out.println("返回游戏"); try { Intent intent = new Intent(this,Class.forName("com.unity3d.player.UnityPlayerActivity")); startActivity(intent);//返回Unity //这里也可以直接finish UnityPlayer.UnitySendMessage("Main Camera","CallBack",fullPath); return; } catch (ClassNotFoundException e) { e.printStackTrace(); } super.onActivityResult(requestCode, resultCode, data); this.finish(); } }修改Build可以不用把Unity的jar包打入 最后把用到的Activity以及权限添加到安卓清单文件
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application > <activity android:name=".permission.TestActivity" /> <activity android:name=".WebViewActivity" /> </application>Unity代码
using System.IO; using UnityEngine; using UnityEngine.UI; public class GetPhoto : MonoBehaviour { public Button Bt; public Image image; private void Start() { Bt.onClick.AddListener(() => { using (AndroidJavaObject javaobj = new AndroidJavaObject("com.jing.pickpicture.CallAndroid")) { javaobj.Call("TakePhoto",Application.persistentDataPath); } }); } private void CallBack(string fullPath) { byte[] datas = File.ReadAllBytes(fullPath); Texture2D texture = new Texture2D((int)image.rectTransform.rect.width, (int)image.rectTransform.rect.height); texture.LoadImage(datas); Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); image.sprite = sprite; } }至于为啥是 AndroidJavaClass jc = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”); var jo = jc.GetStatic(“currentActivity”); 我们创建个新工程导出为安卓工程 然后贴上官方的解释