简体   繁体   English

Android 插件无法与未找到类异常一起工作?

[英]Android Plugin not working in unity with class not found exception?

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我尝试创建一个插件,但没有在统一中工作。我对 Android Studio 不太了解,但观看此视频以创建一个 android 插件。我创建了一个没有任何活动的新 android 项目。所以我编写了两个类 UnityBinder.java 和Gallery.java.它们在下面给出

UnityBinder.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 class given below下面给出的 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

    }
}
}

Next I have build and copied the jar file into Unity.The c# code written is as follows接下来我已经将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);
}


 }

The manifest file which I copied into the Unity folder.Assets>Plugins>Android.我复制到 Unity 文件夹中的清单文件。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 Android插件制作

I dont know how the calls are being made or what should I write in the manifest for the activity to run.我不知道调用是如何进行的,或者我应该在清单中写什么才能运行活动。 Android Studio 和 Unity

If you open UnityPlayerActivity, you can see, that its package name is "com.unity3d.player" and class name is "UnityPlayer"如果你打开 UnityPlayerActivity,你可以看到,它的包名是“com.unity3d.player”,类名是“UnityPlayer”

图片

So you should change you unity player class name in C# script to "com.unity3d.player.UnityPlayer".因此,您应该将 C# 脚本中的统一播放器类名称更改为“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另一个解决方案是添加到您的 android 插件 Unity 类并从 UnityPlayerActivity 扩展您的活动,您可以直接从您的插件调用您的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM