Skip to content

Instantly share code, notes, and snippets.

@ArieLeo
Created July 12, 2023 15:00
Show Gist options
  • Save ArieLeo/cba8f3e15e2f5c053155342bd8e90f2e to your computer and use it in GitHub Desktop.
Save ArieLeo/cba8f3e15e2f5c053155342bd8e90f2e to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class getBonesPath : MonoBehaviour {
public Transform obj;
public AnimationClip _clip;
public bool isOn;
public Transform[] allChildren;
public bool[] use;
Transform bone;
Transform bonex;
Transform bxhierarch;
string[] unpath;//negative path
public int maxdepth = 20;
public int i;
int x;
public string[] path;
public KeyCode getPaths = KeyCode.P;
public KeyCode getCurves = KeyCode.Space;
int k, kx;
bool getpathx;
bool getcurvesx;
[System.Serializable]
public class CRV
{
public AnimationCurve[] curve;
}
public CRV[] boneCurves;
public bool doDebug;
bool cdefined;
void Start () {
if (isOn) {
allChildren = obj.GetComponentsInChildren<Transform> ();
use = new bool[allChildren.Length];
for (i = 0; i < allChildren.Length; i++) use [i] = true;
path = new string[allChildren.Length];
boneCurves = new CRV[allChildren.Length];
}
}
void Update(){
if (!isOn) return;
if (!cdefined && Time.time > 0.5f) {
cdefined = true;
boneCurves [0].curve = new AnimationCurve[7];
for (i = 1; i < allChildren.Length; i++) boneCurves [i].curve = new AnimationCurve[4];
}
if (Input.GetKeyDown (getPaths)) getpathx = true;
if (Input.GetKeyDown (getCurves)) getcurvesx = true;
if (getpathx) {
getpathx = false;
//get all path for used
for(k=0; k<allChildren.Length; k++){
if (use [k]) bone = allChildren [k];
bonex = SearchHierarchyForBone(obj, bone.name);
for (i = 0; i < maxdepth; i++) {
if (i == 0) bxhierarch = bonex;
if (bxhierarch.parent != null) {
if (i == 1) bxhierarch = bonex.parent;
if (i > 1) bxhierarch = bxhierarch.parent;
if (bxhierarch == obj) x = i;
}
}
unpath = new string[x];
for (i = 0; i < x; i++) {
if (i == 0) bxhierarch = bonex;
if (bxhierarch.parent != null) {
if (i == 1) bxhierarch = bonex.parent;
if (i > 1) bxhierarch = bxhierarch.parent;
}
unpath [i] = bxhierarch.name;
}
path[k] += obj.name;
for (i = unpath.Length-1; i > -1; i--) path[k] += "/" + unpath [i];
}
}
if (getcurvesx) {
getcurvesx = false;
i = 0;
var curveBindings = UnityEditor.AnimationUtility.GetCurveBindings (_clip);
for (x = 0; x < path.Length; x++) {
foreach (var curveBinding in curveBindings) {
if (doDebug) Debug.Log (curveBinding.path + "," + curveBinding.propertyName);
if(curveBinding.propertyName.Contains ("RootT.x")) boneCurves [0].curve[0] = UnityEditor.AnimationUtility.GetEditorCurve (_clip, curveBinding);
if(curveBinding.propertyName.Contains ("RootT.y")) boneCurves [0].curve[1] = UnityEditor.AnimationUtility.GetEditorCurve (_clip, curveBinding);
if(curveBinding.propertyName.Contains ("RootT.z")) boneCurves [0].curve[2] = UnityEditor.AnimationUtility.GetEditorCurve (_clip, curveBinding);
if(curveBinding.propertyName.Contains ("RootQ.x")) boneCurves [0].curve[3] = UnityEditor.AnimationUtility.GetEditorCurve (_clip, curveBinding);
if(curveBinding.propertyName.Contains ("RootQ.y")) boneCurves [0].curve[4] = UnityEditor.AnimationUtility.GetEditorCurve (_clip, curveBinding);
if(curveBinding.propertyName.Contains ("RootQ.z")) boneCurves [0].curve[5] = UnityEditor.AnimationUtility.GetEditorCurve (_clip, curveBinding);
if(curveBinding.propertyName.Contains ("RootQ.w")) boneCurves [0].curve[6] = UnityEditor.AnimationUtility.GetEditorCurve (_clip, curveBinding);
if (curveBinding.path == path [x] && !curveBinding.propertyName.Contains ("LocalScale")) {
boneCurves [x].curve[i] = UnityEditor.AnimationUtility.GetEditorCurve (_clip, curveBinding);
i++;
//if (x == 0 && i == 7) i = 0;
//if (x != 0 && i == 4) i = 0;
if (i == 4) i = 0;
}
}
}
}
}
public Transform SearchHierarchyForBone(Transform current, string name)
{
// check if the current bone is the bone we're looking for, if so return it
if (current.name.Contains(name)) return current;
// search through child bones for the bone we're looking for
for (int i = 0; i < current.GetChildCount(); ++i)
{
// the recursive step; repeat the search one step deeper in the hierarchy
Transform found = SearchHierarchyForBone(current.GetChild(i), name);
// a transform was returned by the search above that is not null,
// it must be the bone we're looking for
if (found != null) return found;
}
// bone with name was not found
return null;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class playBonesCurves : MonoBehaviour {
public getBonesPath getBonespath;
public Transform root;
public bool justRoot;
public Vector3 rootOffset;
[System.Serializable]
public class Play
{
public KeyCode start = KeyCode.A;
public KeyCode stop = KeyCode.S;
public KeyCode cont = KeyCode.D;
public bool onStart;
}
public Play play = new Play();
public bool syncClip = true;
[System.Serializable]
public class Sync
{
public Animator animator;
public string state;
}
public Sync sync = new Sync();
int i,x;
public float timescale = 0.5f;
public float time;
public bool timecount;
float starttime;
[System.Serializable]
public class Display
{
public Text time;
}
public Display display = new Display();
void Start () {
if (play.onStart) {
timecount = true;
starttime = Time.time;
}
}
void Update () {
if (Input.GetKeyDown (play.start)) {
timecount = true;
starttime = Time.time;
Time.timeScale = timescale;
if (syncClip) {
sync.animator.speed = timescale;
sync.animator.CrossFadeInFixedTime (sync.state, 0);
}
}
if (Input.GetKeyDown (play.stop)) {
timecount = false;
Time.timeScale = 0;
}
if (Input.GetKeyDown (play.cont)) {
timecount = true;
Time.timeScale = timescale;
}
if (timecount) time = Time.time - starttime;
if (display.time) display.time.text = "" + time;
//if (playIn == a.update) {
//}
}
void LateUpdate(){
if (timecount) {
for (i = 0; i < getBonespath.allChildren.Length; i++) {
if (i == 0) {
root.position = new Vector3 (
getBonespath.boneCurves [0].curve [0].Evaluate (time),
getBonespath.boneCurves [0].curve [1].Evaluate (time),
getBonespath.boneCurves [0].curve [2].Evaluate (time)
);
root.localRotation = new Quaternion (
getBonespath.boneCurves [0].curve [3].Evaluate (time),
getBonespath.boneCurves [0].curve [4].Evaluate (time),
getBonespath.boneCurves [0].curve [5].Evaluate (time),
getBonespath.boneCurves [0].curve [6].Evaluate (time)
);
}
if (i != 0 && !justRoot) {
getBonespath.allChildren [i].localRotation = new Quaternion (
getBonespath.boneCurves [i].curve [0].Evaluate (time),
getBonespath.boneCurves [i].curve [1].Evaluate (time),
getBonespath.boneCurves [i].curve [2].Evaluate (time),
getBonespath.boneCurves [i].curve [3].Evaluate (time)
);
}
}
}
root.rotation = Quaternion.Euler (rootOffset);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment