Spline Mesher - Pro
4.2.Callbacks
Both the Spline Curve Mesher and Spline Fill Mesher components and classes expose two triggers, designed to facilitate custom functionality. For instance, you may want to execute certain functionality whenever the spline mesh is rebuilt.
For example:
- Updating a navmesh after a fence has been altered.
- Convert GameObjects to Entities.
- (Re)spawning objects along the same spline
- Updating game logic data that depends on the spline
- Adding components to the created mesh segments (eg. making meshes breakable)
All in all, this is an excellent way to integrate the tool with custom workflows.
Unlike the Events exposed in the inspector callbacks are entirely code-based, and can be subscribed to. A bare bones example looks like this:
using System;
using UnityEngine;
using sc.splinemesher.pro.runtime;
[ExecuteAlways] //Ensure the OnEnable and OnDisable functions fire outside of play mode
public class RebuildEventExample : MonoBehaviour
{
public SplineMesher splineMesher;
private void OnEnable()
{
//Subscribe
SplineMesher.onPostRebuildMesher += OnPostRebuild;
}
private void OnPostRebuild(SplineMesher instance)
{
//Is the instance being rebuild the one we want to work with
if (instance == splineMesher)
{
//Execute something
}
}
private void OnDisable()
{
//Unsubscribe
SplineMesher.onPostRebuildMesher -= OnPostRebuild;
}
}
There’s also the onPreRebuildMesher callback, that executes before the mesh is being rebuild.
Rebuild() function from these callbacks! This creates infinite recursion and can crash the application.Collision/Trigger Callbacks
The SplineMeshSegment class holds generic collision and trigger enter/stay/exit events that can be subscribed to. The possibilities are endless so two generic examples are demonstrated here.
Segments are always part of a SplineMeshContainer.
using sc.splinemesher.pro.runtime;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Splines;
public class CollisionCallbacksExample : MonoBehaviour
{
void Start()
{
SplineMeshSegment.onCollisionEnter += OnCollisionEnterSpline;
//Similarly, there's a "onCollisionExit" event
SplineMeshSegment.onTriggerStay += OnTriggerStaySpline;
//There's also "onTriggerEnter and onTriggerExit" events"
}
private void OnCollisionEnterSpline(SplineMeshSegment splineMeshSegment, Collision other)
{
//Something collided with this spline mesh segment
//You can perform normal checks here (eg layer mask, components, etc...) to see if the event is relevant to you
if(other.gameObject.CompareTag("Player") == false) return;
//You can traverse upwards to get the related Spline Mesher component, and thus also the spline this segment was made from
SplineMesher mesher = splineMeshSegment.Container.Owner;
SplineContainer splineContainer = mesher.SplineContainer;
int splineIndex = splineMeshSegment.Container.SplineIndex;
Spline spline = splineContainer.Splines[splineIndex];
}
private void OnTriggerStaySpline(SplineMeshSegment splineMeshSegment, Collider other)
{
//Collider is inside of the spline mesh segment's trigger volume.
//Reminder: Convex mesh colliders cannot be made triggers
//Example: A player is inside a wind tube mesh, push it forward along the spline
SplineMesher mesher = splineMeshSegment.Container.Owner;
SplineContainer splineContainer = mesher.SplineContainer;
int splineIndex = splineMeshSegment.Container.SplineIndex;
Spline spline = splineContainer.Splines[splineIndex];
//Sample the forward direction of the spline nearest the collider
SplineUtility.GetNearestPoint(spline, other.transform.position, out float3 nearestPoint, out float t);
float3 forward = math.normalize(spline.EvaluateTangent(t));
Rigidbody rb = other.GetComponent();
rb.AddForce(forward * 2f, ForceMode.VelocityChange);
}
void OnDestroy()
{
SplineMeshSegment.onCollisionEnter -= OnCollisionEnterSpline;
SplineMeshSegment.onTriggerStay -= OnTriggerStaySpline;
}
}
