GpuEcsAnimationBakerEditor.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #if UNITY_EDITOR
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using GpuEcsAnimationBaker.Engine.Data;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace GPUECSAnimationBaker.Engine.Baker
  8. {
  9. [CustomEditor(typeof(GpuEcsAnimationBakerBehaviour))]
  10. public class GpuEcsAnimationBakerEditor : UnityEditor.Editor
  11. {
  12. private SerializedProperty bakerDataProperty;
  13. private SerializedProperty gpuEcsAnimatorProperty;
  14. private bool showPrefabError = false;
  15. void OnEnable()
  16. {
  17. bakerDataProperty = serializedObject.FindProperty("bakerData");
  18. gpuEcsAnimatorProperty = serializedObject.FindProperty("gpuEcsAnimator");
  19. showPrefabError = false;
  20. }
  21. public override void OnInspectorGUI()
  22. {
  23. GameObject sourceModel = ((GpuEcsAnimationBakerBehaviour)target).gameObject;
  24. GUIStyle labelStyle = new GUIStyle(GUI.skin.label);
  25. labelStyle.normal.textColor = new Color(1f, 0.5f, 0f, 1f);
  26. labelStyle.wordWrap = true;
  27. labelStyle.alignment = TextAnchor.MiddleLeft;
  28. labelStyle.fontSize = 22;
  29. labelStyle.fontStyle = FontStyle.Bold;
  30. labelStyle.fixedHeight = 36;
  31. GUILayout.Label("GPU ECS Animation Baker", labelStyle);
  32. serializedObject.Update();
  33. EditorGUILayout.PropertyField(bakerDataProperty);
  34. bool validated = GpuEcsAnimationBakerServices.ValidateAnimationBakerData(
  35. (GpuEcsAnimationBakerData)bakerDataProperty.boxedValue, sourceModel, out string errors);
  36. if(!validated) EditorGUILayout.HelpBox(errors, MessageType.Error);
  37. if(showPrefabError) EditorGUILayout.HelpBox("Generation can only happen on unloaded, selected prefabs", MessageType.Error);
  38. GUI.enabled = validated;
  39. GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
  40. buttonStyle.normal.textColor = new Color(1f, 0.5f, 0f, 1f);
  41. buttonStyle.alignment = TextAnchor.MiddleCenter;
  42. buttonStyle.fontSize = 22;
  43. buttonStyle.fontStyle = FontStyle.Bold;
  44. buttonStyle.fixedHeight = 36;
  45. if (GUILayout.Button("Generate GPU ECS Animator", buttonStyle))
  46. {
  47. string path = AssetDatabase.GetAssetPath(sourceModel);
  48. if (string.IsNullOrEmpty(path) || !PrefabUtility.IsPartOfAnyPrefab(sourceModel))
  49. showPrefabError = true;
  50. else
  51. {
  52. string folder = "Assets/Art/BakedAssets/";
  53. string subFolder = $"BakedAssets_{sourceModel.name}";
  54. string generatedAssetsFolder = Path.Combine(folder, subFolder);
  55. string dirPath= Application.dataPath.Replace("Assets", generatedAssetsFolder);
  56. if (!Directory.Exists(dirPath))
  57. {
  58. Directory.CreateDirectory(dirPath);
  59. }
  60. // if (!AssetDatabase.IsValidFolder(generatedAssetsFolder))
  61. // generatedAssetsFolder = AssetDatabase.CreateFolder("Assets/","Art/BakedAssets/"+subFolder);
  62. string animatorName = sourceModel.name;
  63. GpuEcsAnimationBakerData bakerData = (GpuEcsAnimationBakerData)bakerDataProperty.boxedValue;
  64. GpuEcsAnimationBakerBehaviour gpuEcsAnimationBakerBehaviour= serializedObject.targetObject as GpuEcsAnimationBakerBehaviour;
  65. IGPUSkeletonBake[] gpuSkeletonBakes= gpuEcsAnimationBakerBehaviour.gameObject.transform.GetComponentsInChildren<IGPUSkeletonBake>();
  66. if (gpuSkeletonBakes != null&&gpuSkeletonBakes.Length>0)
  67. {
  68. List<AttachmentAnchor> attachmentAnchors = new List<AttachmentAnchor>();
  69. attachmentAnchors.AddRange(bakerData.attachmentAnchors);
  70. for (int i = 0; i < gpuSkeletonBakes.Length; i++)
  71. {
  72. IGPUSkeletonBake iGPUSkeletonBake= gpuSkeletonBakes[i];
  73. Transform transform= iGPUSkeletonBake.GetTransform();
  74. if (transform != null)
  75. {
  76. string skeletonName= iGPUSkeletonBake.GetSkeletonName();
  77. if (!string.IsNullOrEmpty(skeletonName))
  78. {
  79. bool isOk = true;
  80. for (int j = 0; j < attachmentAnchors.Count; j++)
  81. {
  82. if (attachmentAnchors[i].attachmentAnchorID.Equals(skeletonName))
  83. {
  84. isOk = false;
  85. break;
  86. }
  87. }
  88. if (isOk)
  89. {
  90. AttachmentAnchor attachmentAnchor = new AttachmentAnchor();
  91. attachmentAnchor.attachmentAnchorID= skeletonName;
  92. attachmentAnchor.attachmentAnchorTransform= transform;
  93. attachmentAnchors.Add(attachmentAnchor);
  94. }
  95. }
  96. }
  97. }
  98. bakerData.attachmentAnchors = attachmentAnchors.ToArray();
  99. }
  100. GameObject newGpuEcsAnimator = GpuEcsAnimationBakerServices.GenerateAnimationObject(path, bakerData,
  101. animatorName, generatedAssetsFolder);
  102. gpuEcsAnimatorProperty.boxedValue = newGpuEcsAnimator;
  103. showPrefabError = false;
  104. }
  105. }
  106. GUI.enabled = true;
  107. EditorGUILayout.PropertyField(gpuEcsAnimatorProperty);
  108. serializedObject.ApplyModifiedProperties();
  109. }
  110. }
  111. }
  112. #endif