Hi there,
I’ve been trying to follow an official Unity tutorial (though dated from 2018, link here) for flexible UI using ScriptableObjects, it would seem i’m either missing a step or the behaviour of ScriptableObjects since this tutorial was released has changed, in which case the tutorial should be taken down.
We start with the FlexibleUIData class
[CreateAssetMenu(menuName = "Flexible UI Data")]
public class FlexibleUIData : ScriptableObject
{
public Sprite ButtonSprite;
public SpriteState ButtonSpriteState;
}
Which has a flexible UI class which inherits from it
[ExecuteInEditMode()]
public class FlexibleUI : FlexibleUIData
{
public FlexibleUIData skinData;
protected virtual void OnSkinUI()
{
}
public virtual void Awake()
{
OnSkinUI();
}
public virtual void Update()
{
if (Application.isEditor)
{
OnSkinUI();
}
}
}
And then finally our FlexibleUIButton which is what I’m after ultimately.
[RequireComponent(typeof(Button))]
[RequireComponent(typeof(Image))]
public class FlexibleUIButton : FlexibleUI
{
private Image _image;
private Button _button;
protected override void OnSkinUI()
{
base.OnSkinUI();
_image = GetComponent<Image>();
_button = GetComponent<Button>();
_button.transition = Selectable.Transition.SpriteSwap;
_button.targetGraphic = _image;
_image.sprite = skinData.ButtonSprite;
_image.type = Image.Type.Sliced;
_button.spriteState = skinData.ButtonSpriteState;
}
}
Now, the issue is that a ScriptableObject does not contain the GetComponent methods, and also seemingly cannot be added to objects because of this; changing ScriptableObject to a MonoBehaviour throws all manor of null errors, seemingly because we can no longer set up our default skin?
If there is a new way to do this, is there a tutorial or sample code for that new method, or am I doing something wrong here?
Many thanks,
Ben