GettingStartedWindow.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. //
  2. // ShaderGraphEssentials for Unity
  3. // (c) 2019 PH Graphics
  4. // Source code may be used and modified for personal or commercial projects.
  5. // Source code may NOT be redistributed or sold.
  6. //
  7. // *** A NOTE ABOUT PIRACY ***
  8. //
  9. // If you got this asset from a pirate site, please consider buying it from the Unity asset store. This asset is only legally available from the Unity Asset Store.
  10. //
  11. // I'm a single indie dev supporting my family by spending hundreds and thousands of hours on this and other assets. It's very offensive, rude and just plain evil to steal when I (and many others) put so much hard work into the software.
  12. //
  13. // Thank you.
  14. //
  15. // *** END NOTE ABOUT PIRACY ***
  16. //
  17. using System;
  18. using System.Collections;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using UnityEditor;
  22. using UnityEditor.SceneManagement;
  23. using UnityEngine;
  24. using UnityEngine.WSA;
  25. using Application = UnityEngine.Application;
  26. namespace ShaderGraphEssentials
  27. {
  28. [InitializeOnLoad]
  29. public class GettingStartedWindowShow
  30. {
  31. private const string SettingsPath = "Assets/Plugins/ShaderGraphEssentials/Settings.asset";
  32. static GettingStartedWindowShow()
  33. {
  34. var settings = AssetDatabase.LoadAssetAtPath(SettingsPath, typeof(SGESettings)) as SGESettings;
  35. bool showWindow = !(settings && !settings.OpenGettingStartedWindow);
  36. if (showWindow)
  37. {
  38. EditorApplication.update += OnUpdate;
  39. }
  40. }
  41. private static void OnUpdate()
  42. {
  43. EditorApplication.update -= OnUpdate;
  44. SGESettings newSettings = ScriptableObject.CreateInstance<SGESettings>();
  45. AssetDatabase.CreateAsset(newSettings, SettingsPath);
  46. AssetDatabase.SaveAssets();
  47. AssetDatabase.Refresh();
  48. GettingStartedWindow window = (GettingStartedWindow)EditorWindow.GetWindow(typeof(GettingStartedWindow));
  49. window.Show();
  50. }
  51. }
  52. public class GettingStartedWindow : EditorWindow
  53. {
  54. private const string SGEVersion = "1.1.17";
  55. private const string SGEPath = "Assets/Plugins/ShaderGraphEssentials";
  56. private const string SGELogoFileName = "Plugin/Editor/GettingStarted/Data/SGE_key_128x128.png";
  57. private const string ManualFileName = "ShaderGraphEssentials_Documentation.pdf";
  58. private const string ChangeLogName = "ChangeLog.txt";
  59. private const string NoiseScene = BaseDemoPath + "/Scenes/ShaderGraphEssentials_Demo.unity";
  60. private const string SimpleLitScene = URPDemoPath + "/Scenes/ShaderGraphEssentials_Showcase_SimpleLit.unity";
  61. private const string ToonLitScene = URPDemoPath + "/Scenes/ShaderGraphEssentials_Showcase_ToonLit.unity";
  62. private const string WaterScene = URPDemoPath + "/Scenes/ShaderGraphEssentials_Showcase_Water.unity";
  63. private const string DemoFolderPath = "DemoScenes";
  64. private const string BaseDemoPath = "DemoScenes/Demo_Base";
  65. private const string URPDemoPath = "DemoScenes/Demo_URP";
  66. private const string BasePluginPath = "Plugin/Editor/Plugin_Base";
  67. private const string URPPluginPath = "Plugin/Editor/Plugin_URP";
  68. private const string URPPluginPackagePath = "Packages/SGE_URP.unitypackage";
  69. private const string URPDemoPackagePath = "Packages/SGE_URP_Demo.unitypackage";
  70. private const string HDRPPluginPackagePath = "Packages/SGE_HDRP.unitypackage";
  71. private const string HDRPDemoPackagePath = "Packages/SGE_HDRP_Demo.unitypackage";
  72. private GUIStyle _wrapLabelStyle;
  73. private Texture2D _logoTexture;
  74. private bool _hasError;
  75. // various sizes
  76. private const int LogoTextureSize = 128;
  77. private const int Margin = 10;
  78. private const int ButtonHeight = 30;
  79. private const int ButtonWidth = 120;
  80. private const int LargeButtonWidth = 160;
  81. private Vector2 _scrollPosition = Vector2.zero;
  82. private Vector2 _defaultWindowSize = Vector2.zero;
  83. private bool _initialized = false;
  84. [MenuItem("Tools/ShaderGraph Essentials/Getting Started")]
  85. static void Init()
  86. {
  87. // Get existing open window or if none, make a new one:
  88. GettingStartedWindow window = (GettingStartedWindow)EditorWindow.GetWindow(typeof(GettingStartedWindow));
  89. window.Show();
  90. }
  91. private bool GetInternalFile(string pathFromSGE, out string fullPath)
  92. {
  93. fullPath = Path.Combine(SGEPath, pathFromSGE);
  94. if (File.Exists(fullPath))
  95. return true;
  96. Debug.LogError("File " + fullPath + " doesn't exist. Did you move the ShaderGraphEssentials root folder from Assets/ ? Unfortunately this isn't supported yet.");
  97. _hasError = true;
  98. return false;
  99. }
  100. private void Awake()
  101. {
  102. string fullLogoPath;
  103. if (!GetInternalFile(SGELogoFileName, out fullLogoPath))
  104. {
  105. return;
  106. }
  107. _logoTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(fullLogoPath);
  108. }
  109. private bool OpenFileWithDefaultEditor(string path)
  110. {
  111. string fullPath;
  112. if (!GetInternalFile(path, out fullPath))
  113. {
  114. return false;
  115. }
  116. fullPath = Path.GetFullPath(fullPath);
  117. if (!File.Exists(fullPath))
  118. {
  119. Debug.LogError("File " + fullPath + " doesn't exist. Did you move the ShaderGraphEssentials root folder from Assets/ ? Unfortunately this isn't supported yet.");
  120. _hasError = true;
  121. return false;
  122. }
  123. #if UNITY_EDITOR_WIN
  124. System.Diagnostics.Process.Start($@"{fullPath}");
  125. #elif UNITY_EDITOR_OSX
  126. EditorUtility.RevealInFinder($@"{fullPath}");
  127. #endif
  128. return true;
  129. }
  130. private bool CheckForErrors()
  131. {
  132. if (_hasError)
  133. {
  134. GUILayout.Label(
  135. "There was an error constructing this window. Please check your console for errors. If you can't fix it, please don't hesitate to ask for support");
  136. return true;
  137. }
  138. return false;
  139. }
  140. private void InitializeWindow()
  141. {
  142. titleContent.text = "Getting Started";
  143. minSize = new Vector2(250, 400);
  144. _defaultWindowSize = new Vector2(520, 800);
  145. Vector2 initialPosition = 0.5f * (new Vector2(Screen.currentResolution.width, Screen.currentResolution.height) - _defaultWindowSize);
  146. position = new Rect(initialPosition, _defaultWindowSize);
  147. _wrapLabelStyle = new GUIStyle(EditorStyles.label) {wordWrap = true};
  148. }
  149. void OnGUI()
  150. {
  151. if (!_initialized)
  152. {
  153. InitializeWindow();
  154. _initialized = true;
  155. }
  156. if (CheckForErrors()) return;
  157. _scrollPosition = GUI.BeginScrollView(new Rect(0, 0, position.width, position.height), _scrollPosition,
  158. new Rect(0, 0, _defaultWindowSize.x - 10, _defaultWindowSize.y - 10), false, false);
  159. float yOffset = 0;
  160. float defaultXSize = position.width - Margin - Margin;
  161. // Header
  162. GUI.BeginGroup(new Rect(Margin, Margin, defaultXSize, LogoTextureSize));
  163. float xOffset = 0;
  164. GUI.DrawTexture(new Rect(xOffset, 0, LogoTextureSize, LogoTextureSize), _logoTexture);
  165. xOffset += LogoTextureSize + Margin;
  166. GUI.Label(new Rect(xOffset, 0, 100, 30), "Version: " + SGEVersion);
  167. if (GUI.Button(new Rect(xOffset, 30, ButtonWidth, ButtonHeight), "View Changelog"))
  168. {
  169. OpenChangelog();
  170. if (CheckForErrors()) return;
  171. }
  172. if (GUI.Button(new Rect(xOffset, 30 + ButtonHeight + Margin, ButtonWidth, ButtonHeight), "View Manual"))
  173. {
  174. OpenManual();
  175. if (CheckForErrors()) return;
  176. }
  177. xOffset += ButtonWidth + Margin;
  178. if (GUI.Button(new Rect(xOffset, 30, LargeButtonWidth, ButtonHeight), "View Offline Changelog"))
  179. {
  180. OpenFileWithDefaultEditor(ChangeLogName);
  181. if (CheckForErrors()) return;
  182. }
  183. if (GUI.Button(new Rect(xOffset, 30 + ButtonHeight + Margin, LargeButtonWidth, ButtonHeight), "View Offline Manual"))
  184. {
  185. OpenFileWithDefaultEditor(ManualFileName);
  186. if (CheckForErrors()) return;
  187. }
  188. GUI.EndGroup();
  189. yOffset += Margin + LogoTextureSize;
  190. GUI.Label(new Rect(Margin, yOffset + Margin, defaultXSize, 10), "", GUI.skin.horizontalSlider);
  191. yOffset += Margin + Margin;
  192. // Getting started title
  193. yOffset += Margin;
  194. GUI.BeginGroup(new Rect(Margin, yOffset, defaultXSize, 20));
  195. GUI.Label(new Rect(defaultXSize / 2f - 50, 0, 100, 20), "Getting Started", EditorStyles.largeLabel);
  196. GUI.EndGroup();
  197. yOffset += Margin + 20;
  198. // URP
  199. GUI.BeginGroup(new Rect(Margin, yOffset, defaultXSize, 260));
  200. GUI.Label(new Rect(0, 0, defaultXSize, 30), "URP", EditorStyles.boldLabel);
  201. if (GUI.Button(new Rect(0, 30, defaultXSize, ButtonHeight), "Import URP plugin"))
  202. {
  203. ImportURPPlugin();
  204. }
  205. bool isURPImported = IsURPImported();
  206. using (new EditorGUI.DisabledScope(!isURPImported))
  207. {
  208. if (GUI.Button(new Rect(0, ButtonHeight * 2 + Margin, defaultXSize, ButtonHeight), "Import URP Demo scenes"))
  209. {
  210. ImportURPScenes();
  211. }
  212. }
  213. bool areURPScenesImported = AreURPScenesImported();
  214. using (new EditorGUI.DisabledScope(!areURPScenesImported))
  215. {
  216. if (GUI.Button(new Rect(0, ButtonHeight * 3 + Margin * 2, defaultXSize, ButtonHeight), "Open noise scene"))
  217. {
  218. OpenScene(NoiseScene);
  219. }
  220. if (GUI.Button(new Rect(0, ButtonHeight * 4 + Margin * 3, defaultXSize, ButtonHeight), "Open simple lit scene"))
  221. {
  222. OpenScene(SimpleLitScene);
  223. }
  224. if (GUI.Button(new Rect(0, ButtonHeight * 5 + Margin * 4, defaultXSize, ButtonHeight), "Open toon lit scene"))
  225. {
  226. OpenScene(ToonLitScene);
  227. }
  228. if (GUI.Button(new Rect(0, ButtonHeight * 6 + Margin * 5, defaultXSize, ButtonHeight), "Open water scene"))
  229. {
  230. OpenScene(WaterScene);
  231. }
  232. }
  233. GUI.EndGroup();
  234. yOffset += 260;
  235. // HDRP
  236. yOffset += Margin;
  237. GUI.BeginGroup(new Rect(Margin, yOffset, defaultXSize, 140));
  238. GUI.Label(new Rect(0, 0, defaultXSize, 30), "HDRP", EditorStyles.boldLabel);
  239. if (GUI.Button(new Rect(0, 30, defaultXSize, ButtonHeight), "Import HDRP plugin"))
  240. {
  241. ImportHDRPPlugin();
  242. }
  243. bool isHDRPImported = IsHDRPImported();
  244. using (new EditorGUI.DisabledScope(!isHDRPImported))
  245. {
  246. if (GUI.Button(new Rect(0, ButtonHeight * 2 + Margin, defaultXSize, ButtonHeight), "Import HDRP Demo scenes"))
  247. {
  248. ImportHDRPScenes();
  249. }
  250. }
  251. bool areHDRPScenesImported = AreHDRPScenesImported();
  252. using (new EditorGUI.DisabledScope(!areHDRPScenesImported))
  253. {
  254. if (GUI.Button(new Rect(0, ButtonHeight * 3 + Margin * 2, defaultXSize, ButtonHeight), "Open noise scene"))
  255. {
  256. OpenScene(NoiseScene);
  257. }
  258. }
  259. GUI.EndGroup();
  260. yOffset += 140;
  261. // switch URP / HDRP
  262. yOffset += Margin;
  263. GUI.BeginGroup(new Rect(Margin, yOffset, defaultXSize, 90));
  264. GUI.Label(new Rect(0, 0, defaultXSize, 30), "Switch between URP / HDRP", EditorStyles.boldLabel);
  265. GUI.Label(new Rect(0, 30, defaultXSize, 30), "To switch between URP / HDRP, hit the button below and reimport the correct plugin"
  266. , _wrapLabelStyle);
  267. if (GUI.Button(new Rect(0, 60, defaultXSize, ButtonHeight), "Remove plugins and demo scenes"))
  268. {
  269. CleanEverything();
  270. }
  271. GUI.EndGroup();
  272. yOffset += 90;
  273. GUI.Label(new Rect(Margin, yOffset + Margin, defaultXSize, 10), "", GUI.skin.horizontalSlider);
  274. yOffset += Margin + Margin;
  275. // Help title
  276. yOffset += Margin;
  277. GUI.BeginGroup(new Rect(Margin, yOffset, defaultXSize, 30));
  278. GUI.Label(new Rect(defaultXSize / 2f - 15, 0, 100, 20), "Help", EditorStyles.largeLabel);
  279. GUI.EndGroup();
  280. yOffset += Margin + 20;
  281. // Help
  282. GUI.BeginGroup(new Rect(Margin, yOffset, defaultXSize, ButtonHeight));
  283. if (GUI.Button(new Rect(0, 0, defaultXSize / 2 - Margin, ButtonHeight), "Discord"))
  284. {
  285. OpenDiscordHelp();
  286. }
  287. if (GUI.Button(new Rect(defaultXSize / 2 + Margin, 0, defaultXSize / 2 - Margin, ButtonHeight), "Email"))
  288. {
  289. OpenEmailHelp();
  290. }
  291. GUI.EndGroup();
  292. GUI.EndScrollView();
  293. }
  294. private void OpenEmailHelp()
  295. {
  296. Application.OpenURL("mailto:ph.graphics.unity@gmail.com");
  297. }
  298. private void OpenDiscordHelp()
  299. {
  300. Application.OpenURL("https://discord.gg/ksURBah");
  301. }
  302. private void OpenManual()
  303. {
  304. Application.OpenURL("http://assetstore.phbarralis.com/sge/features.html");
  305. }
  306. private void OpenChangelog()
  307. {
  308. Application.OpenURL("http://assetstore.phbarralis.com/sge/changelog.html");
  309. }
  310. private void ImportHDRPScenes()
  311. {
  312. ImportPackage(HDRPDemoPackagePath);
  313. }
  314. private bool AreHDRPScenesImported()
  315. {
  316. return Directory.Exists(Path.Combine(SGEPath, BaseDemoPath));
  317. }
  318. private bool IsHDRPImported()
  319. {
  320. return Directory.Exists(Path.Combine(SGEPath, BasePluginPath));
  321. }
  322. private void ImportHDRPPlugin()
  323. {
  324. ImportPackage(HDRPPluginPackagePath);
  325. }
  326. private void OpenScene(string sceneName)
  327. {
  328. string fullPath;
  329. if (!GetInternalFile(sceneName, out fullPath))
  330. return;
  331. EditorSceneManager.OpenScene(fullPath, OpenSceneMode.Single);
  332. }
  333. private bool AreURPScenesImported()
  334. {
  335. return Directory.Exists(Path.Combine(SGEPath, URPDemoPath));
  336. }
  337. private bool IsURPImported()
  338. {
  339. return Directory.Exists(Path.Combine(SGEPath, URPPluginPath));
  340. }
  341. private void ImportURPScenes()
  342. {
  343. ImportPackage(URPDemoPackagePath);
  344. }
  345. private void ImportURPPlugin()
  346. {
  347. ImportPackage(URPPluginPackagePath);
  348. }
  349. private void ImportPackage(string packagePath)
  350. {
  351. string fullPath;
  352. if (!GetInternalFile(packagePath, out fullPath))
  353. return;
  354. AssetDatabase.ImportPackage(fullPath, true);
  355. }
  356. private void DeleteIfFolderExist(string directoryPath)
  357. {
  358. string fullpath = Path.Combine(SGEPath, directoryPath);
  359. if (Directory.Exists(fullpath))
  360. FileUtil.DeleteFileOrDirectory(fullpath);
  361. }
  362. private void CleanEverything()
  363. {
  364. DeleteIfFolderExist(DemoFolderPath);
  365. DeleteIfFolderExist(BasePluginPath);
  366. DeleteIfFolderExist(URPPluginPath);
  367. AssetDatabase.Refresh();
  368. }
  369. }
  370. }