CubismUnityEditorMenu.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. using System;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text.RegularExpressions;
  11. using Live2D.Cubism.Editor.OriginalWorkflow;
  12. using Live2D.Cubism.Framework.MotionFade;
  13. using UnityEditor;
  14. using UnityEngine;
  15. namespace Live2D.Cubism.Editor
  16. {
  17. /// <summary>
  18. /// Cubism unity editor menu.
  19. /// </summary>
  20. public class CubismUnityEditorMenu
  21. {
  22. /// <summary>
  23. /// Should import as original workflow.
  24. /// </summary>
  25. public static bool ShouldImportAsOriginalWorkflow
  26. {
  27. get
  28. {
  29. return CubismOriginalWorkflowSettings.OriginalWorkflowSettings.ShouldImportAsOriginalWorkflow;
  30. }
  31. set
  32. {
  33. CubismOriginalWorkflowSettings.OriginalWorkflowSettings.ShouldImportAsOriginalWorkflow = value;
  34. EditorUtility.SetDirty(CubismOriginalWorkflowSettings.OriginalWorkflowSettings);
  35. }
  36. }
  37. /// <summary>
  38. /// Should clear animation clip curves.
  39. /// </summary>
  40. public static bool ShouldClearAnimationCurves
  41. {
  42. get
  43. {
  44. return CubismOriginalWorkflowSettings.OriginalWorkflowSettings.ShouldClearAnimationCurves;
  45. }
  46. set
  47. {
  48. CubismOriginalWorkflowSettings.OriginalWorkflowSettings.ShouldClearAnimationCurves = value;
  49. EditorUtility.SetDirty(CubismOriginalWorkflowSettings.OriginalWorkflowSettings);
  50. }
  51. }
  52. /// <summary>
  53. /// Unity editor menu should import as original workflow.
  54. /// </summary>
  55. [MenuItem ("Live2D/Cubism/OriginalWorkflow/Should Import As Original Workflow")]
  56. private static void ImportAsOriginalWorkflow()
  57. {
  58. SetImportAsOriginalWorkflow(!ShouldImportAsOriginalWorkflow);
  59. // Disable clear animation curves.
  60. if(!ShouldImportAsOriginalWorkflow)
  61. {
  62. SetClearAnimationCurves(false);
  63. }
  64. }
  65. /// <summary>
  66. /// Unity editor menu clear animation curves.
  67. /// </summary>
  68. [MenuItem ("Live2D/Cubism/OriginalWorkflow/Should Clear Animation Curves")]
  69. private static void ClearAnimationCurves()
  70. {
  71. SetClearAnimationCurves(!ShouldClearAnimationCurves);
  72. }
  73. /// <summary>
  74. /// Unity editor context menu create an animator controller for cubism.
  75. /// </summary>
  76. [MenuItem("Assets/Create/Live2D Cubism/Animator Controller for Cubism")]
  77. private static void CreateAnimatorController()
  78. {
  79. var dataPath = Directory.GetParent(Application.dataPath).FullName + "/";
  80. var assetPath = CubismUnityEditorUtility.GetCurrentDirectoryPath();
  81. var assetName = "";
  82. if (!File.Exists(dataPath + assetPath + "/New Cubism Animator Controller.controller"))
  83. {
  84. assetName = "New Cubism Animator Controller.controller";
  85. }
  86. else
  87. {
  88. var regex = new Regex(@"new cubism animator controller [0-9]+.controller");
  89. var files = Directory.GetFiles(dataPath + assetPath, "*.controller")
  90. .Where(path=> regex.IsMatch(Path.GetFileName(path).ToLower()))
  91. .OrderBy(f => f, StringComparer.OrdinalIgnoreCase)
  92. .ToArray();
  93. for (var i = 0; i < files.Length; i++)
  94. {
  95. var name = $"New Cubism Animator Controller {(i + 1)}.controller";
  96. if (files[i].ToLower().EndsWith(name, StringComparison.OrdinalIgnoreCase))
  97. {
  98. continue;
  99. }
  100. assetName = name;
  101. break;
  102. }
  103. if (string.IsNullOrEmpty(assetName))
  104. {
  105. assetName = $"New Cubism Animator Controller {(files.Length + 1)}.controller";
  106. }
  107. }
  108. assetPath = Path.Combine(assetPath, assetName);
  109. CubismFadeMotionImporter.CreateAnimatorController(assetPath);
  110. AssetDatabase.Refresh();
  111. }
  112. /// <summary>
  113. /// Set import as original workflow.
  114. /// </summary>
  115. public static void SetImportAsOriginalWorkflow(bool isEnable)
  116. {
  117. ShouldImportAsOriginalWorkflow= isEnable;
  118. Menu.SetChecked ("Live2D/Cubism/OriginalWorkflow/Should Import As Original Workflow", ShouldImportAsOriginalWorkflow);
  119. }
  120. /// <summary>
  121. /// Set clear animation curves.
  122. /// </summary>
  123. public static void SetClearAnimationCurves(bool isEnable)
  124. {
  125. ShouldClearAnimationCurves= (ShouldImportAsOriginalWorkflow && isEnable);
  126. Menu.SetChecked ("Live2D/Cubism/OriginalWorkflow/Should Clear Animation Curves", ShouldClearAnimationCurves);
  127. }
  128. /// <summary>
  129. /// Initialize cubism menu.
  130. /// </summary>
  131. [InitializeOnLoadMethod]
  132. private static void Initialize()
  133. {
  134. EditorApplication.delayCall += () => Menu.SetChecked ("Live2D/Cubism/OriginalWorkflow/Should Import As Original Workflow", ShouldImportAsOriginalWorkflow);
  135. EditorApplication.delayCall += () => Menu.SetChecked ("Live2D/Cubism/OriginalWorkflow/Should Clear Animation Curves", ShouldClearAnimationCurves);
  136. }
  137. }
  138. }