GpuEcsAnimationBakerEditor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 = "Assets/Art/BakedAssets/";
  52. string subFolder = $"BakedAssets_{sourceModel.name}";
  53. string generatedAssetsFolder = Path.Combine(folder, subFolder);
  54. string dirPath= Application.dataPath.Replace("Assets", generatedAssetsFolder);
  55. if (!Directory.Exists(dirPath))
  56. {
  57. Directory.CreateDirectory(dirPath);
  58. }
  59. // if (!AssetDatabase.IsValidFolder(generatedAssetsFolder))
  60. // generatedAssetsFolder = AssetDatabase.CreateFolder("Assets/","Art/BakedAssets/"+subFolder);
  61. string animatorName = sourceModel.name;
  62. GpuEcsAnimationBakerData bakerData = (GpuEcsAnimationBakerData)bakerDataProperty.boxedValue;
  63. GameObject newGpuEcsAnimator = GpuEcsAnimationBakerServices.GenerateAnimationObject(path, bakerData,
  64. animatorName, generatedAssetsFolder);
  65. gpuEcsAnimatorProperty.boxedValue = newGpuEcsAnimator;
  66. showPrefabError = false;
  67. }
  68. }
  69. GUI.enabled = true;
  70. EditorGUILayout.PropertyField(gpuEcsAnimatorProperty);
  71. serializedObject.ApplyModifiedProperties();
  72. }
  73. }
  74. }
  75. #endif