I find it pretty redundant to have a ScriptableObject with a name variable, and having to separately name the ScriptableObject file and the variable field in the inspector (if they’re the same).
Is there any way of assigning the script’s file name to a string variable within it?
Just curious. I have no issue naming them both individually as it doesn’t take that much more time, but if possible, I’d like to be able to automate it a little.
The easiest way to do it would be to simply use a property.
public string Label
{
get
{
return name;
}
}
But exposing properties in the inspector is the harder part in the default inspector (there are add-ons for this, though).
If that’s not an option it becomes a bit trickier to do, if you want it to be foolproof. You would need to detect changes made by the user to both the variable, as well as the filename.
You could use OnValidate to react to changes made to the variable value. If the variable doesn’t match the filename, you could rename the asset based on the variable using AssetDatabase.RenameAsset.
You could use an AssetModificationProcessor to detect changes to the filename and set the variable value based on it.
1 Like