FullBodyBipedIKInspector.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Collections;
  4. namespace RootMotion.FinalIK {
  5. /*
  6. * Custom inspector for FullBodyBipedIK.
  7. * */
  8. [CustomEditor(typeof(FullBodyBipedIK))]
  9. public class FullBodyBipedIKInspector : IKInspector {
  10. private FullBodyBipedIK script { get { return target as FullBodyBipedIK; }}
  11. private int selectedEffector;
  12. private SerializedProperty references;
  13. private bool autodetected;
  14. private static Color color {
  15. get {
  16. return new Color(0f, 0.75f, 1f);
  17. }
  18. }
  19. protected override MonoBehaviour GetMonoBehaviour(out int executionOrder) {
  20. executionOrder = 9999;
  21. return script;
  22. }
  23. protected override void OnEnableVirtual() {
  24. references = serializedObject.FindProperty("references");
  25. // Autodetecting References
  26. if (script.references.IsEmpty(false) && script.enabled) {
  27. BipedReferences.AutoDetectReferences(ref script.references, script.transform, new BipedReferences.AutoDetectParams(true, false));
  28. script.solver.rootNode = IKSolverFullBodyBiped.DetectRootNodeBone(script.references);
  29. Initiate();
  30. if (Application.isPlaying) Warning.Log("Biped references were auto-detected on a FullBodyBipedIK component that was added in runtime. Note that this only happens in the Editor and if the GameObject is selected (for quick and convenient debugging). If you want to add FullBodyBipedIK dynamically in runtime via script, you will have to use BipedReferences.AutodetectReferences() for automatic biped detection.", script.transform);
  31. references.isExpanded = !script.references.isFilled;
  32. }
  33. }
  34. protected override void AddInspector() {
  35. // While in editor
  36. if (!Application.isPlaying) {
  37. // Editing References, if they have changed, reinitiate.
  38. if (BipedReferencesInspector.AddModifiedInspector(references)) {
  39. Initiate();
  40. return; // Don't draw immediatelly to avoid errors
  41. }
  42. // Root Node
  43. IKSolverFullBodyBipedInspector.AddReferences(true, solver);
  44. // Reinitiate if rootNode has changed
  45. if (serializedObject.ApplyModifiedProperties()) {
  46. Initiate();
  47. return; // Don't draw immediatelly to avoid errors
  48. }
  49. } else {
  50. // While in play mode
  51. // Draw the references and the root node for UMA
  52. BipedReferencesInspector.AddModifiedInspector(references);
  53. IKSolverFullBodyBipedInspector.AddReferences(true, solver);
  54. }
  55. string errorMessage = string.Empty;
  56. if (script.ReferencesError(ref errorMessage) || !script.solver.IsValid(ref errorMessage)) {
  57. AddWarningBox(errorMessage);
  58. Warning.Log(errorMessage, script.transform, false);
  59. } else {
  60. // Draw the inspector for IKSolverFullBody
  61. IKSolverFullBodyBipedInspector.AddInspector(solver, false);
  62. }
  63. EditorGUILayout.Space();
  64. }
  65. private void Initiate() {
  66. Warning.logged = false;
  67. // Check for possible errors, if found, do not initiate
  68. string message = "";
  69. if (script.ReferencesError(ref message)) {
  70. Warning.Log(message, script.transform, false);
  71. return;
  72. }
  73. // Notify of possible problems, but still initiate
  74. if (script.ReferencesWarning(ref message)) Warning.Log(message, script.transform, false);
  75. // Initiate
  76. script.solver.SetToReferences(script.references, script.solver.rootNode);
  77. }
  78. // Draw the scene view handles
  79. void OnSceneGUI() {
  80. // Draw the scene veiw helpers
  81. if (!script.references.isFilled) return;
  82. IKSolverFullBodyBipedInspector.AddScene(target, script.solver, color, ref selectedEffector, script.transform);
  83. }
  84. }
  85. }