Skip to main content

Serialized Fields

Serialization allows you to define the values of variables in the editor. This allows further extension of code reusability and customization. Using the Serialized Fields, you can set a variable as a reference to another game object in your scene. A serialized variable will show up in the editor, under LuaScript inspector, where the values can be edited.

Serializable types

The following types can be serialized:

Serialize a variable

use the following API to create a serialization

{Bool | Number | String | Object} SerializedField(string fieldName, TYPE type, number count = 1);
FunctionCreates and returns the serialized Field.
Parametersstring fieldName: The name of the field appears in the inspector.
TYPE type: The types of serialization.
number count: Number of variable. Define a number to create a array or variables.
ReturnsThe value of the serialization. The type depends on the type of serialization.

Example

  • To create a Boolean serialization, use the following method:
local mySerializedVariable = SerializedField("Name", Bool);

The field will automatically appear under the Serialized Fields title, in the Inspector of the LuaScript components where the script is attached to:

  • To Create a GameObject serialization:
local mySerializedGameObject = SerializedField("GO", GameObject);

  • To Create a component serialization:
local mySerializedComponent = SerializedField("rigidbody", Rigidbody);

  • To Create an asset type serialization:
local mySerializedAsset = SerializedField("Texture", Texture);

  • To Create an array serialization:
local clips = SerializedField("Clips", AudioClip, 5);

  • An Example on how to use the serialized fields:
do -- script audio box

-- get reference to the script
local AudioBox = LUA.script;
-- get an array of clips to play
local clips = SerializedField("Clips", AudioClip, 5);

-- get audio source
local auidoSource = SerializedField("Audio Source", AudioSource);

-- current playing clip index
local aIndex = 1;

-- start only called at beginning
function AudioBox.Start()
-- set audio source to not looping
auidoSource.loop = false;
end

-- update called every frame
function AudioBox.Update()
-- change clip if audio source is not playing
if not auidoSource.isPlaying then
auidoSource.clip = clips[aIndex];
auidoSource.Play();
Debug.Log("Playing " .. clips[aIndex].name);
-- increment index
aIndex = aIndex + 1;

-- reset index when it reaches the end
if(aIndex > #clips) then aIndex = 1; end
end
end
end

Also checkout: Transform, Debug

warning

The editor will allow any type of Unity Object based components and assets to be serialized, however, if the said component or asset type is not supported by the Massive Loop, the SerializedField function will return a nil and show a warning when executing the code.

Serialize an Unity Event

You can add an event to serialized fields by calling the serialized field function with Event type.

local myEvent = SerializedField("Event Name", Event);

This will create an interface in inspector which allows to attach callbacks to this event from any other component, the very same concept as UnityEvents.

You can invoke any attached callbacks using the Invoke function:

do

local script = LUA.script;

local myEvent = SerializedField("Event Name", Event);

function script.Start()
-- invoke all listeners
myEvent.Invoke();
end


end

Footnotes

  1. Components that are supported by Massive Loop are components which are implemented. A best rule of thumb to know if a component is implemented or not to check if the ML API Documentation contains that said component.

  2. Assets that are supported by Massive Loop are assets which are implemented. A best rule of thumb to know if an asset type is implemented or not to check if the ML API Documentation contains that said Asset type.