Unity Android插件onActivityResult未调用

时间:2018-03-26 11:00:42

标签: java c# android unity3d plugins

我正在Android Studio中为Android编写一个插件,并在Unity 5.6中运行它。我创建了一个用于选择图像的类 - 但是,当用户选择图像时,“Gallery”应用程序会无意中打开。这是我的代码:

fotoSec.java

package com.alptugrul.background;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.Toast;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

/**
 * Created by Alpaslan on 25.3.2018.
 */

public class fotoSec extends UnityPlayerActivity {
   static Uri imgUri;

    public static void fotoSece() {
        Activity activity = UnityPlayer.currentActivity;
        Intent gallery = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        activity.startActivityForResult(gallery, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(UnityPlayer.currentActivity.getApplicationContext(), "bnhg", Toast.LENGTH_SHORT).show();

    }
    public static String getURL () {
        String url = null;
        if (imgUri != null){
            url = imgUri.toString();
        }
        return url;
    }
}
Unity中的

imgSec.cs,

using UnityEngine;

public class imgSec : MonoBehaviour {

    private static string fullClassName = "com.alptugrul.background.fotoSec";
    AndroidJavaClass fotoSecClass;

    private void Start() {
        fotoSecClass = new AndroidJavaClass(fullClassName);
    }

    public void fotoSec() {
        fotoSecClass.CallStatic("fotoSece");
    }
}

的Manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.alptugrul.background">
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:label="@string/app_name">
        <activity android:name="com.unity3d.player.UnityPlayerActivity" android:launchMode="singleTask"
            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=".fotoSec">
            <intent-filter>
                <action android:name="android.intent.action.PICK"/>
                <action android:name="android.intent.action.GET_CONTENT"/>
            </intent-filter>
        </activity>
        <receiver android:name=".Baslangic"
            android:enabled ="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

运行应用时,我点击按钮,图库就会打开。然后我选择了一个图像但是在选择之后,我没有在onActivityResult函数中添加 Toast 消息。

由于

0 个答案:

没有答案