Skip to content

Scriptable Architecture

VirtueSky edited this page Sep 4, 2024 · 21 revisions

What

Scriptable Objects are an immensely powerful yet often underutilized feature of Unity. Learn how to get the most out of this versatile data structure and build more extensible systems and data patterns.

Introductory video about Game Architecture with Scriptable Objects

01-image-F-1020x574

Use

Scriptable Event

Create Scriptable Event

You can create Scriptable Event by 3 option

  • Option 1: via menu Create > Sunflower > Scriptable > Event

create_event1

  • Option 2: menu item Sunflower > Scriptable > Create Event

create_event2

  • Option 3: Open tab Scriptable Event in Magic Panel

Screenshot 2024-09-04 173432

  • Scriptable Event no parameter

event_noparm

  • Scriptable Event has parameter (int, float, string, bool, vector3,...)

event_float

Raise Events

You can raise events by code or by the inspector (Enable Debug Field > button Raise Event)

Raise by code

public class Player : MonoBehaviour
{
    public EventNoParam playerAttackEvent;

    public void Attack()
    {
        playerAttackEvent.Raise();
    }
}

Raise by inspector (button Raise Event)

  • Note: It helps you find errors faster when encountering bugs and only works editor playing

event_noparm

Listener Events

You can listener event by component Event Listener or by code

Listener by component

Screenshot 2024-03-01 101428

  • Binding
    • UNTIL_DISABLE : the event is subscribed when OnEnable() and unsubscribed when OnDisable()
    • UNTIL_DESTROY : the event is subscribed when Awake() and unsubscribed when OnDestroy()
  • Event Response Data
    • Event : Drag scriptable event here
    • Response() : Response() works similarly to a button's OnClick, When the above Event is raised, the functions embedded in Response() will be called
  • Raise & valueDebug : Similar to using raise event in inspector scriptable event, it helps you find errors faster when encountering bugs (it only works editor playing)

Listener by code

  • Use AddListener to subscribe and unsubscribe
 public EventNoParam playerAttackEvent;

    private void OnEnable()
    {
        playerAttackEvent.AddListener(HandleAttack);
    }

    private void OnDisable()
    {
        playerAttackEvent.RemoveListener(HandleAttack);
    }

    void HandleAttack()
    {
        // Attack
    }
  • Use OnRaised to subscribe and unsubscribe
public EventNoParam playerAttackEvent;

    private void OnEnable()
    {
        playerAttackEvent.OnRaised += HandleAttack;
    }

    private void OnDisable()
    {
        playerAttackEvent.OnRaised -= HandleAttack;
    }

    void HandleAttack()
    {
        // Attack
    }

Scriptable Variable

Create Scriptable Variable

  • Option 1: via menu Create > Sunflower > Scriptable > Variables
  • Option 2: menu item Sunflower > Scriptable > Create Variable
  • Option 3: Open tab Scriptable Variable in Sunflower Control Panel
  • You can create Scriptable Variable with bool, float, int, string, object, Rect, Short Double, Transform, Vector3 values ...

Screenshot 2024-02-28 095805

  • Scriptable Variables also has a Raise Event capability similar to Scriptable Events. it can also have the same value as regular variables

Change value - Raise Event

  • Initialize Value: Initialize default value for scriptable variable
  • Is Set Data: With true then change the value set in the dictionary, the value will automatically be saved to the file when pausing the game or exiting the game. Otherwise, it will not be installed, now the used value of Scriptable Variable is the runtime value
  • Is Save Data: With true the value will be saved to the file immediately. Otherwise, it will not be saved
  • Is Raise Event: With true then any change to the value of Scriptable Variable will be Raise Event
  • Change value by code
 public StringVariable skinName;

    void ChangeSkinName()
    {
        skinName.Value = "skin_2";
    }

Listener Variable

  • Listener by code
 public StringVariable skinName;

    private void OnEnable()
    {
        skinName.AddListener(HandleChangeSkin);
    }

    private void OnDisable()
    {
        skinName.RemoveListener(HandleChangeSkin);
    }

    void HandleChangeSkin(string skinName)
    {
        // handel skin
    }

or

public StringVariable skinName;

    private void OnEnable()
    {
        skinName.OnRaised += HandleChangeSkin;
    }

    private void OnDisable()
    {
        skinName.OnRaised -= HandleChangeSkin;
    }

    void HandleChangeSkin(string skinName)
    {
        // handel skin
    }
  • Listener by component

Similar to Scriptable Event

Screenshot 2024-02-28 102918

Scriptable Event-Result

  • The way to create and use is similar to Scriptable Event, however it supports returning a value when Raised
  • Note: Scriptable Event-Result is only listened to by code

Raise Event

    public StringEventBoolResult isSetupSkinSuccessEvent;

    private bool isSetupSuccess;

    void SetupSkin()
    {
        isSetupSuccess = isSetupSkinSuccessEvent.Raise("Skin_2");
    }

Listener Event

public StringEventBoolResult isSetupSkinSuccessEvent;

    private bool isSetupSuccess;

    private void OnEnable()
    {
        isSetupSkinSuccessEvent.AddListener(HandleSetupSkin);
    }

    private void OnDisable()
    {
        isSetupSkinSuccessEvent.RemoveListener(HandleSetupSkin);
    }

    bool HandleSetupSkin(string skinName)
    {
        //Handle Setup Skin
        return isSetupSuccess;
    }

or

public StringEventBoolResult isSetupSkinSuccessEvent;

    private bool isSetupSuccess;

    private void OnEnable()
    {
        isSetupSkinSuccessEvent.OnRaised += HandleSetupSkin;
    }

    private void OnDisable()
    {
        isSetupSkinSuccessEvent.OnRaised -= HandleSetupSkin;
    }

    bool HandleSetupSkin(string skinName)
    {
        //Handle Setup Skin
        return isSetupSuccess;
    }