SampleSetup.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Unity.RenderStreaming.Editor;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace Unity.RenderStreaming.Samples
  8. {
  9. [InitializeOnLoad]
  10. class SampleSetup
  11. {
  12. private const string kSavePath = "Library/RenderStreamingSampleSettings.json";
  13. private static string cacheGuid = "";
  14. class DeleteSampleSettings : UnityEditor.AssetModificationProcessor
  15. {
  16. // When SampleSetup script is deleted, also delete RenderStreamingSampleSettings.json.
  17. private static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions options)
  18. {
  19. var existPath = AssetDatabase.GUIDToAssetPath(cacheGuid);
  20. if (existPath.StartsWith(assetPath))
  21. {
  22. File.Delete(kSavePath);
  23. }
  24. return AssetDeleteResult.DidNotDelete;
  25. }
  26. }
  27. static SampleSetup()
  28. {
  29. cacheGuid = AssetDatabase.FindAssets($"t:Script {nameof(SampleSetup)}")[0];
  30. Load();
  31. if (s_Settings.dialogAlreadyShowOnStartup || !RenderStreaming.AutomaticStreaming)
  32. {
  33. return;
  34. }
  35. const string dialogText =
  36. "It is recommended to turn off AutomaticStreaming in the scenes included in the sample. Do you want to change the config assets for Sample?";
  37. if (EditorUtility.DisplayDialog("Warning", dialogText, "Change Settings", "Ignore"))
  38. {
  39. var guids = AssetDatabase.FindAssets("t:RenderStreamingSettings");
  40. var path = guids.Select(AssetDatabase.GUIDToAssetPath).First(x => x.EndsWith("RenderStreamingSample.asset"));
  41. var asset = AssetDatabase.LoadAssetAtPath<RenderStreamingSettings>(path);
  42. if (asset != null)
  43. {
  44. RenderStreamingEditor.SetRenderStreamingSettings(asset);
  45. }
  46. else
  47. {
  48. Debug.LogError("RenderStreamingSample.asset not found.");
  49. }
  50. }
  51. s_Settings.dialogAlreadyShowOnStartup = true;
  52. Save();
  53. }
  54. [Serializable]
  55. private struct SerializedState
  56. {
  57. public bool dialogAlreadyShowOnStartup;
  58. }
  59. private static SerializedState s_Settings;
  60. private static void Load()
  61. {
  62. s_Settings = new SerializedState();
  63. if (!File.Exists(kSavePath))
  64. return;
  65. try
  66. {
  67. var json = File.ReadAllText(kSavePath);
  68. s_Settings = JsonUtility.FromJson<SerializedState>(json);
  69. }
  70. catch
  71. {
  72. s_Settings = new SerializedState();
  73. }
  74. }
  75. private static void Save()
  76. {
  77. var json = JsonUtility.ToJson(s_Settings, prettyPrint: true);
  78. File.WriteAllText(kSavePath, json);
  79. }
  80. }
  81. }