Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

URP2DConverterUtility.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.IO;
  5. using UnityEditor.SceneManagement;
  6. using UnityEngine.SceneManagement;
  7. using static UnityEditor.Rendering.AnimationClipUpgrader;
  8. internal static class URP2DConverterUtility
  9. {
  10. public static bool IsPSB(string path)
  11. {
  12. if (string.IsNullOrEmpty(path))
  13. throw new ArgumentNullException(nameof(path));
  14. if (path.StartsWith("Packages"))
  15. return false;
  16. return path.EndsWith(".psb") || path.EndsWith(".psd");
  17. }
  18. public static bool IsMaterialPath(string path, string id)
  19. {
  20. if (string.IsNullOrEmpty(path))
  21. throw new ArgumentNullException(nameof(path));
  22. if (path.StartsWith("Packages"))
  23. return false;
  24. if (path.EndsWith(".mat"))
  25. return URP2DConverterUtility.DoesFileContainString(path, new string[] { id });
  26. return false;
  27. }
  28. public static bool IsPrefabOrScenePath(string path, string[] ids)
  29. {
  30. if (string.IsNullOrEmpty(path))
  31. throw new ArgumentNullException(nameof(path));
  32. if (path.StartsWith("Packages"))
  33. return false;
  34. if (path.EndsWith(".prefab", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".unity", StringComparison.OrdinalIgnoreCase))
  35. return URP2DConverterUtility.DoesFileContainString(path, ids);
  36. return false;
  37. }
  38. public static bool IsPrefabOrScenePath(string path, string id)
  39. {
  40. return IsPrefabOrScenePath(path, new string[] { id });
  41. }
  42. public static bool DoesFileContainString(string path, string[] strs)
  43. {
  44. if (strs != null && strs.Length > 0)
  45. {
  46. using (StreamReader file = File.OpenText(path))
  47. {
  48. string line;
  49. while ((line = file.ReadLine()) != null)
  50. {
  51. for (int i = 0; i < strs.Length; i++)
  52. {
  53. if (line.Contains(strs[i]))
  54. return true;
  55. }
  56. }
  57. }
  58. }
  59. return false;
  60. }
  61. public static string UpgradePSB(string path)
  62. {
  63. AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate);
  64. return string.Empty;
  65. }
  66. public static string UpgradePrefab(string path, Action<GameObject> objectUpgrader)
  67. {
  68. UnityEngine.Object[] objects = AssetDatabase.LoadAllAssetsAtPath(path);
  69. int firstIndex = 0;
  70. for (int i = 0; i < objects.Length; i++)
  71. {
  72. if (objects[i] as GameObject)
  73. {
  74. firstIndex = i;
  75. break;
  76. }
  77. }
  78. // There should be no need to check this as we have already determined that there is something that needs upgrading
  79. if (!PrefabUtility.IsPartOfImmutablePrefab(objects[firstIndex]))
  80. {
  81. for (int objIndex = 0; objIndex < objects.Length; objIndex++)
  82. {
  83. GameObject go = objects[objIndex] as GameObject;
  84. if (go != null)
  85. {
  86. objectUpgrader(go);
  87. }
  88. }
  89. GameObject asset = objects[firstIndex] as GameObject;
  90. PrefabUtility.SavePrefabAsset(asset.transform.root.gameObject);
  91. return string.Empty;
  92. }
  93. return "Unable to modify an immutable prefab";
  94. }
  95. public static void UpgradeScene(string path, Action<GameObject> objectUpgrader)
  96. {
  97. Scene scene = new Scene();
  98. bool openedByUser = false;
  99. for (int i = 0; i < SceneManager.sceneCount && !openedByUser; i++)
  100. {
  101. scene = SceneManager.GetSceneAt(i);
  102. if (path == scene.path)
  103. openedByUser = true;
  104. }
  105. if (!openedByUser)
  106. scene = EditorSceneManager.OpenScene(path, OpenSceneMode.Additive);
  107. GameObject[] gameObjects = scene.GetRootGameObjects();
  108. foreach (GameObject go in gameObjects)
  109. objectUpgrader(go);
  110. EditorSceneManager.SaveScene(scene);
  111. if (!openedByUser)
  112. EditorSceneManager.CloseScene(scene, true);
  113. }
  114. public static void UpgradeMaterial(string path, Shader oldShader, Shader newShader)
  115. {
  116. Material material = AssetDatabase.LoadAssetAtPath<Material>(path);
  117. if (material.shader == oldShader)
  118. material.shader = newShader;
  119. GUID guid = AssetDatabase.GUIDFromAssetPath(path);
  120. AssetDatabase.SaveAssetIfDirty(guid);
  121. }
  122. public static string GetObjectIDString(UnityEngine.Object obj)
  123. {
  124. string guid;
  125. long localId;
  126. if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj.GetInstanceID(), out guid, out localId))
  127. return "fileID: " + localId + ", guid: " + guid;
  128. return null;
  129. }
  130. }