MyBox icon indicating copy to clipboard operation
MyBox copied to clipboard

Feature request: GUID for Scriptable Objects

Open CraftedPvP opened this issue 4 years ago • 2 comments

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;
}

image

I think this would be a great addition to add. but, feel free to adjust those above

CraftedPvP avatar Aug 26 '21 02:08 CraftedPvP

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?

tonygiang avatar Aug 27 '21 09:08 tonygiang

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

CraftedPvP avatar Aug 27 '21 11:08 CraftedPvP