我尝试创建一个插件,但没有统一工作。我对 Android Studio 不太了解,但观看了这个视频来创建一个 android 插件。我创建了一个没有任何活动的新 android 项目。所以我编写了两个类 UnityBinder.java 和Gallery.java.它们在下面给出
UnityBinder.java
package com.techripples.unityplugin;
import android.app.Activity;
import android.content.Intent;
public class UnityBinder {
public static void OpenGallery(Activity activity)
{
Intent intent =new Intent(activity,Gallery.class);
activity.startActivity(intent);
}
}
Gallery.java 类如下
package com.techripples.unityplugin;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.net.Uri;
import android.database.*;
import com.unity3d.player.*;
public class Gallery extends Activity {
int PHOTO_GALLERY=1;
@Override
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle);
Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,PHOTO_GALLERY);
}
//it will run when user picks a photo from the gallery
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode==PHOTO_GALLERY && data!=null)
{
Uri uri=data.getData();
String[] fileColoumn={MediaStore.Images.Media.DATA};
Cursor cursor=getContentResolver().query(uri,fileColoumn,null,null,null);
cursor.moveToFirst();
int coloumnIndex=cursor.getColumnIndex(fileColoumn[0]);
String photoPath=cursor.getString(coloumnIndex);
//Send path to unity to get photo
//GamObject Name,method name,argument
UnityPlayer.UnitySendMessage("Gallery","OnPhotoPick",photoPath);
Gallery.this.finish();//close activity
}
}
}
接下来我将jar文件构建并复制到Unity中。编写的c#代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShowPicture : MonoBehaviour
{
Texture2D galleryImage;
bool isGalleryImageLoaded = false;
WWW www;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
if(isGalleryImageLoaded)
{
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), galleryImage);
}
if(GUI.Button(new Rect(50,50,100,100),"Open"))
{
isGalleryImageLoaded = false;
AndroidJavaClass ajc = new AndroidJavaClass("com.unity.player.UnityPlayer");
//While asking question used AndroidJavaClass instead of AndroidJavaObject
AndroidJavaObject ajo = new AndroidJavaObject("com.techripples.unityplugin.UnityBinder");
ajo.CallStatic("OpenGallery", ajc.GetStatic<AndroidJavaObject>("currentActivity"));
}
if(www!=null && www.isDone)
{
galleryImage = new Texture2D(www.texture.width, www.texture.height);
galleryImage.SetPixels32(www.texture.GetPixels32());
galleryImage.Apply();
www = null;
isGalleryImageLoaded = true;
}
}
public void OnPhotoPick(string filePath)
{
Debug.Log(filePath);
www = new WWW("file://" + filePath);
}
}
我复制到 Unity 文件夹中的清单文件。Assets>Plugins>Android。如下。
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="preferExternal">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<application
android:theme="@style/UnityThemeSelector"
android:icon="@mipmap/app_icon"
android:label="@string/app_name">
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:label="@string/app_name">
<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>
<activity android:name="com.techripples.unityplugin.Gallery"></activity>
</application>
我从终端得到的错误如下
01-28 13:25:06.305 27032 27061 E Unity : java.lang.ClassNotFoundException: Didn't find class "com/unity/player/UnityPlayer" on path: DexPathList[[zip file "/data/app/com.testing.androidplugin-Aq1j3vpTxsomWMvd4kK7NQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.testing.androidplugin-Aq1j3vpTxsomWMvd4kK7NQ==/lib/arm, /data/app/com.testing.androidplugin-Aq1j3vpTxsomWMvd4kK7NQ==/base.apk!/lib/armeabi-v7a, /system/lib, /system/vendor/lib]]
插件视频完成。我在这里做错了什么。 Android插件制作