GpuEcsAnimationBakerEditor.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #if UNITY_EDITOR
  2. using System.IO;
  3. using GpuEcsAnimationBaker.Engine.Data;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace GPUECSAnimationBaker.Engine.Baker
  7. {
  8. [CustomEditor(typeof(GpuEcsAnimationBakerBehaviour))]
  9. public class GpuEcsAnimationBakerEditor : UnityEditor.Editor
  10. {
  11. private SerializedProperty bakerDataProperty;
  12. private SerializedProperty gpuEcsAnimatorProperty;
  13. private bool showPrefabError = false;
  14. void OnEnable()
  15. {
  16. bakerDataProperty = serializedObject.FindProperty("bakerData");
  17. gpuEcsAnimatorProperty = serializedObject.FindProperty("gpuEcsAnimator");
  18. showPrefabError = false;
  19. }
  20. public override void OnInspectorGUI()
  21. {
  22. GameObject sourceModel = ((GpuEcsAnimationBakerBehaviour)target).gameObject;
  23. GUIStyle labelStyle = new GUIStyle(GUI.skin.label);
  24. labelStyle.normal.textColor = new Color(1f, 0.5f, 0f, 1f);
  25. labelStyle.wordWrap = true;
  26. labelStyle.alignment = TextAnchor.MiddleLeft;
  27. labelStyle.fontSize = 22;
  28. labelStyle.fontStyle = FontStyle.Bold;
  29. labelStyle.fixedHeight = 36;
  30. GUILayout.Label("GPU ECS Animation Baker", labelStyle);
  31. serializedObject.Update();
  32. EditorGUILayout.PropertyField(bakerDataProperty);
  33. bool validated = GpuEcsAnimationBakerServices.ValidateAnimationBakerData(
  34. (GpuEcsAnimationBakerData)bakerDataProperty.boxedValue, sourceModel, out string errors);
  35. if(!validated) EditorGUILayout.HelpBox(errors, MessageType.Error);
  36. if(showPrefabError) EditorGUILayout.HelpBox("Generation can only happen on unloaded, selected prefabs", MessageType.Error);
  37. GUI.enabled = validated;
  38. GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
  39. buttonStyle.normal.textColor = new Color(1f, 0.5f, 0f, 1f);
  40. buttonStyle.alignment = TextAnchor.MiddleCenter;
  41. buttonStyle.fontSize = 22;
  42. buttonStyle.fontStyle = FontStyle.Bold;
  43. buttonStyle.fixedHeight = 36;
  44. if (GUILayout.Button("Generate GPU ECS Animator", buttonStyle))
  45. {
  46. string path = AssetDatabase.GetAssetPath(sourceModel);
  47. if (string.IsNullOrEmpty(path) || !PrefabUtility.IsPartOfAnyPrefab(sourceModel))
  48. showPrefabError = true;
  49. else
  50. {
  51. string folder = Path.GetDirectoryName(path);
  52. string subFolder = $"BakedAssets_{sourceModel.name}";
  53. string generatedAssetsFolder = Path.Combine(folder, subFolder);
  54. if (!AssetDatabase.IsValidFolder(generatedAssetsFolder))
  55. generatedAssetsFolder = AssetDatabase.GUIDToAssetPath(AssetDatabase.CreateFolder(
  56. folder, subFolder));
  57. string animatorName = sourceModel.name;
  58. GpuEcsAnimationBakerData bakerData = (GpuEcsAnimationBakerData)bakerDataProperty.boxedValue;
  59. GameObject newGpuEcsAnimator = GpuEcsAnimationBakerServices.GenerateAnimationObject(path, bakerData,
  60. animatorName, generatedAssetsFolder);
  61. gpuEcsAnimatorProperty.boxedValue = newGpuEcsAnimator;
  62. showPrefabError = false;
  63. }
  64. }
  65. GUI.enabled = true;
  66. EditorGUILayout.PropertyField(gpuEcsAnimatorProperty);
  67. serializedObject.ApplyModifiedProperties();
  68. }
  69. }
  70. }
  71. #endif