UnityScript (Javascript in Unity) **is not** Javascript and absolutely does have classes!
However UnityScript cannot define a generic class - it can however inherit from one...
If you defined your base MonoSingleton class in C# in a Plugins folder then you could use:
class TestInherit extends MonoSingleton.
{
}
The base class would look like this:
using System.Collections;
public class MonoSingleton : MonoBehaviour where T: MonoSingleton {
public static T instance;
void Awake()
{
instance = this as T;
AwakeEx();
}
protected virtual void AwakeEx()
{
}
protected virtual void OnDestroyEx()
{
}
void OnDestroy()
{
OnDestroyEx();
if(instance == this) instance = null;
}
}
Note to avoid problems you should use AwakeEx and OnDestroyEx in the classes derived from MonoSingleton - they work just the same way but ensure the singleton is handled properly
While I'm at it, here's a base class that creates a static collection of all the enabled derivatives:
using System.Collections.Generic;
using Unity.Engine;
public class MonoCollection : MonoBehaviour where T : MonoCollection
{
public static List all = new List();
protected virtual void OnEnableEx()
{
}
protected virtual void OnDisableEx()
{
}
void OnEnable()
{
all.Add(this as T);
OnEnableEx();
}
void OnDisable()
{
all.Remove(this as T);
OnDisableEx();
}
}
↧