- Home /
How to tie Scriptable Object's file name with a "name" field in a Scriptable Object?
I just learned about ScriptableObjects and I think they're great. But after working for a while, I realized Unity could use a lot of things to streamline the workflow. As for now, I want to manually write code that will help me along.
I want to be able to say Assets -> Create -> ScriptableObjectName and have that Asset's filename be automatically highlighted in my inspector, the same way creating a new C# script works. Then I want to type in my name (just like renaming something). Optional: when I hit enter, the name I entered in the inspector will also update a field in the ScriptableObject called "name."
Or, I want to be able to input a name field in an already created ScriptableObject asset and when I hit enter in the inspector, I want the filename of the ScriptableObject asset to change to match my name field.
If anyone knows of any way to achieve at least one of the above, I would be a happy camper. ;)
Answer by H1ddeNGames · Dec 17, 2018 at 09:13 AM
I know this question is over a year old but whoever finds this might be helped by this solution.
If you want to create an asset by right clicking on your project folder or going into the Assets Menu > Create then do this:
[CreateAssetMenu(fileName = "New Item", menuName = "H1ddenGames/ItemSystem/Item")]
public class Item : ScriptableObject
When you make it using this method, it should automatically highlight the name of the new ScriptableObject file so you can rename as you wish.
I couldn't exactly figure out how to change the name field of the ScriptableObject upon pressing enter but I did do it this way in another script.
string assetPath = AssetDatabase.GetAssetPath(item.GetInstanceID());
item.ItemIdentifiers.ItemName = Path.GetFileNameWithoutExtension(assetPath);
Here "item" is the Scriptable Object file that I have in this type of folder structure:
= Scriptable Object = = Resources = = = Items = = = = Armor
Please note that Scriptable Object is the top level folder, then Resources is inside Scriptable Object, then Items is inside Resources... etc.
Placing the above code into a method like the one below will allow you to press the context wheel in the inspector of the script you place this code snippet into and press "FileNameToNameField" option that will run even outside of play mode.
[ContextMenu("FileNameToNameField")]
public void FileNameToNameField()
{
if(ArmorList.Count > 0)
{
foreach (var item in ArmorList)
{
string assetPath = AssetDatabase.GetAssetPath(item.GetInstanceID());
item.ItemIdentifiers.ItemName = Path.GetFileNameWithoutExtension(assetPath);
}
}
}
Hope that helped. If it doesn't make sense try to look at my project that implements this idea here: https://github.com/h1ddengames/Item-System
I have several other projects where I try to do unconventional things like what you're tying to do here with this question so feel free to take a look and get ideas.
You can use OnValidate for this.
#if UNITY_EDITOR
void OnValidate() {
string assetPath = AssetDatabase.GetAssetPath(this.GetInstanceID());
displayName = Path.GetFileNameWithoutExtension(assetPath);
}
}
#endif
Answer by aurorasunrisegames · Jun 29, 2021 at 07:45 AM
I was looking for a similar answer and try the comment below, than do few minutes research and here it is, more simplier issue. Here is my thanks to you, @H1ddeNGames
This script will change the name value after you edit any other values of SO.
using System.IO; using UnityEditor; using UnityEngine;
[CreateAssetMenu(fileName = "newCell", menuName = "New Cell")] public class Cell : ScriptableObject { public new string name; public Sprite sprite; public Color32 backColor = Color.white;
private void OnValidate()
{
name = Path.GetFileNameWithoutExtension( AssetDatabase.GetAssetPath(this));
}
}
Your answer
