Search Unity

UniqueID

Discussion in 'Community Learning & Teaching' started by IsGreen, Jul 31, 2015.

Thread Status:
Not open for further replies.
  1. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    We need an identification number (ID) to connect a GameObject (entity) to a database or differentiate GameObjects with the same name.



    But the ID provided by Unity though is unique for each GameObject changes every session. That is, it can not be used to create a database of the game, or differentiate GameObjects with the same name in different sessions.



    I created a class called UniqueID that generates and stores a unique and constant ID, saving the last ID generated by PlayerPrefs a file or a binary file.

    PlayerPrefs using to store the last ID generated:
    :

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [System.Serializable]
    4. public class UniqueID{
    5.  
    6.     public int ID;
    7.  
    8.     static string key = "UniqueID";
    9.  
    10.     public UniqueID(){
    11.  
    12.         if(PlayerPrefs.HasKey("UniqueID")){
    13.  
    14.             this.ID = PlayerPrefs.GetInt(key);
    15.             PlayerPrefs.SetInt(key,this.ID+1);
    16.             return;
    17.  
    18.         }
    19.  
    20.         PlayerPrefs.SetInt(key,1);
    21.         this.ID = 0;
    22.  
    23.     }
    24.  
    25.     public static implicit operator int(UniqueID uniqueID){ return uniqueID.ID; }
    26.     public static implicit operator string(UniqueID uniqueID) { return uniqueID.ID.ToString(); }
    27.  
    28. }
    29.  

    Using BinaryWriter, It is easier to restart identifiers to 0. You just need to remove the UniqueID file.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.IO;
    3.  
    4. [System.Serializable]
    5. public class UniqueID{
    6.  
    7.     public int ID;
    8.  
    9.     static string path = "C:/UniqueID";
    10.  
    11.     public UniqueID(){
    12.  
    13.         if(File.Exists(path)){
    14.  
    15.             BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open));
    16.             this.ID = reader.Read();
    17.             reader.Close();
    18.  
    19.             BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Open));
    20.             writer.Write(this.ID+1);
    21.             writer.Close();
    22.  
    23.             return;
    24.  
    25.         }
    26.  
    27.         BinaryWriter _writer = new BinaryWriter(File.Open(path, FileMode.Create));
    28.         _writer.Write(1);
    29.         _writer.Close();
    30.         this.ID = 0;
    31.  
    32.     }
    33.  
    34.     public static implicit operator int(UniqueID uniqueID){ return uniqueID.ID; }
    35.     public static implicit operator string(UniqueID uniqueID) { return uniqueID.ID.ToString(); }
    36.  
    37. }

    Also, I created a CustomDrawer for UniqueID class. Only read ID property:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4.  
    5. [CustomPropertyDrawer (typeof (UniqueID))]
    6. public class UniqueIDDrawer : PropertyDrawer {
    7.  
    8.     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
    9.  
    10.         EditorGUI.BeginProperty (position, label, property);
    11.         position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID (FocusType.Passive), label);
    12.         int indent = EditorGUI.indentLevel;
    13.         EditorGUI.indentLevel = 0;
    14.         string value = property.FindPropertyRelative ("ID").intValue.ToString();
    15.         Rect IDRect = new Rect (position.x, position.y, 25f + value.Length * 6f, position.height+2f);
    16.         EditorGUI.LabelField(IDRect,value,GUI.skin.box);
    17.         EditorGUI.indentLevel = indent;
    18.         EditorGUI.EndProperty ();
    19.  
    20.     }
    21.  
    22. }
    How to use:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class script : MonoBehaviour {
    4.  
    5.     public UniqueID ID;
    6.  
    7.     void Start(){
    8.  
    9.         Debug.Log(this.gameObject.name + " UniqueID:  " + this.ID);
    10.  
    11.     }
    12.  
    13. }
     
    Last edited: Jul 31, 2015
    theANMATOR2b and Stardog like this.
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please post in the appropriate section.

    The teaching section is for discussion and sharing of learning materials and tutorials, not technical support (beyond supporting the lessons posted here).

    As such, I am closing this thread.

    Please repost this elsewhere.
     
Thread Status:
Not open for further replies.