Reputation: 1389
I tried creating a plugin but not working in unity.I do not have much idea about Android studio but watched this video to create an android plugin.I created a new android project without any activity.So I have written two classes UnityBinder.java and Gallery.java.They are given below
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 class given below
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[] fileColumn={MediaStore.Images.Media.DATA};
Cursor cursor=getContentResolver().query(uri,fileColumn,null,null,null);
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(fileColumn[0]);
String photoPath=cursor.getString(columnIndex);
//Send path to unity to get photo
//GamObject Name,method name,argument
UnityPlayer.UnitySendMessage("Gallery","OnPhotoPick",photoPath);
Gallery.this.finish();//close activity
}
}
}
Next I have build and copied the jar file into Unity.The c# code written is as follows
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);
}
}
The manifest file which I copied into the Unity folder.Assets>Plugins>Android. is as follows.
<?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>
The error I got from terminal is as follows
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]]
Video of plugin done.What I am doing wrong here. Making Of Android plugin
I dont know how the calls are being made or what should I write in the manifest for the activity to run.
Upvotes: 2
Views: 3358
Reputation: 613
If you open UnityPlayerActivity, you can see, that its package name is "com.unity3d.player" and class name is "UnityPlayer"
So you should change you unity player class name in C# script to "com.unity3d.player.UnityPlayer".
Another solution is add to your android plugin Unity classes and extend your activity from UnityPlayerActivity and you can call your methods directly from your plugin
Upvotes: 2