I have the following scriptable object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/SpawnManagerScriptableObject", order = 1)]
public class Style : ScriptableObject
{
public Texture2D[] textures;
public Sprite[] sprites;
public AnimationCurve animationCurve;
Sprite[] MakeSprites(Texture2D[] baseTextures){
Sprite[] output = new Sprite[baseTextures.Length];
for (int i = 0; i < baseTextures.Length; i++)
{
output[i] = MakeSprite(baseTextures[i]);
}
return output;
}
Sprite MakeSprite(Texture2D baseTexture)
{
Sprite sprite = Sprite.Create(baseTexture, new Rect(0, 0, baseTexture.width, baseTexture.height), new Vector2(baseTexture.width / 2f, baseTexture.height / 2f), Mathf.Max(baseTexture.width, baseTexture.height));
return sprite;
}
public void UpdateSprites()
{
sprites = MakeSprites(textures);
}
}
[CustomEditor(typeof(Style))]
public class customStyleEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
Style style = (Style) target;
if (GUILayout.Button("Update Sprites"))
{
style.UpdateSprites();
EditorUtility.SetDirty(target);
}
}
}
This works as I would expect, but when I enter play mode, the sprites
field resets to being empty, I'm pretty sure this is to do with my custom editor script, but I have tried several fixes without any success.
Including:
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(target)
Everything else I have tried has caused an error.
So, what is the problem here and how do I fix it?