CustomAssets
Askowl Custom Assets
Base.cs
1 // Copyright 2018,19 (C) paul@marrington.net http://www.askowl.net/unity-packages
2 using System;
3 using Askowl;
4 using UnityEngine;
5 
6 namespace CustomAsset {
7  /// <a href="http://bit.ly/2CwSS8S">Base class for all custom assets - implementing initialisation</a>
8  public class Base : ScriptableObject {
9  /// <a href="http://bit.ly/2CzuMKF">If this is a project asset, then you will need to reference it somewhere. Other classes can get a reference using `Instance()` or `Instance(string name)`. Also useful for creating in-memory versions to share between hosts</a>
10  public static T Instance<T>(string path) where T : Base => Resources.Load<T>(path);
11 
12  [SerializeField, Multiline] private string description = default;
13 
14  /// <a href="http://bit.ly/2CwSVS6">Editor only description of what the asset is all about</a>
15  public string Description => description;
16 
17  /// <a href="http://bit.ly/2CzuMKF"></a>
18  [NonSerialized] protected bool Initialised;
19 
20  /// <a href="http://bit.ly/2CzuMKF">Called by Managers MonoBehaviour</a>
21  public void Initialiser() {
22  if (Initialised) return;
23  Initialised = true;
24  Initialise();
25  }
26  /// <a href="http://bit.ly/2CzuMKF">Called by Managers MonoBehaviour or when mutual data is first accessed</a>
27  protected virtual void Initialise() { }
28 
29  /// make sure that this item will be initialised at least one frame after it is enabled and the scene is loaded
30  protected virtual void OnEnable() {
31  AssetsWaitingInitialisation.Push(this);
33  }
34  /// triggered by scene load in <see cref="Manager"/>
35  protected static readonly Emitter InitialiseAssetEmitter = Emitter.SingleFireInstance;
36 
37  /// initialisation for all assets loaded before the related/first scene
38  protected static readonly Fifo<Base> AssetsWaitingInitialisation = Fifo<Base>.Instance;
39 
40  /// clear the decks as all is being destroyed
41  protected virtual void OnDisable() => Initialised = false;
42  }
43 }
static readonly Emitter InitialiseAssetEmitter
triggered by scene load in Manager
Definition: Base.cs:35
string Description
Editor only description of what the asset is all about
Definition: Base.cs:15
static T Instance< T >(string path)
If this is a project asset, then you will need to reference it somewhere. Other classes can get a ref...
void Initialiser()
Called by Managers MonoBehaviour
Definition: Base.cs:21
bool Initialised
Definition: Base.cs:18
static readonly Fifo< Base > AssetsWaitingInitialisation
initialisation for all assets loaded before the related/first scene
Definition: Base.cs:38
virtual void Initialise()
Called by Managers MonoBehaviour or when mutual data is first accessed
Definition: Base.cs:27
virtual void OnEnable()
make sure that this item will be initialised at least one frame after it is enabled and the scene is ...
Definition: Base.cs:30
Base class for all custom assets - implementing initialisation
Definition: Base.cs:8
virtual void OnDisable()
clear the decks as all is being destroyed