LoginSignup
2
0

More than 1 year has passed since last update.

【Unity】iOS,Androidでuuidを取得する方法

Last updated at Posted at 2021-07-29

Android

SystemInfo.deviceUniqueIdentifier

iOS

SystemInfo.deviceUniqueIdentifier
ではOSのアップデート時に値が変わるそうなので一工夫が必要です。

前提条件

iOSのキーチェーンを取り扱うアセットを使います。

手順

1.キーチェーンからuuidを取得する。
2.uuidがなければ「Guid.NewGuid()」でuuidを発行する。
3.発行したuuidをキーチェーンに保存する。

これでアプリを端末から削除しても変わらないuuidが使えます。

サンプルソース

using UnityEngine;
using FSG.iOSKeychain;
using System;

public class TestManager : MonoBehaviour
{
    public static readonly string KEY_ID = "CustomID";

    void Start()
    {
        string id = string.Empty;

#if UNITY_IOS
        id = Keychain.GetValue(KEY_ID);

        if (string.IsNullOrWhiteSpace(id))
        {
            id = Guid.NewGuid().ToString();

            Keychain.SetValue(KEY_ID, id);
        }

#elif UNITY_ANDROID
        id = SystemInfo.deviceUniqueIdentifier;
#endif

        Debug.Log($"id : {id}");
    }
}
2
0
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
2
0