LoginSignup
10
7

More than 5 years have passed since last update.

Unityで使えるAndroidの指紋認証プラグイン作ってみた

Posted at

ゲームに指紋認証を組み込んだものって見ないなぁと思ったので、そのベースとなる指紋認証を組み込めるプラグインをAndroidのみですが作成しました。

本当は、iOSプラグインを作って両プラットフォーム対応にしたかったのですが、何せiOS端末を持っていないということで実機検証不可…oh…

このプラグインの制作にあたって、様々な記事を参考にさせていただいたのですが…主にここのブログと、もうひとつ参考になったブログがあったのですが…見失いました…
http://redfreshet.com/2015/08/18/unity_android_link_1/

まずは、プラグインを使用してるUnityのソースから

FingerManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace FingerAuth {

    public class FingerManager : MonoBehaviour {

        private const string JAVA_CLASS_NAME = "com.example.fingerauthunity.lib.FingerAuth";
        //Androidプラグインからコールバックを受け取るため
        private const string OBJECT_NAME = "FingerManager"; 

        private FingerAuthState _state;

        public FingerAuthState State { get { return _state; } }

        // Use this for initialization
        void Start () {
            _state = FingerAuthState.INIT;
            gameObject.name = OBJECT_NAME;
            FingerAuthStart();
        }

        // Update is called once per frame
        void Update () {

        }
        public void FingerAuthStart() {
            using (AndroidJavaClass plugin = new AndroidJavaClass(JAVA_CLASS_NAME)) {
                plugin.CallStatic("AuthStart");
            }
        }

        //CallBackで処理結果が返ってくる
        public void FingerAuthResult(string message) {
            _state = (FingerAuthState)int.Parse(message);
        }
    }


    public enum FingerAuthState {
        INIT = -1,
        WAIT = 0,
        SUCCESS = 1,
        FAILD = 2
    }

}

… #if を追加するの忘れていたので、必要であらば追加してください。

そして、Androidでの指紋認証を行うソースはこちら

FingerAuth.java
package com.example.fingerauthunity.lib;

import android.annotation.TargetApi;
import android.app.Activity;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.CancellationSignal;
import android.os.Handler;
import android.util.Log;

import com.unity3d.player.UnityPlayer;

import static android.content.Context.FINGERPRINT_SERVICE;

public class FingerAuth {

    private static CancellationSignal cancel;

    @TargetApi(Build.VERSION_CODES.M)
    public static void AuthStart() {
        UnityPlayer.UnitySendMessage("FingerManager", "FingerAuthResult", "0");
        final Activity activity = UnityPlayer.currentActivity;
        FingerprintManager fingerprintManager = (FingerprintManager) activity.getSystemService(FINGERPRINT_SERVICE);
        cancel = new CancellationSignal();
        fingerprintManager.authenticate(null, cancel, 0, new FingerprintManager.AuthenticationCallback() {
            @Override
            public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
                UnityPlayer.UnitySendMessage("FingerManager", "FingerAuthResult", "1");
                cancel.cancel();
                Log.i("", "auth success");
            }

            @Override
            public void onAuthenticationFailed() {
                UnityPlayer.UnitySendMessage("FingerManager", "FingerAuthResult", "2");
                cancel.cancel();
                Log.e("", "failed");
            }
            /*
            @Override
            public void onAuthenticationError(int errorCode, CharSequence errString) {
                UnityPlayer.UnitySendMessage("FingerManager", "FingerAuthResult", "3");
                cancel.cancel();
                Log.e("", "error " + errorCode + " " + errString);
            }
            */
        }, new Handler());
    }
}

これで、端末に登録してある指紋との一致不一致をUnityにコールバックしてUnity側でよしなにする…という流れですね

指紋認証でモンスター召喚とかしたい!と思って作成したものの、指紋が人差し指か中指かわかるのかと思いきや、登録してるしてないでしか判断できないとは……
さらに言うと登録のAPIがないらしいので、アプリ側からは登録できないのかぁ…と残念な感じに…

せめて、指紋がどの指とかわかればなぁ…

リポジトリは、サクッとテキトーに作ったものなのでご容赦を

Plugin用AndroidProject
GitHub Android Project

UnitySampleProject
GitHub Unity Project

10
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
7