EntityViewCodeGen.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Runtime.Remoting.Metadata.W3cXsd2001;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using XGame.Framework.Map;
  6. using XGame.Framework.ObjectCollection;
  7. namespace XGame.Editor.Map
  8. {
  9. internal class EntityViewCodeGen : CodeGenerator
  10. {
  11. /// <summary>
  12. /// 生成代码
  13. /// </summary>
  14. /// <param name="behaviour"></param>
  15. /// <param name="entityType">0->Entity 1->MapUI</param>
  16. /// <returns></returns>
  17. public bool Gen(EntityBehaviour behaviour, int entityType)
  18. {
  19. if (!VerifyPrefab(behaviour.gameObject))
  20. {
  21. return false;
  22. }
  23. try
  24. {
  25. var isMapUI = entityType == 1;
  26. var codesFolder = isMapUI ? "Map/UI" : "Map/Entities";
  27. InitPath(codesFolder);
  28. var entityName = ToTitleCase(behaviour.gameObject.name);
  29. if (!GenCommonCode(entityName, entityName, entityName + "View", isMapUI ? "MapUIViewTemplate" : "EntityViewTemplate"))
  30. {
  31. return false;
  32. }
  33. if (!GenAIComponent(behaviour, isMapUI, entityName))
  34. {
  35. return false;
  36. }
  37. if (!GenViewModel(behaviour, isMapUI, entityName))
  38. {
  39. return false;
  40. }
  41. AssetDatabase.SaveAssets();
  42. AssetDatabase.Refresh();
  43. }
  44. catch (Exception ex)
  45. {
  46. Debug.LogException(ex);
  47. return false;
  48. }
  49. return true;
  50. }
  51. private bool GenAIComponent(EntityBehaviour behaviour, bool isMapUI, string entityName)
  52. {
  53. if (isMapUI)
  54. { //MapUI 不生成AI脚本
  55. return true;
  56. }
  57. var lastCodesPath = codesRootPath;
  58. codesRootPath = codesRootPath.Replace("Map/Entities", "Battle/Components/AI");
  59. var result = GenCommonCode(entityName, entityName, entityName + "AI", "AIComponentTemplate");
  60. codesRootPath = lastCodesPath;
  61. return result;
  62. }
  63. private bool GenViewModel(EntityBehaviour behaviour, bool isMapUI, string entityName)
  64. {
  65. var result = GenViewModelCode(behaviour.Collector as ObjectCollector, entityName, entityName, entityName + "VM", "EntityVMTemplate", codeText =>
  66. {
  67. return codeText.Replace("<#Namespace>", isMapUI ? $"{productName}.Map.UI" : $"{productName}.Map");
  68. });
  69. return result;
  70. }
  71. }
  72. }