Class LunyObject
Engine-agnostic wrapper for engine objects. Safeguards against NullReferenceExceptions when the engine-native object may have been destroyed.
public abstract class LunyObject : ILunyObject
- Inheritance
-
objectLunyObject
- Implements
Constructors
LunyObject(object, long, bool, bool)
Instantiates a LunyObject instance.
protected LunyObject(object nativeObject, long nativeObjectId, bool isNativeObjectEnabled, bool isNativeObjectVisible)
Parameters
nativeObjectobjectnativeObjectIdlongisNativeObjectEnabledboolisNativeObjectVisiblebool
Properties
IsEnabled
Whether the engine object is processing and visible. Matches the "Active" state of Unity.
public bool IsEnabled { get; set; }
Property Value
- bool
Remarks
The object may be enabled, but still be disabled in the hierarchy due to a parent not being enabled. IsEnabled also toggles visibility. If the object's IsVisible was set to false, and then the object gets enabled, the object's IsVisible state will also change to true.
IsEnabledInHierarchy
Returns true only if BOTH the object itself AND all of its parents are enabled. Otherwise returns false.
public bool IsEnabledInHierarchy { get; }
Property Value
- bool
IsNativeObjectValid
Is true when Destroy() was called on the NativeObject and it was set to null.
public bool IsNativeObjectValid { get; }
Property Value
- bool
IsValid
Whether the object and its native representation are valid (not null, not destroyed).
public bool IsValid { get; }
Property Value
- bool
IsVisible
Whether the object is visible (will render).
public bool IsVisible { get; set; }
Property Value
- bool
Remarks
This property does NOT imply that the object is currently visible on screen. It is technically visible, but may still be outside the camera's frame, obstructed by other objects, fully transparent, scaled infinitely small, etc.
LunyObjectId
Unique, immutable identifier for LunyObject. This ID is distinct from engine's native object ID!
public LunyObjectId LunyObjectId { get; }
Property Value
Name
The name of the object in the scene hierarchy.
public string Name { get; set; }
Property Value
- string
Remarks
To aid debugging, this property remains valid even after the object has been destroyed.
NativeObject
Gets the underlying engine-native object (GameObject, Node) as generic System.Object type. Use the Cast<T>() or As<T>() methods to avoid manually casting the reference.
public object NativeObject { get; }
Property Value
- object
NativeObjectId
Engine-native object's unique, immutable identifier. Subject to engine's behaviour (ie may change between runs).
public LunyNativeObjectId NativeObjectId { get; }
Property Value
Remarks
To aid debugging, this ID remains valid after the engine-native object has been destroyed.
Transform
The LunyTransform of this object.
public LunyTransform Transform { get; }
Property Value
Methods
As<T>()
Gets the engine-native object as type T. Returns null for non-matching types.
public T As<T>() where T : class
Returns
- T
Type Parameters
T
Cast<T>()
Gets the engine-native object cast to T. Throws if the type cast is invalid.
public T Cast<T>() where T : class
Returns
- T
Type Parameters
T
Clone()
Creates a new instance of the current object.
public abstract ILunyObject Clone()
Returns
Clone(LunyTransform)
Creates a new instance of the current object and parents it.
public abstract ILunyObject Clone(LunyTransform parent)
Parameters
parentLunyTransform
Returns
Destroy()
Marks this object for destruction. If object is enabled, will run its OnDisabled event. Then it run its OnDestroyed event.
public void Destroy()
Remarks
The engine-native object destruction is deferred until the end of the current frame to prevent exceptions.
DestroyNativeObject()
protected abstract void DestroyNativeObject()
~LunyObject()
protected ~LunyObject()
GetNativeObjectEnabled()
protected abstract bool GetNativeObjectEnabled()
Returns
- bool
GetNativeObjectEnabledInHierarchy()
protected abstract bool GetNativeObjectEnabledInHierarchy()
Returns
- bool
GetNativeObjectName()
protected abstract string GetNativeObjectName()
Returns
- string
GetNativeTransform()
protected abstract LunyTransform GetNativeTransform()
Returns
Initialize()
public void Initialize()
InvokeOnCollisionEntered(LunyCollision)
public void InvokeOnCollisionEntered(LunyCollision collision)
Parameters
collisionLunyCollision
InvokeOnCollisionEntered2D(LunyCollision2D)
public void InvokeOnCollisionEntered2D(LunyCollision2D collision)
Parameters
collisionLunyCollision2D
InvokeOnCollisionExited(LunyCollision)
public void InvokeOnCollisionExited(LunyCollision collision)
Parameters
collisionLunyCollision
InvokeOnCollisionExited2D(LunyCollision2D)
public void InvokeOnCollisionExited2D(LunyCollision2D collision)
Parameters
collisionLunyCollision2D
InvokeOnCollisionUpdate(LunyCollision)
public void InvokeOnCollisionUpdate(LunyCollision collision)
Parameters
collisionLunyCollision
InvokeOnCollisionUpdate2D(LunyCollision2D)
public void InvokeOnCollisionUpdate2D(LunyCollision2D collision)
Parameters
collisionLunyCollision2D
InvokeOnTriggerEntered(LunyCollider)
public void InvokeOnTriggerEntered(LunyCollider collider)
Parameters
colliderLunyCollider
InvokeOnTriggerEntered2D(LunyCollider2D)
public void InvokeOnTriggerEntered2D(LunyCollider2D collider)
Parameters
colliderLunyCollider2D
InvokeOnTriggerExited(LunyCollider)
public void InvokeOnTriggerExited(LunyCollider collider)
Parameters
colliderLunyCollider
InvokeOnTriggerExited2D(LunyCollider2D)
public void InvokeOnTriggerExited2D(LunyCollider2D collider)
Parameters
colliderLunyCollider2D
InvokeOnTriggerUpdate(LunyCollider)
public void InvokeOnTriggerUpdate(LunyCollider collider)
Parameters
colliderLunyCollider
InvokeOnTriggerUpdate2D(LunyCollider2D)
public void InvokeOnTriggerUpdate2D(LunyCollider2D collider)
Parameters
colliderLunyCollider2D
IsNativeObjectReferenceValid()
protected abstract bool IsNativeObjectReferenceValid()
Returns
- bool
SetNativeObjectDisabled()
protected abstract void SetNativeObjectDisabled()
SetNativeObjectEnabled()
protected abstract void SetNativeObjectEnabled()
SetNativeObjectInvisible()
protected abstract void SetNativeObjectInvisible()
SetNativeObjectName(string)
protected abstract void SetNativeObjectName(string name)
Parameters
namestring
SetNativeObjectVisible()
protected abstract void SetNativeObjectVisible()
ToString()
public override string ToString()
Returns
- string
TryGetCached(long, out ILunyObject)
protected static bool TryGetCached(long nativeId, out ILunyObject lunyObject)
Parameters
nativeIdlonglunyObjectILunyObject
Returns
- bool
Events
OnCollisionEntered
Runs when the object's collider has "entered" (overlaps) another static or non-kinematic collider.
public event Action<LunyCollision> OnCollisionEntered
Event Type
- Action<LunyCollision>
OnCollisionEntered2D
public event Action<LunyCollision2D> OnCollisionEntered2D
Event Type
- Action<LunyCollision2D>
OnCollisionExited
Runs when the object's collider stops overlapping/touching another static or non-kinematic collider.
public event Action<LunyCollision> OnCollisionExited
Event Type
- Action<LunyCollision>
OnCollisionExited2D
public event Action<LunyCollision2D> OnCollisionExited2D
Event Type
- Action<LunyCollision2D>
OnCollisionUpdate
Runs every heartbeat while the collision is ongoing.
public event Action<LunyCollision> OnCollisionUpdate
Event Type
- Action<LunyCollision>
OnCollisionUpdate2D
public event Action<LunyCollision2D> OnCollisionUpdate2D
Event Type
- Action<LunyCollision2D>
OnCreated
Runs when the object was created or ownership was transferred to LunyEngine. Runs even if the object starts disabled.
public event Action OnCreated
Event Type
- Action
Remarks
Engine-native creation events (Unity: Awake, OnEnable / Godot: ctor, _init) will run before OnCreated. OnEnabled will run right after OnCreated if the object starts enabled.
OnDestroyed
Runs when the object was destroyed. Runs even if the object is disabled.
public event Action OnDestroyed
Event Type
- Action
Remarks
The object's NativeObject reference is still accessible, it has not been destroyed yet.
OnDisabled
Runs when the object's enabled state changes to "disabled": hidden, not updating, not receiving events, not interacting with other objects.
public event Action OnDisabled
Event Type
- Action
OnEnabled
Runs when the object's enabled state changes to "enabled": visible, updating, receiving events, interacting with other objects. Also runs right after OnCreated if the object starts enabled.
public event Action OnEnabled
Event Type
- Action
OnReady
Runs once before the object's first frame processing, before OnFrameUpdate and OnHeartbeat. If the object starts disabled, OnReady runs after the object first gets enabled.
public event Action OnReady
Event Type
- Action
Remarks
It is not guaranteed that the object will simulate physics in its first active frame. The event order is either: OnReady => OnFrameUpdate (object's first) or: OnReady => OnHeartbeat (object's first) => OnFrameUpdate (object's first)
OnTriggerEntered
Runs when first overlapping a trigger collider.
public event Action<LunyCollider> OnTriggerEntered
Event Type
- Action<LunyCollider>
OnTriggerEntered2D
public event Action<LunyCollider2D> OnTriggerEntered2D
Event Type
- Action<LunyCollider2D>
OnTriggerExited
Runs when leaving a trigger collider.
public event Action<LunyCollider> OnTriggerExited
Event Type
- Action<LunyCollider>
OnTriggerExited2D
public event Action<LunyCollider2D> OnTriggerExited2D
Event Type
- Action<LunyCollider2D>
OnTriggerUpdate
Runs while overlapping a trigger collider.
public event Action<LunyCollider> OnTriggerUpdate
Event Type
- Action<LunyCollider>
Remarks
In Unity, to receive this event you have to explicitly enable it: Project Settings: Physics/Settings -> GameObject -> Generate On Trigger Stay Events
OnTriggerUpdate2D
public event Action<LunyCollider2D> OnTriggerUpdate2D
Event Type
- Action<LunyCollider2D>