Getting Started
Welcome to the SuperBehaviour Getting Started Guide! This tutorial will guide you through the installation and basic usage of SuperBehaviour, an extension that simplifies Unity development while providing quality-of-life enhancements.
Prerequisites
Before starting, ensure you have:
- Unity Editor: Version 2022.3.16f1 or higher (Unity 6 is supported).
- Visual Studio Code (Optional): For enhanced editing with additional snippets and quality-of-life features.
- SuperBehaviour Unity Package: Available on the Unity Asset Store.
Installation
Install the SuperBehaviour Unity Package
- Open the Unity Editor.
- Navigate to the SuperBehaviour page on the Unity Asset Store.
- Click Add to My Assets and then Open in Unity to import the package into your project.
Install the SuperBehaviour VS Code Extension (Optional)
If you use Visual Studio Code, you can install the SuperBehaviour extension for additional features like context-aware code snippets and auto-adjustments for MonoBehaviour replacement.
- Open Visual Studio Code.
- Go to the SuperBehaviour for Unity extension page.
- Click Install to add the extension to your VS Code environment.
Basic Usage
The whole idea behind SuperBehaviour is to simplify your Unity development workflow. By using SuperBehaviour scripts instead of MonoBehaviour, you can access enhanced features like serialized properties, cached access to frequently used properties, and more. Most of the features are only available in SuperBehaviour scripts, so it's essential to replace MonoBehaviour with SuperBehaviour in your scripts to take full advantage of the extension.
Creating a SuperBehaviour Script
- In Unity, create a new SuperBehaviour script:
- Right-click in the Project window.
- Select Create > Scripts > SuperBehaviour.
- Name your script, for example,
MySuperScript
. - Open
MySuperScript.cs
in you IDE
using UnityEngine;
using LoM.Super;
public class MySuperScript : SuperBehaviour
{
private void Update()
{
// Access Transform and GameObject properties directly without having to worry about caching.
transform.Rotate(Vector3.up * Time.deltaTime * 10);
}
}
You can also replace MonoBehaviour with SuperBehaviour in existing scripts to access the enhanced features.
Serialized Properties
The [SerializeProperty]
attribute allows you to expose Properties (getters and setters) in the inspector like you can with fields. It will not be serialized in the final build but will be exposed in the inspector.
using UnityEngine;
using LoM.Super;
public class MySuperScript : SuperBehaviour
{
private Rigidbody m_rigidbody;
[SerializeProperty]
public int Speed => m_rigidbody.velocity.magnitude;
[SerializeProperty]
public float Rotation { get; set; }
}
SingletonBehaviour<T>
To make your script a singleton in one single line by inheriting from SingletonBehaviour<T>
.
using UnityEngine;
using LoM.Super;
public class GameManager : SingletonBehaviour<GameManager>
{
public void Start()
{
Debug.Log("Game Manager Initialized!");
}
}
- Access the instance using
GameManager.Instance
. - Ensures there is only one instance of the script in the scene.
- Use
LazySingletonBehaviour<T>
if you want to automatically create a new instance if none exists. - Use
AfterAwake()
andOnAfterDestroy()
to run code after the script is initialized and before it is destroyed.
More about Singletons: SingletonBehaviour<T> Documentation
Conditional Field Attributes
Use [ShowIf]
, [HideIf]
, [EnableIf]
, and [DisableIf]
to control field visibility in the inspector based on conditions. Where Show/Hide completely hides the field or property, and Enable/Disable only make it read-only.
using UnityEngine;
using LoM.Super;
public class ConditionalExample : SuperBehaviour
{
public bool showDetails;
[ShowIf("showDetails")]
public string details;
}
If
showDetails
is true, thedetails
field will appear in the inspector. No need to write a custom editor script just for this!
VS Code Extension
Using snippets
If you have installed the VS Code extension:
- Open the script, and the default MonoBehaviour snippets will automatically adjust to use
SuperBehaviour
. - This ensures your code integrates with SuperBehaviour’s features without manual changes.
MonoBeh▯
results in:
using UnityEngine;
using LoM.Super;
namespace my.project.namespace
{
public class NameOfMyScript : SuperBehaviour
{
}
}
More about snippets: VS Code Extension Documentation
Additional Resources
- Full API Documentation: Explore all SuperBehaviour features in detail.
- Community Discord: Get support, share ideas, and connect with other developers.
Now that you're equipped with SuperBehaviour, you're ready to elevate your Unity development workflow. Enjoy coding smarter and faster!