How to invoke Unity API in YAHAHA Studio
Overview
Unity APIs are organized by type (class, struct, or enum), and each member of a class is categorized into three parts: Fields and Properties, Constructors, and Methods. This guide will explain how to access and use these members in YAHAHA Studio using Lua.
Fields and Properties
Fields and properties in Unity API are described in two formats:
- Static members:
<field-type> <type>.<field-name>
- Instance members:
<field-type> <field-name>
Example (from UnityEngine.Rect):
(static) UnityEngine.Rect UnityEngine.Rect.zero
UnityEngine.Vector2 position
In Lua scripts within YAHAHA Studio, these fields and properties are accessed differently:
- Instance members are accessed using the object instance:
objectInstance.fieldName
- Static members are accessed using the type name:
TypeName.fieldName
Lua usage:
-- Accessing a static property
local rect = UnityEngine.Rect.zero
-- Accessing an instance property
local pos = rect.position
Constructors
In YAHAHA Studio, constructors are described using the format: <type>.New(<parameters>)
.
Example(from UnityEngine.Rect):
UnityEngine.Rect.New(float, float, float, float)
The type UnityEngine.Rect
has a constructor with 4 parameters, which can be invoked in Lua as follows:
Lua usage:
local rect = UnityEngine.Rect.New(1, 2, 3, 4)
Methods
Methods in Unity API are described in two formats:
- Instance methods:
<return-type> <method-name>(<parameters>)
- Static methods:
<return-type> <type>.<method-name>(<parameters>)
Example (from UnityEngine.Rect):
void Set(float, float, float, float)
(static) UnityEngine.Rect UnityEngine.Rect.MinMaxRect(float, float, float, float)
In Lua scripts within YAHAHA Studio, these methods are invoked differently:
- Instance methods are called using the colon
:
syntax:objectInstance:methodName(parameters)
- Static methods are called using the dot
.
syntax:TypeName.methodName(parameters)
Lua usage:
-- Instance method
rect:Set(1, 2, 3, 4)
-- Static method
local rect2 = UnityEngine.Rect.MinMaxRect(5, 4, 3, 2)