Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: GUID for Scriptable Objects #178

Open
CraftedPvP opened this issue Aug 26, 2021 · 2 comments
Open

Feature request: GUID for Scriptable Objects #178

CraftedPvP opened this issue Aug 26, 2021 · 2 comments

Comments

@CraftedPvP
Copy link

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

@tonygiang
Copy link
Contributor

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?

@CraftedPvP
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants