Unity 移动设备软件绑定(通过设备SN码)
原理:
已有一台设备(手机),想让自己应用(apk)仅在这一台手机上.通过获取手机的设备码(SN码),然后在程序中收入对应SN码,进行验证.
关键代码:
一.程序运行获取本机SN码:
AndroidJavaObject jo = new AndroidJavaObject("android.os.Build");
string serial = jo.GetStatic<string>("SERIAL");
二.手动输入的SN和设备的SN码比较
[Header("设备SN码")]
public string[] snlist = { };
private void Start()
{
bool has = false;
for (int i = 0; i < snlist.Length; i++)
{
if (snlist[i].Equals(serial))
{
has = true;
break;
}
}
完整代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AndroidSerialNumber : MonoBehaviour
{
[Header("设备SN码")]
public string[] snlist = { };
private void Start()
{
#if UNITY_ANDROID
if (snlist.Length <= 0)
{
return;
}
AndroidJavaObject jo = new AndroidJavaObject("android.os.Build");
string serial = jo.GetStatic<string>("SERIAL");
Debug.Log("本机的SN码为:" + serial);
if (string.IsNullOrEmpty(serial))
{
Application.Quit();
return;
}
bool has = false;
for (int i = 0; i < snlist.Length; i++)
{
if (snlist[i].Equals(serial))
{
has = true;
break;
}
}
if (!has)
{
Application.Quit();
}
#endif
}
}
|