- Home /
Uniquely identify objects ?
Hi, I'd like to identify objects or components in a unique way, but it seems that GetHashCode() or GetInstanceId() are different each time I build my projet. I know I could setup the name to identify uniquely my objects, but this may lead to other issues. Is there any other easy solution apart from creating custom code ?
As you discovered, GetInstanceID is transient -- every object has a unique ID, but only for the duration of the Unity (game or editor) session. In the editor environment, depending on use case, AssetDatabase.AssetPathToGUID might be helpful -- this will give you a unique ID per asset file in your project. Note that you only get one ID per asset (e.g. not per component on a prefab), you can't access these at runtime, only in editor code (though you could store them away for use later), and if you delete and regenerate .meta files you will get new GUID's, which will invalidate any ID's you've saved elsewhere.
Answer by whydoidoit · Jun 11, 2012 at 12:12 PM
Well there are two ways of looking at this:
Ensure that the name of an object is unique
Create and store a unique identifier for the object
You can create a unique Id using something like this:
public string Id = Guid.NewGuid().ToString();
GetHashCode() changes as your properties and fields change (probably, depends on the implementation). GetInstanceId() is doing something with the memory address the object is currently at. There is some script here that will make objects have unique names.
thanks $$anonymous$$ike, changing the name manually is cumbersome, and automatically unpreferred since for most objects I prefer to keep the exact name I setup. For the other solution, alas I'm not sure where I would store the id since it would entail adding a script dedicated to that, or serializing the pair my own way. If there is no other solution, I'll probably write a custom hashcode method, actually in my case I realized that it may be something as simple as storing the name+position :p thanks for the pointer anyway, I might need that in the future!
Yeah I see your point. Be careful on writing a GetHashCode - it has to interact nicely with an Equals override so I understand so that sorts/dictionaries etc continue to work.
Answer by IsGreen · Jul 31, 2015 at 08:33 AM
You can store index ID using PlayerPrefs. Effectively, Unity ID are different each time.
using UnityEditor;
using UnityEngine;
[System.Serializable]
public class UniqueID{
public int ID;
static string key = "UniqueID";
public UniqueID(){
if(PlayerPrefs.HasKey("UniqueID")){
this.ID = PlayerPrefs.GetInt(key);
PlayerPrefs.SetInt(key,this.ID+1);
return;
}
PlayerPrefs.SetInt(key,1);
this.ID = 0;
}
}
[CustomPropertyDrawer (typeof (UniqueID))]
public class UniqueIDDrawer : PropertyDrawer {
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
EditorGUI.BeginProperty (position, label, property);
position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID (FocusType.Passive), label);
int indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
string value = property.FindPropertyRelative ("ID").intValue.ToString();
Rect IDRect = new Rect (position.x, position.y, 25f + value.Length * 6f, position.height+2f);
EditorGUI.LabelField(IDRect,value,GUI.skin.box);
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty ();
}
}
How to use:
using UnityEngine;
using System.Collections;
public class script : MonoBehaviour {
public UniqueID ID;
}
IMPORTANT: If you copy/paste GameObject or component, remember "Reset" the component to create unique ID.
Your answer
Follow this Question
Related Questions
How to write Multidimensional Hashes? 1 Answer
Unity 5 - nameHash obsolete? shortNameHash vs fullPathHash 0 Answers
Unique Identifier for both iOS 7 and for older iOS 2 Answers
Check asset bundles compatibility before downloading 0 Answers
How to Setup Openssl & Facebook SDK With Android key HASH on mac OS 10.11.6 1 Answer