对UnityPlayerActivity来说,onActivityResult没有被调用。

0 人关注

我没有很多为Unity3d创建Java(.aar)插件的经验,但我正试图从这样一个插件中设置谷歌认证与Firebase。举个例子来说明我的问题,我首先打开一个unity android应用程序,然后我运行下面的c#代码,在我的显示器上得到一个弹出窗口,让我用google登录。然后我选择了正确的google账户,然后google意图/活动消失了,然后我没有收到 "onActivityResult "被调用的指示。没有错误发生,我无法对我选择的谷歌账户信息进行任何操作。

In the image below, I click submit -> it opens the google sign-in activity in the next picture -> then it returns back to the submit screen (closing the google sign-in activity).

I think my issue is in this line:

activity.startActivityForResult(signInIntent, RC_SIGN_IN);

在这种情况下,"活动 "是由下面的c#unity代码发送的UnityPlayerActivity。我认为这使得我的代码在C# unity代码中寻找一个 "onActivityResult "方法,而不是在java代码中寻找。如果有任何帮助,我将不胜感激。如果你需要任何其他信息或屏幕截图,请告诉我。如果我是个白痴,请叫我出来。

Here is my code for calling the Google Signin Plugin From C# & Unity3d:

        AndroidJNIHelper.debug = true; 
        using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
            activity_context = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
        using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.package.class")) {
            if (pluginClass != null) {
                GoogleSignInActivity = pluginClass.CallStatic<AndroidJavaObject>("instance");
                GoogleSignInActivity.Call("SetContext", activity_context);
                GoogleSignInActivity.Call("StartGoogleLogin", activity_context);
                activity_context.Call("runOnUiThread", new AndroidJavaRunnable(() => {
                    GoogleSignInActivity.Call("ShowMessage", "You signed in as " + display_name);

下面是创建Google SignIn活动的代码。

public void StartGoogleLogin(UnityPlayerActivity activity) {
    gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken("some url")
            .requestEmail()
            .build();
    mGoogleSignInClient = GoogleSignIn.getClient(activity, gso);
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    activity.startActivityForResult(signInIntent, RC_SIGN_IN);
    Log.d(TAG, "Activity Started; Waiting For Result");
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(TAG, "Result Received!");
    if (requestCode == RC_SIGN_IN) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            // Google Sign In was successful
            GoogleSignInAccount account = task.getResult(ApiException.class);
            someMethod(account);
        } catch (ApiException e) {
            // Google Sign In failed, update UI appropriately
            Log.d(TAG, "Google sign in failed", e);
        setResult(RESULT_OK);

谢谢你的时间。

java
c#
android
unity3d
aar
airvine
airvine
发布于 2019-02-05
2 个回答
airvine
airvine
发布于 2021-04-02
已采纳
0 人赞同

好吧,我解决了自己的问题,让谷歌认证在安卓.ar插件和Unity c#之间工作。一直在不知疲倦地工作,并找到了一些踢爆的资源。

首先,也是最重要的,我参考了一个叫cwgtech的人写的代码 HERE .

我还翻阅了他所有的 videos .

我没有使用UnitySendMessage,而是使用了一个类似于CWGTech的回调方法,将googleIdToken送回Unity,用Google登录到Firebase。我也正确地认为我的错误是在下面的语句中。

activity.startActivityForResult(signInIntent, RC_SIGN_IN);

我没有这样做,而是按照CWGTech的建议,删除了 "activity. "部分。我在一个扩展了Activity的ResultCallback类中运行了startActivityForResult。如果你仍然感到困惑,可以给我打电话或在这个帖子上发表评论。谢谢!

下面是我用C#写的一些代码,通过一个Java代理向Unity发送回调字符串。关于编写Java代理的信息可以在上面的cwgtech信息中找到。如果你想让信息从Android活动流向Unity C#,编写java代理是非常重要的。CWGTech对java代理错综复杂的解释比我能做的更好。

public static final String LOGTAG = GoogleSignInActivity.TAG + "_OnResult";
public static GoogleSignInActivity.ShareStringCallback shareStringCallback;
private static final int RC_SIGN_IN = 9001;
private GoogleSignInClient mGoogleSignInClient;
private GoogleSignInOptions gso;
private CallbackManager mCallbackManager;
public void myFinish(String myresult) {
    if (shareStringCallback != null) {
        shareStringCallback.onLoginComplete(myresult);
    shareStringCallback = null;
    finish();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(LOGTAG, "onCreateBundle");
    Intent intent = getIntent();
    setContentView(R.layout.login_activity);
    findViewById(R.id.buttonFacebookLogin).setOnClickListener(this);
    findViewById(R.id.signInButton).setOnClickListener(this);
    findViewById(R.id.buttonAnonymousSignIn).setOnClickListener(this);
/* GOOGLE SIGN-IN CODE */
public Intent StartGoogleLogin() {
    Google Sign In Client Init Code Goes Here
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    return signInIntent;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //Detects some type of result from an activity, in this case Google
    String id_result = "Send This To Unity";
    myFinish(id_result);

下面是一个不同的java类文件中的一些代码。登录 "方法是由Unity c#调用的。

/* INTERFACES FOR CALLBACK FUNCTIONAILITY */
public interface ShareStringCallback {
    public void onLoginComplete(String result);
public void Login(final ShareStringCallback callback)
    mainActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            try {
                Log.i(TAG,"Starting Authentication");
                try {
                    try {
                        Intent shareIntent = new Intent();
                        shareIntent.setAction(Intent.ACTION_SEND);
                        shareIntent.setClass(mainActivity,OnResultCallback.class);
                        OnResultCallback.shareStringCallback = callback;
                        mainActivity.startActivity(shareIntent);
                    catch (Exception e)
                        e.printStackTrace();
                        Log.i(TAG,"error sharing intent: " + e);
                catch (Exception e)
                    e.printStackTrace();
                    Log.i(TAG,"Error getting Uri: " + e);
            catch (Exception e)
                e.printStackTrace();
                Log.i(TAG,"Error writing file: " + e);
    
嘿...我一直想在unity上使用一个Android语音插件,但我的onActivityResult回调与你的类似....。你能不能给我一个详细的解释,让我能够遵循...请联系我
@alamshahbaz16497 我在上面又贴了一点代码,希望能有所帮助。这些代码是用来从Android studio的java代码回调到c# Unity端。我建议你看一下我上面发布的cwgtech的视频。他们对我帮助很大。让我知道这是否有帮助以及你遇到的是什么类型的问题?
Nradar
Nradar
发布于 2021-04-02
0 人赞同

当你从UnityPlayerActivity启动一个活动时,onActivityResult会在完成后被调用,但会有其默认的返回值。你可以做的是在Android中创建一个新的活动,并扩展UnityPlayerActivity。

  • In Android, create a new Activity
  • import com.unity3d.player.UnityPlayer;
    public class OverrideUnityPlayerActivity extends UnityPlayerActivity{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        public Activity getCurrentActivity(){
            return mUnityPlayer.currentActivity;
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 0) {
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        //do something
                        break;
                    case Activity.RESULT_CANCELED:
                        //do something
                        break;
    
  • You need to set this override activity as launch endpoint in AndroidManifest.XML
  • <?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">
        <uses-permission android:name="android.permission.INTERNET" />
        <application
            android:allowBackup="false"
            tools:replace="android:allowBackup">
            <activity 
                android:name="com.example.unitylibrary.manager.OverrideUnityPlayerActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <meta-data android:name="app_name" android:value="app_value"/>
            </activity>
        </application>
    </manifest>
    

    3.然后在Unity中,你可以得到这个活动和背景,也可以得到onActivityResult。

    AndroidJavaObject overridePlayerActivity;
    AndroidJavaObject overrideActivity;
    public void init(){
        overridePlayerActivity = new AndroidJavaObject("com.example.unitylibrary.manager.OverrideUnityPlayerActivity");
        overrideActivity= overridePlayerActivity.Call<AndroidJavaObject>("getCurrentActivity");