90 lines
2.1 KiB
C#
90 lines
2.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using BITKit;
|
|
using Unity.Collections;
|
|
using Unity.Jobs;
|
|
using UnityEngine;
|
|
using UnityEngine.Jobs;
|
|
|
|
namespace BITFALL.Rig{
|
|
public class TickOverrideTranformService : MonoBehaviour
|
|
{
|
|
public struct SetTransformJob : IJobParallelForTransform
|
|
{
|
|
// Jobs declare all data that will be accessed in the job
|
|
// By declaring it as read only, multiple jobs are allowed to access the data in parallel
|
|
[Unity.Collections.ReadOnly]
|
|
public NativeArray<Vector3> positions;
|
|
[Unity.Collections.ReadOnly]
|
|
public NativeArray<Quaternion> rotations;
|
|
|
|
// The code actually running on the job
|
|
public void Execute(int index, TransformAccess transform)
|
|
{
|
|
transform.position = positions[index];
|
|
transform.rotation = rotations[index];
|
|
}
|
|
}
|
|
public static void Register(int id,TickOverrideTransform tickOverrideTransform)
|
|
{
|
|
Dictionary.Add(id,tickOverrideTransform);
|
|
IsDirty = true;
|
|
}
|
|
public static void UnRegister(int id)
|
|
{
|
|
Dictionary.Remove(id);
|
|
IsDirty = true;
|
|
}
|
|
private static readonly Dictionary<int, TickOverrideTransform> Dictionary = new();
|
|
private static bool IsDirty;
|
|
|
|
private static TickOverrideTransform[] Array;
|
|
|
|
[SerializeReference, SubclassSelector] private ITicker ticker;
|
|
|
|
private TransformAccessArray m_AccessArray;
|
|
private bool _isCompleted;
|
|
private void OnEnable()
|
|
{
|
|
ticker.Add(Tick);
|
|
}
|
|
private void OnDisable()
|
|
{
|
|
ticker.Remove(Tick);
|
|
}
|
|
private void Tick(float obj)
|
|
{
|
|
if (!_isCompleted) return;
|
|
if (IsDirty)
|
|
{
|
|
Array = Dictionary.Values.ToArray();
|
|
}
|
|
|
|
m_AccessArray = new TransformAccessArray(Array.Length);
|
|
|
|
var rotations = new NativeArray<Quaternion>(Array.Length, Allocator.TempJob);
|
|
var positions = new NativeArray<Vector3>(Array.Length, Allocator.TempJob);
|
|
|
|
var index = 0;
|
|
foreach (var x in Array)
|
|
{
|
|
positions[index] = x.Source.position;
|
|
rotations[index] = x.Source.rotation;
|
|
index++;
|
|
}
|
|
|
|
var job = new SetTransformJob()
|
|
{
|
|
positions = positions,
|
|
rotations = rotations
|
|
};
|
|
job.Schedule(m_AccessArray);
|
|
|
|
_isCompleted = true;
|
|
}
|
|
}
|
|
|
|
}
|