89 lines
1.8 KiB
C#
89 lines
1.8 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace BoneTool.Script.Runtime
|
|
{
|
|
[ExecuteInEditMode]
|
|
public class BoneVisualiser : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
private struct BoneTransform
|
|
{
|
|
public Transform Target;
|
|
|
|
public Vector3 LocalPosition;
|
|
|
|
public BoneTransform(Transform target, Vector3 localPosition)
|
|
{
|
|
Target = target;
|
|
LocalPosition = localPosition;
|
|
}
|
|
|
|
public void SetLocalPosition(Vector3 position)
|
|
{
|
|
LocalPosition = position;
|
|
}
|
|
}
|
|
|
|
public Transform RootNode;
|
|
|
|
public float BoneGizmosSize = 0.01f;
|
|
|
|
public Color BoneColor = Color.white;
|
|
|
|
public bool HideRoot;
|
|
|
|
public bool EnableConstraint = true;
|
|
|
|
private Transform _preRootNode;
|
|
|
|
private Transform[] _childNodes;
|
|
|
|
private BoneTransform[] _previousTransforms;
|
|
|
|
public Transform[] GetChildNodes()
|
|
{
|
|
return _childNodes;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!EnableConstraint || _previousTransforms == null)
|
|
{
|
|
return;
|
|
}
|
|
BoneTransform[] previousTransforms = _previousTransforms;
|
|
for (int i = 0; i < previousTransforms.Length; i++)
|
|
{
|
|
BoneTransform boneTransform = previousTransforms[i];
|
|
if ((bool)boneTransform.Target && boneTransform.Target.hasChanged)
|
|
{
|
|
if (boneTransform.Target.parent.childCount == 1)
|
|
{
|
|
boneTransform.Target.localPosition = boneTransform.LocalPosition;
|
|
}
|
|
else
|
|
{
|
|
boneTransform.SetLocalPosition(boneTransform.Target.localPosition);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void PopulateChildren()
|
|
{
|
|
if ((bool)RootNode)
|
|
{
|
|
_preRootNode = RootNode;
|
|
_childNodes = RootNode.GetComponentsInChildren<Transform>();
|
|
_previousTransforms = new BoneTransform[_childNodes.Length];
|
|
for (int i = 0; i < _childNodes.Length; i++)
|
|
{
|
|
Transform transform = _childNodes[i];
|
|
_previousTransforms[i] = new BoneTransform(transform, transform.localPosition);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|