Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created October 25, 2018 14:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/c47e9affc0cc1959dc355fa949ea24ac to your computer and use it in GitHub Desktop.
Save tsubaki/c47e9affc0cc1959dc355fa949ea24ac to your computer and use it in GitHub Desktop.
Animation C# Jobs 小さなサンプル
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Experimental.Animations;
using Unity.Collections;
public class Sample : MonoBehaviour
{
private NativeArray<TransformStreamHandle> joints;
private NativeArray<TransformSceneHandle> effectors;
private PlayableGraph graph;
void Awake()
{
var animator = GetComponent<Animator>();
graph = PlayableGraph.Create(name);
joints = new NativeArray<TransformStreamHandle>(4, Allocator.Persistent)
{
[0] = animator.BindStreamTransform(transform.Find("Bone1")),
[1] = animator.BindStreamTransform(transform.Find("Bone1/Bone2")),
[2] = animator.BindStreamTransform(transform.Find("Bone1/Bone2/Bone3")),
[3] = animator.BindStreamTransform(transform.Find("Bone1/Bone2/Bone3/Bone4"))
};
AnimatorUtility.OptimizeTransformHierarchy(gameObject, null);
effectors = new NativeArray<TransformSceneHandle>(4, Allocator.Persistent)
{
[0] = animator.BindSceneTransform(new GameObject().transform),
[1] = animator.BindSceneTransform(new GameObject().transform),
[2] = animator.BindSceneTransform(new GameObject().transform),
[3] = animator.BindSceneTransform(new GameObject().transform),
};
var playable = AnimationScriptPlayable.Create(graph, new SampleJob()
{
effectors = effectors,
joints = joints,
});
AnimationPlayableUtilities.Play(animator, playable, graph);
}
void OnDestroy()
{
AnimatorUtility.DeoptimizeTransformHierarchy(gameObject);
graph.Destroy();
effectors.Dispose();
joints.Dispose();
}
}
using UnityEngine.Experimental.Animations;
using Unity.Collections;
public struct SampleJob : IAnimationJob
{
public NativeArray<TransformStreamHandle> joints;
public NativeArray<TransformSceneHandle> effectors;
public void ProcessAnimation(AnimationStream stream)
{
for (int i=0; i<joints.Length; i++)
{
// effectorから取得してストリームに書き込む
var rotation = effectors[i].GetLocalRotation(stream);
joints[i].SetLocalRotation(stream, rotation);
}
}
public void ProcessRootMotion(AnimationStream stream){}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment