MyBox
MyBox copied to clipboard
Feature request: GUID for Scriptable Objects
If we have a lot of inventory items and other heavily reliant scriptable objects, we'd normally utilize an ID system. But a common problem is that when duplicating and copying scriptable objects, its ID gets duplicated; hence, making their IDs not unique.
We can grab the unique ID from the meta file like what this user did:
BaseScriptableObject.cs :
using System;
using UnityEditor;
using UnityEngine;
public class ScriptableObjectIdAttribute : PropertyAttribute { }
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(ScriptableObjectIdAttribute))]
public class ScriptableObjectIdDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
GUI.enabled = false;
if (string.IsNullOrEmpty(property.stringValue)) {
property.stringValue = Guid.NewGuid().ToString();
}
EditorGUI.PropertyField(position, property, label, true);
GUI.enabled = true;
}
}
#endif
public class BaseScriptableObject : ScriptableObject {
[ScriptableObjectId]
public string Id;
}
Weapon.cs (Example scriptable object):
using System;
using UnityEngine;
[CreateAssetMenu(fileName = "weapon", menuName = "Items/Weapon")]
public class Weapon : BaseScriptableObject {
public int Attack;
public int Defence;
}

I think this would be a great addition to add. but, feel free to adjust those above
Wouldn't this feature be more adaptable to general architectures as an attribute instead of a base class that every SO has to inherit from?
I don't think you saw the attribute named [ScriptableObjectId] found in the base scriptable object.
But, yes. This suggestion is going for an attribute. The attribute name is just named like that as I copied it from StackOverflow