[펌] Unity 에서 장치 ID, MAC 주소 가져오기
Unity3D/Tips / 2016. 5. 2. 10:49
Unity에서 장치의 고유한 식별 번호를 얻고 싶은데...
아이폰으로는 테스트를 못해 봤습니다만... 잘 되지 않을까 싶네요.
안드로이드에서 장치의 식별자로 쓸만한 것이 3개 있는데
- Settings.Secure 의 ANDROID_ID
- WiFi의 MAC 주소
- 전화 모듈의 식별자
입니다.
아래 코드에서는 위 순서대로 가져오기를 시도해서 잘 가져오는 것을 반환하게 했습니다.
당연히... 별다른 이상한 오류 상황이 아니면 ANDROID_ID를 가져 오겠지요?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | public string GetDeviceID (){ if (Application.platform == RuntimePlatform.Android) { try { using (AndroidJavaObject activity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject> ("currentActivity")) { // ANDROID_ID try { using (AndroidJavaObject resolver = activity.Call<AndroidJavaObject> ("getContentResolver")) { using (AndroidJavaObject settingsSecure = new AndroidJavaObject ("android.provider.Settings.Secure")) { string deviceID = settingsSecure.CallStatic<string> ("getString", resolver, settingsSecure.GetStatic<string> ("ANDROID_ID")); if (!string.IsNullOrEmpty (deviceID)) { return deviceID; } } } } catch (System.Exception) { } // WiFi MAC try { using (AndroidJavaObject wifiManager = activity.Call<AndroidJavaObject> ("getSystemService", activity.GetStatic<string>("WIFI_SERVICE"))) { string macAddr = wifiManager.Call<AndroidJavaObject> ("getConnectionInfo").Call<string> ("getMacAddress"); if (!string.IsNullOrEmpty (macAddr)) { return macAddr; } } } catch (System.Exception) { } // IMEI/MEID code try { using (AndroidJavaObject telephonyManager = activity.Call<AndroidJavaObject> ("getSystemService", activity.GetStatic<string>("TELEPHONY_SERVICE"))) { string imeiCode = telephonyManager.Call<string> ("getDeviceId"); if (!string.IsNullOrEmpty (imeiCode)) { return imeiCode; } } } catch (System.Exception) { } } } catch (System.Exception) { } } else { // 이 방법은 안드로이드에서는 안된다. - Unity 4.3.4f1 try { var nics = NetworkInterface.GetAllNetworkInterfaces (); if (nics.Length > 0) { return nics[0].GetPhysicalAddress ().ToString (); } } catch (System.Exception) { } } return "";} |
WiFi MAC 주소를 가져오려면 ACCESS_WIFI_STATE 권한이 필요합니다.
IMEI code 를 가져오려면 READ_PHONE_STATE 권한이 필요합니다.
그럼 이 권한은 어떻게 설정할까요?
만약 프로젝트 경로 아래에
Assets/Plugins/Android/AndroidManifest.xml 파일이 있다면 해당 파일을 수정하시면 됩니다.
이 파일이 없으시다면
(Unity 설치 경로)/Editor/Data/PlaybackEngines 폴더 밑에 있는
androidplayer 또는 androiddevelopmentplayer 폴더에 있는 파일을 복사해서 사용하시면 됩니다.
아래 내용을 추가하시면 되겠죠?
1 2 3 4 5 6 7 8 | <uses-permission android:name="android.permission.READ_PHONE_STATE" ></uses-permission><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission> |
반응형
'Unity3D > Tips' 카테고리의 다른 글
| [펌] Application.LoadLevel(string) is obsolete 마이그레이션 (0) | 2016.05.16 |
|---|---|
| [펌] Unity로 다수의 인원이 개발할 때 알아두면 좋은 9가지 (0) | 2016.05.11 |
| [펌] 유니티 작업에 대한 50 팁 (모범 사례) 50 Tips for Working with Unity (Best Practices) (0) | 2016.03.22 |
| [링크] 유니티 관련 괜찮은 자료 (0) | 2016.03.20 |
| [펌] Find references in scene (0) | 2016.02.26 |
