DOCUMENTATION / 04
Crowd APICrowd 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.每套烘焙资源使用一个 GpuAnimationCrowd。实例共享 mesh、material 和骨骼纹理,无需每个角色一个 GameObject。调用须在 Unity 主线程;矩阵采用世界空间,移动管理器 GameObject 不会移动其实例。
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控制实例
| API | Purpose用途 |
|---|---|
AddInstance(matrix, clip) | Return a handle for the new instance.返回新实例的句柄。 |
SetAnimation(handle, index) | Select a clip and reset time.切换动画并重置时间。 |
Seek / SetSpeed / SetPaused / SetLoop | Independent playback control, with the handle first.首参数为句柄,独立控制时间、速度、暂停和循环。 |
SetTransform(handle, matrix) | Set a world-space transform.设置世界空间矩阵。 |
GetState(handle) | Read a value snapshot.读取状态的值快照。 |
Contains / RemoveInstance | Return false for invalid handles.无效句柄返回 false。 |
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.GpuCrowdHandle 只属于创建它的 Crowd;默认、已移除和已清空的句柄无效。其他播放操作会用 ArgumentException 拒绝无效句柄。删除可重排内部存储,但其他句柄保持稳定。绑定不同 Resource 会清空实例;绑定同一个引用则保留。
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.AutoAdvance 默认为 true;手动调用 Advance(deltaTime) 前先设为 false。禁用组件会停止自动推进和渲染但保留状态;Clear 释放内部缓冲并保留资源引用。
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.需要 GPU instancing 支持和包内启用实例化的 Unlit 材质,每批最多1023实例。Unity把整个透明批次作为一个对象排序,不保证任意角色重叠或与其他透明物体交错的正确顺序。请从分离布局开始,并检查实际效果。
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.VisibleCamera 可限制相机,null 表示符合条件的相机。相机 culling mask 使用 Crowd GameObject 的 Layer;相机回调只提交绘制,不推进动画时间。