Skip to main content

Step-by-Step Guide

This guide will walk you through the process of creating custom actions and triggers for your VR story. Follow these steps to extend the functionality of your VRse Builder system.

1. Create a Folder for Custom Actions and Triggers

  1. In your Unity project, create a new folder under Assets to store your custom scripts. Example: Assets/CustomActionsAndTriggers.
  2. This folder will house all your custom action and trigger scripts.

2. Identify or Create Mechanic Script

  1. Explore the AutoHands Demo Scene: Open the AutoHands Demo Scene located under: AutoHand/Examples/Scenes/XR/Demo. Familiarize yourself with the existing actions and triggers. Identify any pre-built mechanisms that can be adapted into custom actions or triggers for your project.
  2. Create a Custom Mechanic Script (if needed): If you cannot find an existing mechanism or script to control it, create a custom mechanic script.
    • Use meaningful, descriptive names for variables and methods.
    • Expose variables in the Inspector using [SerializeField] for debugging and tweaking.
    • Declare public events using UnityEvent , methods or custom event classes to expose functionalities to other scripts.
    • Implement error handling using try-catch blocks to prevent unexpected behavior or crashes. Example:
using UnityEngine;
using UnityEngine.Events;

public class CustomMechanic : MonoBehaviour{
    [SerializeField] private float _threshold = 0.5f;
    public UnityEvent OnThresholdReached;

    private void Update()
    {
        if (CheckThreshold())
        {
            OnThresholdReached?.Invoke();
        }
    }

    private bool CheckThreshold()
    {
        // Custom logic to check threshold
        return true;
    }
}

3. Define Constants for Custom Actions/Triggers

  1. Create a new script named CustomConstants.cs in your custom folder.
  2. Define constants for your custom actions and triggers. These will act as identifiers in your JSON story files. Example:
namespace VRseBuilder.Core.NoCode.CustomActions
{
    public static class CustomConstants
    {
        public static readonly string CUSTOM_ACTION_NAME = "CustomActionIdentifier";
        public static readonly string CUSTOM_TRIGGER_NAME = "CustomTriggerIdentifier";
    }
}

4. Create Custom Actions/Triggers

  1. Create new scripts for each custom action or trigger in your custom folder.
  2. Ensure these scripts inherit from the appropriate base classes:
    • Custom Actions: Inherit from PlayableAction.
    • Custom Triggers: Inherit from PlayableTrigger.

Key Practices and Guidelines for Creating Custom Actions/Triggers

Override Deserialize(Node node)

  • This method initializes the trigger based on data from a Node object during runtime.
  • Example:
public override void Deserialize(Node node)
{
    base.Deserialize(node);
    if (node.TargetGameObject == null)
        _wrenchRotator = ReferenceManager.Instance.QueryByType<WrenchRotator>();
    else
        _wrenchRotator = node.TargetGameObject.GetComponent<WrenchRotator>();
}

OnBegin()

  • This method is called when the trigger is activated. It subscribes to events from the mechanic script to identify states and take appropriate actions.
  • Example:
public override void OnBegin()
{
    if (_wrenchRotator != null)
    {
        _wrenchRotator.onAngleRequiredReached.AddListener(OnCompleteAction);
    }
    else
    {
        Debug.Log(_wrenchRotator + "not found");
    }
    OnBeginEvent?.Invoke(this);
}

Listener Methods

  • These methods are invoked for the events listened to from the mechanic script.
  • Example:
private void OnCompleteAction()
{
    if (_wrenchRotator)
    {
        _wrenchRotator.onAngleRequiredReached.RemoveListener(OnCompleteAction);
        Debug.Log("Removed the Wrench Rotator Listeners");
    }
    OnEnd();
}

OnEnd()

  • This method is called when the trigger completes its action. It invokes the OnEndEvent.
  • Example:
public override void OnEnd()
{
    Debug.Log("OnAngleRequiredReached Events called!");
    OnEndEvent?.Invoke(this);
}

DoesRequireQuery()

  • This method returns true or false to indicate whether the trigger requires a query to find the target.
  • Example:
public override bool DoesRequireQuery() => true;

IsQueryValid()

  • This method checks if the query is valid by ensuring the target game object exists and has all dependencies.
  • Example:
public override bool IsQueryValid()
{
    return node.TargetGameObject != null && node.TargetGameObject.GetComponent<WrenchRotator>() != null;
}

5. Modify the JSON Helper

  1. Open the JsonHelper.cs script.
  2. Add logic to handle your custom actions and triggers based on the node data from JSON files.
  3. Use the constants defined in CustomConstants to match the action or trigger types. Example:
if (node.Type == CustomConstants.CUSTOM_ACTION_NAME)
{
    // Handle custom action logic
}

6. Update JSON Structure

  1. Ensure your JSON story files include your custom actions and triggers using the constants defined in CustomConstants. Example JSON Node:
{
    "Name": "CustomAction",
    "Type": "CustomActionIdentifier",
    "Data": {
        "key": "value"
    }
}

7. Add Documentation and Namespaces

  1. Add comprehensive XML documentation comments to your custom scripts for clarity.
  2. Use appropriate namespaces to avoid conflicts and ensure organization. Example:
namespace VRseBuilder.Core.NoCode.CustomActions
{
    // Your custom action/trigger code
}

8. Test and Validate

  1. Test your custom actions and triggers in the Unity Editor and runtime environments.
  2. Validate JSON parsing and ensure the custom logic executes correctly during gameplay.
  3. Check the logs for any exceptions or errors and troubleshoot as needed.

9. Save and Use in Your Story

  1. Save your story after adding custom actions and triggers.
  2. Play the scene in VR to see your custom logic in action.


title: “Customize” description: “Customize VRse Builder as per your requirements.”