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
-
In your Unity project, create a new folder under
Assetsto store your custom scripts. Example:Assets/CustomActionsAndTriggers. - This folder will house all your custom action and trigger scripts.
2. Identify or Create Mechanic Script
-
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. -
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-catchblocks to prevent unexpected behavior or crashes. Example:
3. Define Constants for Custom Actions/Triggers
-
Create a new script named
CustomConstants.csin your custom folder. - Define constants for your custom actions and triggers. These will act as identifiers in your JSON story files. Example:
4. Create Custom Actions/Triggers
- Create new scripts for each custom action or trigger in your custom folder.
-
Ensure these scripts inherit from the appropriate base classes:
-
Custom Actions: Inherit from
PlayableAction. -
Custom Triggers: Inherit from
PlayableTrigger.
-
Custom Actions: Inherit from
Key Practices and Guidelines for Creating Custom Actions/Triggers
Override Deserialize(Node node)
-
This method initializes the trigger based on data from a
Nodeobject during runtime. - Example:
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:
Listener Methods
- These methods are invoked for the events listened to from the mechanic script.
- Example:
OnEnd()
-
This method is called when the trigger completes its action. It invokes the
OnEndEvent. - Example:
DoesRequireQuery()
-
This method returns
trueorfalseto indicate whether the trigger requires a query to find the target. - Example:
IsQueryValid()
- This method checks if the query is valid by ensuring the target game object exists and has all dependencies.
- Example:
5. Modify the JSON Helper
-
Open the
JsonHelper.csscript. - Add logic to handle your custom actions and triggers based on the node data from JSON files.
-
Use the constants defined in
CustomConstantsto match the action or trigger types. Example:
6. Update JSON Structure
-
Ensure your JSON story files include your custom actions and triggers using the constants defined in
CustomConstants. Example JSON Node:
7. Add Documentation and Namespaces
- Add comprehensive XML documentation comments to your custom scripts for clarity.
- Use appropriate namespaces to avoid conflicts and ensure organization. Example:
8. Test and Validate
- Test your custom actions and triggers in the Unity Editor and runtime environments.
- Validate JSON parsing and ensure the custom logic executes correctly during gameplay.
- Check the logs for any exceptions or errors and troubleshoot as needed.
9. Save and Use in Your Story
- Save your story after adding custom actions and triggers.
- Play the scene in VR to see your custom logic in action.