Preview
Asset is in development, documentation is a draft
4.Scripting
Whenever using the API, ensure you add using sc.splines.mesher.runtime; to include the namespace.
Manual rebuilding
Both the Spline Curve Mesher and Spline Fill Mesher components have a Rebuild() function, which will trigger a regeneration of the entire mesh. An option splineIndex argument can be passed on to rebuild for a specific spline.
When changing settings in the inspector rebuilding happens automatically, but when changing them through script its required to call Rebuild() after you’ve finished making changes.
Mesh Generation 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.
- (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.splines.mesher.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.onPostRebuildMesh += 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.onPostRebuildMesh -= OnPostRebuild;
}
}
There’s also the onPreRebuildMesh callback, that executes before the mesh is being rebuild.
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.
A Spline Mesh Segment represents an individual mesh objects created for a spline, with a Mesh Collider attached.
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;
}
}
Editor tools
The SplineMesherEditor class contains a few public static functions, for use in custom tooling and pipelines:
- GenerateLODS (with overloads for specific scenes and meshers)
- GenerateLightmapUVs (with an overload for a list of meshers)
