浏览代码

Moved TwoBoneIKAutoSetup to its own script

/main
Dave Hunt 5 年前
当前提交
580babf1
共有 4 个文件被更改,包括 180 次插入139 次删除
  1. 72
      Assets/Editor/HelperMenu.cs
  2. 150
      Assets/Scenes/SampleSceneNinja2.unity
  3. 86
      Assets/Editor/TwoBoneIKAutoSetup.cs
  4. 11
      Assets/Editor/TwoBoneIKAutoSetup.cs.meta

72
Assets/Editor/HelperMenu.cs


return reflectionMat;
}
[MenuItem("Animation Rigging/Utilities/Auto-Setup TwoBoneIK from Tip Transform", false, 0)]
public static void TwoBoneIKAutoSetup2(MenuCommand command)
{
var selection = Selection.activeObject as GameObject;
if (!selection)
{
Debug.LogWarning("Please select a TwoBoneIK before running auto setup.");
return;
}
var constraint = selection.GetComponent<TwoBoneIKConstraint>() ;// command.context as UnityEngine.Animations.Rigging.TwoBoneIKConstraint;
var tip = constraint.data.tip ;// constraint.data.tip;
var animator = constraint.GetComponentInParent<Animator>()?.transform;
if (!tip)
{
Debug.LogWarning("Please provide a tip before running auto setup.");
return;
}
if (!constraint.data.mid)
{
Undo.RecordObject(constraint, "Setup mid bone for TwoBoneIK");
constraint.data.mid = tip.parent;
}
if (!constraint.data.root)
{
Undo.RecordObject(constraint, "Setup root bone for TwoBoneIK");
constraint.data.root = tip.parent.parent;
}
if (!constraint.data.target)
{
var target = constraint.transform.Find(constraint.gameObject.name + "_target");
if (target == null)
{
var t = new GameObject();
Undo.RegisterCreatedObjectUndo(t, "Created target");
t.name = constraint.gameObject.name + "_target";
t.transform.localScale = .1f * t.transform.localScale;
Undo.SetTransformParent(t.transform, constraint.transform, "Set new parent");
target = t.transform;
}
constraint.data.target = target;
}
if (!constraint.data.hint)
{
var hint = constraint.transform.Find(constraint.gameObject.name + "_hint");
if (hint == null)
{
var t = new GameObject();
Undo.RegisterCreatedObjectUndo(t, "Created hint");
t.name = constraint.gameObject.name + "_hint";
t.transform.localScale = .1f * t.transform.localScale;
Undo.SetTransformParent(t.transform, constraint.transform, "Set new parent");
hint = t.transform;
}
constraint.data.hint = hint;
}
// align target and hint to bones
constraint.data.target.position = constraint.data.tip.position;
constraint.data.target.rotation = constraint.data.tip.rotation;
constraint.data.hint.position = constraint.data.mid.position;
constraint.data.hint.rotation = constraint.data.mid.rotation;
}
}

150
Assets/Scenes/SampleSceneNinja2.unity


type: 3}
m_PrefabInstance: {fileID: 824842124}
m_PrefabAsset: {fileID: 0}
--- !u!1 &42986163
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 42986164}
m_Layer: 0
m_Name: LeftArmIK_hint
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &42986164
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 42986163}
m_LocalRotation: {x: -0.25018853, y: 0.50019187, z: 0.36122316, w: 0.7461446}
m_LocalPosition: {x: -0.443, y: 1.084, z: -0.101}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1141099735}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &52723285
GameObject:
m_ObjectHideFlags: 0

type: 3}
m_PrefabInstance: {fileID: 824842124}
m_PrefabAsset: {fileID: 0}
--- !u!1 &500310103
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 500310104}
m_Layer: 0
m_Name: LeftArmIK_hint
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &500310104
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 500310103}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: -0.5176035, y: 0.9860894, z: -0.19885659}
m_LocalScale: {x: 0.1, y: 0.1, z: 0.1}
m_Children: []
m_Father: {fileID: 1141099735}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!4 &626918372 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 2163959474932033770, guid: 665552469e0a73c49bc77e824e69137d,

position: {x: 0, y: 0, z: 0}
rotation: {x: 0, y: 0, z: 0}
m_Visible: 1
- m_Transform: {fileID: 1481984174}
- m_Transform: {fileID: 0}
m_Style:
shape: {fileID: 4300000, guid: 687b70eb9c49243639f9379f6965034f, type: 2}
color: {r: 0, g: 1, b: 0, a: 0.5}

m_Visible: 1
- m_Transform: {fileID: 500310104}
- m_Transform: {fileID: 0}
m_Style:
shape: {fileID: 4300000, guid: e050c2b16fe384bd994474655a4b4968, type: 2}
color: {r: 0, g: 1, b: 0, a: 0.5}

position: {x: -0.1, y: 0.06, z: 0}
rotation: {x: 0, y: 0, z: 0}
m_Visible: 1
- m_Transform: {fileID: 1113683084}
m_Style:
shape: {fileID: 4300000, guid: 687b70eb9c49243639f9379f6965034f, type: 2}
color: {r: 0, g: 1, b: 0, a: 0.5}
size: 0.05
position: {x: 0, y: 0, z: 0}
rotation: {x: 0, y: 0, z: 0}
m_Visible: 1
- m_Transform: {fileID: 42986164}
m_Style:
shape: {fileID: 4300000, guid: e050c2b16fe384bd994474655a4b4968, type: 2}
color: {r: 0, g: 1, b: 0, a: 0.5}
size: 0.05
position: {x: 0, y: 0, z: 0}
rotation: {x: 0, y: 0, z: 0}
m_Visible: 1
--- !u!4 &772315690
Transform:
m_ObjectHideFlags: 0

type: 3}
m_PrefabInstance: {fileID: 824842124}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1113683083
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1113683084}
m_Layer: 0
m_Name: LeftArmIK_target
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1113683084
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1113683083}
m_LocalRotation: {x: -0.24978378, y: 0.4993559, z: 0.36150315, w: 0.74670434}
m_LocalPosition: {x: -0.40185997, y: 1.1453999, z: 0.33601412}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1141099735}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1141099734
GameObject:
m_ObjectHideFlags: 0

m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 1481984174}
- {fileID: 500310104}
- {fileID: 1113683084}
- {fileID: 42986164}
m_Father: {fileID: 772315690}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

m_Root: {fileID: 632459716}
m_Mid: {fileID: 1709524214}
m_Tip: {fileID: 16853550}
m_Target: {fileID: 1481984174}
m_Hint: {fileID: 500310104}
m_Target: {fileID: 1113683084}
m_Hint: {fileID: 42986164}
m_SourceObjectsGUIToggle: 0
m_SourceObjectsGUIToggle: 1
m_SettingsGUIToggle: 0
--- !u!4 &1165449195 stripped
Transform:

type: 3}
m_PrefabInstance: {fileID: 3151587887460958956}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1481984173
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1481984174}
m_Layer: 0
m_Name: LeftArmIK_target
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1481984174
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1481984173}
m_LocalRotation: {x: -0.24978378, y: 0.4993559, z: 0.36150315, w: 0.74670434}
m_LocalPosition: {x: -0.40185997, y: 1.1453999, z: 0.33601412}
m_LocalScale: {x: 0.1, y: 0.1, z: 0.1}
m_Children: []
m_Father: {fileID: 1141099735}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!4 &1499208248 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 3151587886006327508, guid: 8799b0e9b65ae384cb6078d2b25dfc11,

86
Assets/Editor/TwoBoneIKAutoSetup.cs


using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Animations.Rigging;
public class TwoBoneIKAutoSetup : Editor
{
[MenuItem("Animation Rigging/Utilities/Auto-Setup TwoBoneIK from Tip Transform", false, 0)]
public static void TwoBoneIKAutoSetupSelected(MenuCommand command)
{
var selection = Selection.activeObject as GameObject;
if (!selection)
{
Debug.LogWarning("Please select a GameObject with TwoBoneIKConstraint before running auto setup.");
return;
}
var constraint = selection.GetComponent<TwoBoneIKConstraint>();
if (!constraint)
{
Debug.LogWarning("Please select a GameObject with TwoBoneIKConstraint before running auto setup.");
return;
}
if (!constraint.data.tip)
{
Debug.LogWarning("Please provide a tip transform for the TwoBoneIKConstraint before running auto setup.");
return;
}
TwoBoneIKAutoSetupUtility(constraint);
}
public static void TwoBoneIKAutoSetupUtility(TwoBoneIKConstraint constraint)
{
var tip = constraint.data.tip;
if (!constraint.data.mid)
{
Undo.RecordObject(constraint, "Setup mid bone for TwoBoneIK");
constraint.data.mid = tip.parent;
}
if (!constraint.data.root)
{
Undo.RecordObject(constraint, "Setup root bone for TwoBoneIK");
constraint.data.root = tip.parent.parent;
}
if (!constraint.data.target)
{
var target = constraint.transform.Find(constraint.gameObject.name + "_target");
if (target == null)
{
var t = new GameObject();
Undo.RegisterCreatedObjectUndo(t, "Created target");
t.name = constraint.gameObject.name + "_target";
Undo.SetTransformParent(t.transform, constraint.transform, "Set new parent");
target = t.transform;
}
constraint.data.target = target;
}
if (!constraint.data.hint)
{
var hint = constraint.transform.Find(constraint.gameObject.name + "_hint");
if (hint == null)
{
var t = new GameObject();
Undo.RegisterCreatedObjectUndo(t, "Created hint");
t.name = constraint.gameObject.name + "_hint";
Undo.SetTransformParent(t.transform, constraint.transform, "Set new parent");
hint = t.transform;
}
constraint.data.hint = hint;
}
// align target and hint to bones
constraint.data.target.position = constraint.data.tip.position;
constraint.data.target.rotation = constraint.data.tip.rotation;
constraint.data.hint.position = constraint.data.mid.position;
constraint.data.hint.rotation = constraint.data.mid.rotation;
}
}

11
Assets/Editor/TwoBoneIKAutoSetup.cs.meta


fileFormatVersion: 2
guid: bb9a4c3794fa6044891294faac0c2361
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存