JOYLINK STUDIO

DOCUMENTATION / 04

Crowd API

Share one resource while controlling instances independently.

Create instances

Create one GpuAnimationCrowd per baked resource. Instances share mesh, material and bone texture without a GameObject per character. Calls run on Unity’s main thread. Instance matrices are world-space; moving the manager GameObject does not move its instances.

using JoyLinkStudio.SpineGpuAnimation;
using UnityEngine;

public sealed class BakedCrowdExample : MonoBehaviour
{
    [SerializeField] private GpuAnimationResource bakedResource;
    private GpuAnimationCrowd crowd;

    private void Start()
    {
        if (bakedResource == null) return;
        crowd = gameObject.AddComponent<GpuAnimationCrowd>();
        crowd.Resource = bakedResource;
        var first = crowd.AddInstance(Matrix4x4.identity, 0);
        var second = crowd.AddInstance(
            Matrix4x4.Translate(new Vector3(2, 0, 0)), 0);
        crowd.Seek(second, 0.35f);
        crowd.SetSpeed(second, 0.8f);
        crowd.SetPaused(first, true);
    }

    private void OnDestroy()
    {
        if (crowd != null) crowd.Clear();
    }
}

Control an instance

APIPurpose
AddInstance(matrix, clip)Return a handle for the new instance.
SetAnimation(handle, index)Select a clip and reset time.
Seek / SetSpeed / SetPaused / SetLoopIndependent playback control, with the handle first.
SetTransform(handle, matrix)Set a world-space transform.
GetState(handle)Read a value snapshot.
Contains / RemoveInstanceReturn false for invalid handles.
Clear()Remove all instances and invalidate handles.

Handles and lifetime

A GpuCrowdHandle belongs to exactly one Crowd. Default, removed and cleared handles are invalid. Other playback operations reject invalid handles with ArgumentException. Removing an instance may reorder internal storage but leaves other handles stable. Assigning a different Resource clears all instances; assigning the same resource preserves them.

AutoAdvance defaults to true. Set false before driving Advance(deltaTime) yourself. Disabling stops automatic advance and rendering while retaining state. Clear releases internal buffers and keeps the resource reference.

Plan for transparency

The bundled instancing-enabled Unlit material and GPU instancing support are required. Draws are submitted in batches of at most 1023 instances. Unity sorts each transparent batch as a whole; arbitrary per-character overlap and interleaving with other transparent renderers are not guaranteed. Start with separated layouts and inspect real results.

VisibleCamera can restrict rendering; null allows eligible cameras. Camera culling masks use the Crowd GameObject’s Layer. Camera callbacks submit drawing and do not advance animation time.