Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Quick Start

Daniel Everland edited this page Nov 23, 2019 · 17 revisions

Table of Contents

Requirement

The first thing you should do, is ensure your Unity editor version is compatible with SO-A. You currently need Unity version 2017.3 or higher

Installation Instructions

There are three ways you can install SO-A. Here's a short guide that explains the pros and cons of each method

+ Get updates as soon as they're ready.

+ Has an in-editor manager for updates.

+ Assets are kept in a separate section, which reduces clutter in project-view

+/- Assets will be locked, so you can't modify the source code in any way. Whether this is a good or bad thing is up to you

- Not properly supported until 2018.1

+ Has an in-editor manager for updates.

- Updates can take several days to be published, due to manual verification step by Unity.

- Installation and updating can be tedious

- No in-editor way to check for updates.

Unity Package Manager Instructions

  1. Ensure you're using Unity 2018.1 or higher
  2. Open your asset folder

  1. Open the "Packages" folder

  1. Open the "manifest.json" file in a text editor

  1. Add the following to your dependencies

"com.danieleverland.scriptableobjectarchitecture": "https://github.com/DanielEverland/ScriptableObject-Architecture.git#release/stable",

  1. Save changes. Unity will automatically import the package.

Unity Asset Store Instructions

  1. Open the Unity Asset Store window

  1. Search for "ScriptableObject-Architecture"

  1. Select the ScriptableObject-Architecture package and click on "IMPORT"

  1. Import all assets

Manual Installation

  1. Navigate to the GitHub Release Page

  2. Open the newest release and click on the .unitypackage file

  1. In Unity, click on Assets > Import Package > Custom Package...

  1. Select the downloaded .unitypackage file

  1. Import all assets

Tutorial

Under Construction

Throughout this tutorial I'll take you through the step-by-step instructions for using the following features of ScriptableObject-Architecture

  • Variables
  • References
  • Events
  • Typed events
  • Runtime sets

This will also give you a fundamental understanding of how using scriptable objects can improve your architecture in terms of decoupling systems and debugging them.

In order to keep the length of this tutorial manageable, I will only describe code and concepts relating to SO-A specifically. Everything else, like how to register collisions in Unity, code snippets for code that makes the player move etc. will not be covered. The entire repository is available in the branch called "tutorial", though, in case you want to check it out.

Creating a Player

The first thing we're going to do is create two assets. These assets will define the maximum- and current amount of health the player has. These are both what SO-A calls 'variables', as they store data, and both will be of the type float.

First, we'll create the max health variable. To do so, simply right-click somewhere in your project and select Create > Variables > float.

We're going to name this asset "PlayerMaxHealth" and set its value to 100

We're going to do the exact same thing for our players current health, but leave it blank, since it'll be managed by code.

Great! Now we need to tie this together with some code. For now the only responsibility of this piece of code is to set the current health to maximum when the game starts, but we'll expand it later on to add more complex behaviour.

This leads us to SO-A's next concept: references. References are what allows us to reference some value from our code without knowing where it comes from - all we care about is reading and writing to it, the rest is handled entirely from Unity's inspector. We're dealing with data of the type float, so we want to use the reference type called FloatReference.

We simply create a component that includes the following snippet

[SerializeField]
private FloatReference currentHealth = null;
[SerializeField]
private FloatReference maxHealth = null;

private void Awake()
{
    currentHealth.Value = maxHealth.Value;
}

In order to couple our assets to these values, we simply drag them into the inspector

Let's move on to something more complex. We want to be able to know when the player dies, but it doesn't really make sense the variable's value every frame. Instead we're going to introduce a new concept: event listeners. Event listeners automatically subscribe to an event (or a variable, in this case), and will let you know when said event is raised. If you subscribe to a variable, the event is raised whenever the value of the variable changes.

First step is to add a GameEventListener component to our player, which is our event listener, obviously.

Next, we drag our current health variable into the field called "Event"

Alright, now we want to couple this event listener to our script. First, we create a function in our player health component the event listener can call - the important part here is that the protection level of the function is public, otherwise the event listener component won't be able to see it.

public void HealthChanged()
{
}

Next step is to couple the event listener with this function. This step uses a Unity feature called UnityEvents, so I'll skip the details, but you basically drag the component that contains the function, and then select the function from the dropdown.

Last step is to add functionality to our function. If the current health is zero or below, we destroy it.

public void HealthChanged()
{
    if (currentHealth.Value < 0)
        Destroy(gameObject);
}
  • Player (Variables - Max Health, Clamped Variables - Current Health, References)

Adding a healthbar

  • Health UI (Events - OnDamagedEvent, decoupling)

Taking damage

  • Damage Indicator (Typed event)

Placing cubes

  • Placing Cubes (Runtime sets)
Clone this wiki locally