x


How can I generate a GUID or a UUID from within Unity

System.GUID.newGUID() crashes the app, I'm assuming due to the stripping.

more ▼

asked Apr 29 '12 at 10:48 PM

kalvinlyle gravatar image

kalvinlyle
241 11 16 19

for future readers, Unity does all this work for you ...

http://answers.unity3d.com/questions/373055/deviceuniqueidentifier-curiosity-.html

Apr 15 at 06:42 AM Fattie
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

I ended up using:

  • System Language
  • Platform Type
  • Time Stamp
  • Running Time
  • Random Number

So for the same ID to be generated two of the same device with the same language setting would have to open the game at the exact same time and the devices would both have to request the ID at the exact same time, then both devices would have to generate the same random number.

    public static string GetUniqueID(){
       string key = "ID";

       var random = new System.Random();              
       DateTime epochStart = new System.DateTime(1970, 1, 1, 8, 0, 0, System.DateTimeKind.Utc);
       double timestamp = (System.DateTime.UtcNow - epochStart).TotalSeconds;

       string uniqueID = Application.systemLanguage                 //Language
          +"-"+GetPlatform()                           //Device   
          +"-"+String.Format("{0:X}", Convert.ToInt32(timestamp))          //Time
          +"-"+String.Format("{0:X}", Convert.ToInt32(Time.time*1000000))     //Time in game
          +"-"+String.Format("{0:X}", random.Next(1000000000));          //random number

       Debug.Log("Generated Unique ID: "+uniqueID);

       if(PlayerPrefs.HasKey(key)){
         uniqueID = PlayerPrefs.GetString(key);      
       } else {       
         PlayerPrefs.SetString(key, uniqueID);
         PlayerPrefs.Save();  
       }

       return uniqueID;
    }
more ▼

answered May 07 '12 at 12:43 AM

kalvinlyle gravatar image

kalvinlyle
241 11 16 19

(comments are locked)
10|3000 characters needed characters left

If System.GUID.newGUID() crashes the environment, I would be tempted to raise that as a software defect as that is what should, in principle, be used. However, if that were not available, consider using a low level timestamp (say ... seconds since Jan 1st 1970). You can get away with this is the ID doesn't have to be guaranteed to be "globally" or "universally" unique.

more ▼

answered Apr 29 '12 at 11:00 PM

kolban gravatar image

kolban
1.8k 2 7

How do you get a timestamp though from unity (without an external WWW call)

May 06 '12 at 02:57 AM ina
May 06 '12 at 03:17 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

According to the Mono Compatibility page the Guid class is fully supported on all platforms / Mono versions. Stripping shouldn't be an issue unless you use the micro mscorlib. Are you sure you spelled it correct?

It's:

System.Guid.NewGuid();

So in C# you would do something like:

System.Guid myGUID = System.Guid.NewGuid();

edit
Like poday mentioned, if you use the micro mscorlib the Guid class is not included and you have to use a custom way to build a pseudo Guid.

more ▼

answered May 06 '12 at 03:10 AM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

Jus FTR for anyone reading in the future, this works perfectly.

Jan 02 at 03:21 PM Fattie

It may work depending upon your stripping level. If you use Micro mscorlib there are several classes that are stripped. http://docs.unity3d.com/Documentation/Manual/iphone-playerSizeOptimization.html specifically mentions GUIDs.

Apr 15 at 06:39 AM poday
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1969
x9
x3
x2

asked: Apr 29 '12 at 10:48 PM

Seen: 2974 times

Last Updated: Apr 15 at 07:41 AM