VFXAssetEditorUtility.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System.Collections;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEditor.VFX;
  7. using UnityEngine.VFX;
  8. using UnityEditor;
  9. using UnityEditor.VFX.UI;
  10. using UnityEditor.ProjectWindowCallback;
  11. using UnityObject = UnityEngine.Object;
  12. using UnityEditor.Build;
  13. using UnityEditor.Build.Reporting;
  14. namespace UnityEditor
  15. {
  16. class VFXBuildPreprocessor : IPreprocessBuildWithReport
  17. {
  18. int IOrderedCallback.callbackOrder => 0;
  19. void IPreprocessBuildWithReport.OnPreprocessBuild(BuildReport report)
  20. {
  21. VFXManagerEditor.CheckVFXManager();
  22. }
  23. }
  24. [InitializeOnLoad]
  25. static class VisualEffectAssetEditorUtility
  26. {
  27. private static string m_TemplatePath = null;
  28. public static string templatePath
  29. {
  30. get
  31. {
  32. if (m_TemplatePath == null)
  33. {
  34. m_TemplatePath = VisualEffectGraphPackageInfo.assetPackagePath + "/Editor/Templates/";
  35. }
  36. return m_TemplatePath;
  37. }
  38. }
  39. static void CheckVFXManagerOnce()
  40. {
  41. VFXManagerEditor.CheckVFXManager();
  42. EditorApplication.update -= CheckVFXManagerOnce;
  43. }
  44. static VisualEffectAssetEditorUtility()
  45. {
  46. EditorApplication.update += CheckVFXManagerOnce;
  47. UnityEngine.VFX.VFXManager.activateVFX = true;
  48. }
  49. public const string templateAssetName = "SimpleParticleSystem.vfx";
  50. public const string templateBlockSubgraphAssetName = "DefaultSubgraphBlock.vfxblock";
  51. public const string templateOperatorSubgraphAssetName = "DefaultSubgraphOperator.vfxoperator";
  52. public const string editorResourcesFolder = "Editor/UIResources";
  53. public static string editorResourcesPath => VisualEffectGraphPackageInfo.assetPackagePath + "/" + editorResourcesFolder;
  54. [MenuItem("GameObject/Visual Effects/Visual Effect", priority = 12)]
  55. public static void CreateVisualEffectGameObject(MenuCommand menuCommand)
  56. {
  57. GameObject go = new GameObject(GameObjectUtility.GetUniqueNameForSibling(null, "Visual Effect"));
  58. GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
  59. var vfxComp = go.AddComponent<VisualEffect>();
  60. if (Selection.activeObject != null && Selection.activeObject is VisualEffectAsset)
  61. {
  62. vfxComp.visualEffectAsset = Selection.activeObject as VisualEffectAsset;
  63. vfxComp.startSeed = (uint)Random.Range(int.MinValue, int.MaxValue);
  64. }
  65. Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
  66. Selection.activeObject = go;
  67. }
  68. public static VisualEffectAsset CreateNewAsset(string path)
  69. {
  70. return CreateNew<VisualEffectAsset>(path);
  71. }
  72. public static T CreateNew<T>(string path) where T : UnityObject
  73. {
  74. string emptyAsset =
  75. @"%YAML 1.1
  76. %TAG !u! tag:unity3d.com,2011:
  77. --- !u!114 &114350483966674976
  78. MonoBehaviour:
  79. m_Script: {fileID: 11500000, guid: 7d4c867f6b72b714dbb5fd1780afe208, type: 3}
  80. --- !u!2058629511 &1
  81. VisualEffectResource:
  82. m_Graph: {fileID: 114350483966674976}
  83. ";
  84. File.WriteAllText(path, emptyAsset);
  85. AssetDatabase.ImportAsset(path);
  86. return AssetDatabase.LoadAssetAtPath<T>(path);
  87. }
  88. [MenuItem("Assets/Create/Visual Effects/Visual Effect Graph", false, 306)]
  89. public static void CreateVisualEffectAsset()
  90. {
  91. VFXLibrary.LogUnsupportedSRP();
  92. string templateString = "";
  93. try
  94. {
  95. templateString = System.IO.File.ReadAllText(templatePath + templateAssetName);
  96. }
  97. catch (System.Exception e)
  98. {
  99. Debug.LogError("Couldn't read template for new vfx asset : " + e.Message);
  100. return;
  101. }
  102. Texture2D texture = EditorGUIUtility.FindTexture(typeof(VisualEffectAsset));
  103. var action = ScriptableObject.CreateInstance<DoCreateNewVFX>();
  104. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, action, "New VFX.vfx", texture, null);
  105. }
  106. [MenuItem("Assets/Create/Visual Effects/Visual Effect Defaults", false, 307)]
  107. public static void CreateVisualEffectDefaults()
  108. {
  109. var obj = VFXResources.CreateInstance<VFXResources>();
  110. obj.SetDefaults();
  111. AssetDatabase.CreateAsset(obj, "Assets/Visual Effects Defaults.asset");
  112. Selection.activeObject = obj;
  113. }
  114. [MenuItem("Assets/Create/Visual Effects/Visual Effect Defaults", true)]
  115. public static bool IsCreateVisualEffectDefaultsActive()
  116. {
  117. var resources = Resources.FindObjectsOfTypeAll<VFXResources>();
  118. return resources == null || resources.Length == 0;
  119. }
  120. public static void CreateTemplateAsset(string pathName)
  121. {
  122. try
  123. {
  124. var templateString = System.IO.File.ReadAllText(templatePath + templateAssetName);
  125. System.IO.File.WriteAllText(pathName, templateString);
  126. }
  127. catch (FileNotFoundException)
  128. {
  129. CreateNewAsset(pathName);
  130. }
  131. AssetDatabase.ImportAsset(pathName);
  132. }
  133. internal class DoCreateNewVFX : EndNameEditAction
  134. {
  135. public override void Action(int instanceId, string pathName, string resourceFile)
  136. {
  137. CreateTemplateAsset(pathName);
  138. var resource = VisualEffectResource.GetResourceAtPath(pathName);
  139. ProjectWindowUtil.FrameObjectInProjectWindow(resource.asset.GetInstanceID());
  140. }
  141. }
  142. internal class DoCreateNewSubgraphOperator : EndNameEditAction
  143. {
  144. public override void Action(int instanceId, string pathName, string resourceFile)
  145. {
  146. var sg = CreateNew<VisualEffectSubgraphOperator>(pathName);
  147. ProjectWindowUtil.FrameObjectInProjectWindow(sg.GetInstanceID());
  148. }
  149. }
  150. internal class DoCreateNewSubgraphBlock : EndNameEditAction
  151. {
  152. public override void Action(int instanceId, string pathName, string resourceFile)
  153. {
  154. var sg = CreateNew<VisualEffectSubgraphBlock>(pathName);
  155. ProjectWindowUtil.FrameObjectInProjectWindow(sg.GetInstanceID());
  156. }
  157. }
  158. [MenuItem("Assets/Create/Visual Effects/Visual Effect Subgraph Operator", false, 308)]
  159. public static void CreateVisualEffectSubgraphOperator()
  160. {
  161. string fileName = "New VFX Subgraph Operator.vfxoperator";
  162. CreateVisualEffectSubgraph<VisualEffectSubgraphOperator, DoCreateNewSubgraphOperator>(fileName, templateOperatorSubgraphAssetName);
  163. }
  164. [MenuItem("Assets/Create/Visual Effects/Visual Effect Subgraph Block", false, 309)]
  165. public static void CreateVisualEffectSubgraphBlock()
  166. {
  167. string fileName = "New VFX Subgraph Block.vfxblock";
  168. CreateVisualEffectSubgraph<VisualEffectSubgraphBlock, DoCreateNewSubgraphBlock>(fileName, templateBlockSubgraphAssetName);
  169. }
  170. public static void CreateVisualEffectSubgraph<T, U>(string fileName, string templateName) where U : EndNameEditAction
  171. {
  172. string templateString = "";
  173. Texture2D texture = EditorGUIUtility.FindTexture(typeof(T));
  174. try // try with the template
  175. {
  176. templateString = System.IO.File.ReadAllText(templatePath + templateName);
  177. ProjectWindowUtil.CreateAssetWithContent(fileName, templateString, texture);
  178. }
  179. catch (System.Exception e)
  180. {
  181. Debug.LogError("Couldn't read template for new visual effect subgraph : " + e.Message);
  182. var action = ScriptableObject.CreateInstance<U>();
  183. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, action, fileName, texture, null);
  184. return;
  185. }
  186. }
  187. }
  188. }