When I create an instance of my custom ScriptableObject in the editor, some code in Awake will set certain properties based on finding a sprite asset with the same name. However, these changes do not show up in the inspector until I either run and stop the game, or restart Unity. Is there a way to make the changes show up straight away? I tried EditorUtility.SetDirty but it doesn’t help.
public void Awake()
{
if (spriteRef == null)
{
var results = AssetDatabase.FindAssets($"{name} t:sprite");
if (results.Length == 0)
Debug.LogError($"Cannot find sprite with name '{name}");
else if (results.Length == 1)
{
string guid = results[0];
string path = AssetDatabase.GUIDToAssetPath(guid);
spriteRef = AssetDatabase.LoadAssetAtPath<Sprite>(path);
if (spriteSize == Vector2.one)
{
spriteSize.x = spriteRef.texture.width;
spriteSize.y = spriteRef.texture.height;
}
EditorUtility.SetDirty(this);
Debug.Log($"Set defaults for {name}");
}
else
Debug.LogError($"More than one sprite with name '{name}!");
}
}
I have also tried SerializedObject with ApplyModifiedProperties and that didn’t help either.
public void Awake()
{
if (spriteRef == null)
{
var results = AssetDatabase.FindAssets($"{name} t:sprite");
if (results.Length == 0)
Debug.LogError($"Cannot find sprite with name '{name}");
else if (results.Length == 1)
{
string guid = results[0];
string path = AssetDatabase.GUIDToAssetPath(guid);
SerializedObject so = new SerializedObject(this);
var spriteRefLocal = AssetDatabase.LoadAssetAtPath<Sprite>(path);
so.FindProperty("spriteRef").objectReferenceValue = spriteRefLocal;
if (spriteSize == Vector2.one)
{
so.FindProperty("spriteSize").vector2Value = new Vector2(spriteRefLocal.texture.width, spriteRefLocal.texture.height);
}
so.ApplyModifiedPropertiesWithoutUndo();
Debug.Log($"Set defaults for {name}");
}
else
Debug.LogError($"More than one sprite with name '{name}!");
}
}