没有为UnityPlayerActivity调用onActivityResult

时间:2019-02-04 19:25:29

标签: java c# android unity3d aar

我没有为Unity3d创建Java(.aar)插件的丰富经验,但是我正在尝试通过Firebase从此类插件设置google身份验证。举一个我的问题的例子,我首先打开一个统一的android应用程序,然后运行下面的c#代码,并在显示器上弹出一个窗口以使用google登录。然后,我选择了正确的Google帐户,然后Google意图/活动消失了,然后我没有收到表示已调用“ onActivityResult”的指示。没有错误发生,我无法使用我选择的Google帐户信息做任何事情。

在下面的图片中,我单击提交->它在下一张图片中打开google登录活动->然后返回到提交屏幕(关闭google登录活动)。 enter image description here enter image description here

我认为我的问题在这一行:

activity.startActivityForResult(signInIntent, RC_SIGN_IN);

在这种情况下,“活动”是从下面的c#统一代码发送的UnityPlayerActivity。我认为这已经做到了,所以我的代码正在C#统一代码而不是Java代码中寻找“ onActivityResult”方法。任何帮助将不胜感激。让我知道您是否需要其他信息或屏幕截图。如果我是个白痴,请叫我出去。

这是我的从C#和Unity3d调用Google登录插件的代码:

        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登录活动的代码:

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);
    }
}

谢谢您的时间。

2 个答案:

答案 0 :(得分:0)

好吧,我解决了我自己的使Google身份验证在Android .aar插件和Unity c#之间工作的问题。孜孜不倦地工作,发现了一些宝贵的资源。

首先,我引用了一个叫cwgtech HERE的人编写的代码。

我也经历了他所有的videos

我可以使用类似于CWGTech的回调方法,而不是使用UnitySendMessage来将googleIdToken发送回Unity并使用Google登录到Firebase。我还以为我的错误在于以下陈述是正确的:

activity.startActivityForResult(signInIntent, RC_SIGN_IN);

我没有这样做,而是遵循了CWGTech的建议,并删除了“活动”。一部分。我在扩展Activity的ResultCallback类中运行了startActivityForResult。如果您仍然感到困惑,请给我dm或发表评论。谢谢!

这是我用来通过C#编写的Java代理将回调字符串发送给Unity的一些代码。可以在上面的cwgtech信息中找到有关编写Java代理的信息。如果要使信息从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);
            }
        }
    });
}

答案 1 :(得分:0)

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

  1. 在 Android 中,创建一个新的 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;
            }
        }
    }
}
  1. 您需要将此覆盖活动设置为 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中,可以得到这个activity和context,也可以得到onActivityResult

AndroidJavaObject overridePlayerActivity;
AndroidJavaObject overrideActivity;

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

public void startAct(){
    anonymousFunction.Call("StartActForRes", overrideActivity);
}

希望这能解决您的问题。