前提・実現したいこと
AndroidStudioで生体認証のプラグインを作成しUnityで繋ぎ込みを行っているときに発生しました。
ContextをFragmentActivityにキャストできるかなと思いやってみましたがエラーになってしまいました。
UnityでFragmentActivityを取得することはできますでしょうか?
biometricPrompt = new BiometricPrompt((FragmentActivity) context, executor, callback);
発生している問題・エラーメッセージ
AndroidJavaException: java.lang.ClassCastException: com.unity3d.player.UnityPlayerActivity cannot be cast to androidx.fragment.app.FragmentActivity java.lang.ClassCastException: com.unity3d.player.UnityPlayerActivity cannot be cast to androidx.fragment.app.FragmentActivity at com.example.biometricslibs.MainActivity.AuthCreate(MainActivity.java:43) at com.unity3d.player.UnityPlayer.nativeRender(Native Method) at com.unity3d.player.UnityPlayer.c(Unknown Source:0) at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:95) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:280) at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20) at UnityEngine.AndroidJNISafe.CheckException () [0x00091] in <157db3d40e194fc2b8370dbe68a88be2>:0 at UnityEngine.AndroidJNISafe.CallVoidMethod (System.IntPtr obj, System.IntPtr methodID, UnityEngine.jvalue[] args) [0x00011] in <157db3d40e194fc2b8370dbe68a88be2>:0
該当のソースコード
java
1package com.example.biometricslibs; 2 3import android.content.Context; 4import android.widget.Toast; 5import androidx.annotation.NonNull; 6import androidx.biometric.BiometricPrompt; 7import androidx.core.content.ContextCompat; 8import androidx.fragment.app.FragmentActivity; 9 10import java.util.concurrent.Executor; 11 12class MainActivity { 13 public static MainActivity instance() { 14 return new MainActivity(); 15 } 16 17 private BiometricPrompt biometricPrompt; 18 19 private BiometricPrompt.AuthenticationCallback callback = new BiometricPrompt.AuthenticationCallback() { 20 @Override 21 public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) { 22 super.onAuthenticationError(errorCode, errString); 23 if (errorCode == 13 && biometricPrompt != null) 24 biometricPrompt.cancelAuthentication(); 25 } 26 27 @Override 28 public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) { 29 super.onAuthenticationSucceeded(result); 30 } 31 32 @Override 33 public void onAuthenticationFailed() { 34 super.onAuthenticationFailed(); 35 } 36 }; 37 38 public void AuthCheck(Context context) { 39 Toast.makeText(context, "call", Toast.LENGTH_SHORT).show(); 40 Executor executor = ContextCompat.getMainExecutor(context); 41 42 biometricPrompt = new BiometricPrompt((FragmentActivity) context, executor, callback); 43 BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder() 44 .setTitle("title") 45 .setSubtitle("subTitle") 46 .setDescription("description") 47 .setNegativeButtonText("cancel") 48 .build(); 49 50 biometricPrompt.authenticate(promptInfo); 51 } 52}
C#
1 using(var nativeDialog = new AndroidJavaClass("com.example.biometricslibs.MainActivity")) 2 { 3 using(var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) 4 { 5 using(var currentUnityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) 6 { 7 using(var instance = nativeDialog.CallStatic<AndroidJavaObject>("instance")) 8 { 9 instance.Call( 10 "AuthCheck", 11 currentUnityActivity 12 ); 13 } 14 } 15 } 16 }
補足情報(FW/ツールのバージョンなど)
Unity2019.2.21f1
Android Studio Arctic Fox | 2020.3.1
あなたの回答
tips
プレビュー