PackageImportSettings.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.IO;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEngine.Rendering;
  5. namespace Nfynt.Editor
  6. {
  7. [InitializeOnLoad]
  8. public class PackageImportSettings
  9. {
  10. static PackageImportSettings()
  11. {
  12. RefreshPackage();
  13. }
  14. public static void RefreshPackage()
  15. {
  16. if (Application.isPlaying) return;
  17. //Copy video resource to streaming assets for test
  18. string srcDir = "Packages/com.nfynt.wvp/Samples~/Resource";
  19. string destDir = Application.streamingAssetsPath;
  20. DirectoryCopy(srcDir, destDir, false);
  21. //Always include Nfynt/TextureBlit shader
  22. //AddAlwaysIncludedShader("");
  23. }
  24. private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
  25. {
  26. // Get the subdirectories for the specified directory.
  27. DirectoryInfo dir = new DirectoryInfo(sourceDirName);
  28. if (!dir.Exists)
  29. {
  30. //Debug.Log("Source directory does not exist or could not be found: " + sourceDirName);
  31. return;
  32. }
  33. DirectoryInfo[] dirs = dir.GetDirectories();
  34. // If the destination directory doesn't exist, create it.
  35. Directory.CreateDirectory(destDirName);
  36. // Get the files in the directory and copy them to the new location.
  37. FileInfo[] files = dir.GetFiles();
  38. foreach (FileInfo file in files)
  39. {
  40. if (file.Extension == ".meta")
  41. continue;
  42. string tempPath = Path.Combine(destDirName, file.Name);
  43. file.CopyTo(tempPath, true);
  44. }
  45. // If copying subdirectories, copy them and their contents to new location.
  46. if (copySubDirs)
  47. {
  48. foreach (DirectoryInfo subdir in dirs)
  49. {
  50. string tempPath = Path.Combine(destDirName, subdir.Name);
  51. DirectoryCopy(subdir.FullName, tempPath, copySubDirs);
  52. }
  53. }
  54. }
  55. public static void AddAlwaysIncludedShader(string shaderName)
  56. {
  57. Shader shader = Shader.Find(shaderName);
  58. if (shader == null)
  59. return;
  60. GraphicsSettings gSettings = AssetDatabase.LoadAssetAtPath<GraphicsSettings>("ProjectSettings/GraphicsSettings.asset");
  61. SerializedObject so = new SerializedObject(gSettings);
  62. SerializedProperty arrayProp = so.FindProperty("m_AlwaysIncludedShaders");
  63. bool hasShader = false;
  64. for (int i = 0; i < arrayProp.arraySize; ++i)
  65. {
  66. SerializedProperty arrayElem = arrayProp.GetArrayElementAtIndex(i);
  67. if (shader == arrayElem.objectReferenceValue)
  68. {
  69. hasShader = true;
  70. break;
  71. }
  72. }
  73. if (!hasShader)
  74. {
  75. int arrayIndex = arrayProp.arraySize;
  76. arrayProp.InsertArrayElementAtIndex(arrayIndex);
  77. SerializedProperty arrayElem = arrayProp.GetArrayElementAtIndex(arrayIndex);
  78. arrayElem.objectReferenceValue = shader;
  79. so.ApplyModifiedProperties();
  80. AssetDatabase.SaveAssets();
  81. }
  82. }
  83. }
  84. }