UnityGuidRegenerator.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //using System;
  2. //using System.Collections.Generic;
  3. //using System.IO;
  4. //using UnityEditor;
  5. //namespace UnityGuidRegenerator
  6. //{
  7. // public class UnityGuidRegeneratorMenu
  8. // {
  9. // [MenuItem("Tools/Regenerate asset GUIDs")]
  10. // public static void RegenerateGuids()
  11. // {
  12. // if (EditorUtility.DisplayDialog("GUIDs regeneration",
  13. // "You are going to start the process of GUID regeneration. This may have unexpected results. \n\n MAKE A PROJECT BACKUP BEFORE PROCEEDING!",
  14. // "Regenerate GUIDs", "Cancel"))
  15. // {
  16. // try
  17. // {
  18. // AssetDatabase.StartAssetEditing();
  19. // string path = Path.GetFullPath(".") + Path.DirectorySeparatorChar + "Assets";//要更新guid的文件夹
  20. // UnityGuidRegenerator regenerator = new UnityGuidRegenerator(path);
  21. // regenerator.RegenerateGuids();
  22. // }
  23. // finally
  24. // {
  25. // AssetDatabase.StopAssetEditing();
  26. // EditorUtility.ClearProgressBar();
  27. // AssetDatabase.Refresh();
  28. // }
  29. // }
  30. // }
  31. // }
  32. // internal class UnityGuidRegenerator
  33. // {
  34. // private static readonly string[] kDefaultFileExtensions = {
  35. // // "*.meta",
  36. // // "*.mat",
  37. // // "*.anim",
  38. // // "*.prefab",
  39. // // "*.unity",
  40. // // "*.asset"
  41. // "*.*"
  42. // };
  43. // private readonly string _assetsPath;
  44. // public UnityGuidRegenerator(string assetsPath)
  45. // {
  46. // _assetsPath = assetsPath;
  47. // }
  48. // public void RegenerateGuids(string[] regeneratedExtensions = null)
  49. // {
  50. // if (regeneratedExtensions == null)
  51. // {
  52. // regeneratedExtensions = kDefaultFileExtensions;
  53. // }
  54. // // Get list of working files
  55. // List<string> filesPaths = new List<string>();
  56. // foreach (string extension in regeneratedExtensions)
  57. // {
  58. // filesPaths.AddRange(
  59. // Directory.GetFiles(_assetsPath, extension, SearchOption.AllDirectories)
  60. // );
  61. // }
  62. // // Create dictionary to hold old-to-new GUID map
  63. // Dictionary<string, string> guidOldToNewMap = new Dictionary<string, string>();
  64. // Dictionary<string, List<string>> guidsInFileMap = new Dictionary<string, List<string>>();
  65. // // We must only replace GUIDs for Resources present in Assets.
  66. // // Otherwise built-in resources (shader, meshes etc) get overwritten.
  67. // HashSet<string> ownGuids = new HashSet<string>();
  68. // // Traverse all files, remember which GUIDs are in which files and generate new GUIDs
  69. // int counter = 0;
  70. // foreach (string filePath in filesPaths)
  71. // {
  72. // EditorUtility.DisplayProgressBar("Scanning Assets folder", MakeRelativePath(_assetsPath, filePath), counter / (float)filesPaths.Count);
  73. // string contents = File.ReadAllText(filePath);
  74. // IEnumerable<string> guids = GetGuids(contents);
  75. // bool isFirstGuid = true;
  76. // foreach (string oldGuid in guids)
  77. // {
  78. // // First GUID in .meta file is always the GUID of the asset itself
  79. // if (isFirstGuid && Path.GetExtension(filePath) == ".meta")
  80. // {
  81. // ownGuids.Add(oldGuid);
  82. // isFirstGuid = false;
  83. // }
  84. // // Generate and save new GUID if we haven't added it before
  85. // if (!guidOldToNewMap.ContainsKey(oldGuid))
  86. // {
  87. // string newGuid = Guid.NewGuid().ToString("N");
  88. // guidOldToNewMap.Add(oldGuid, newGuid);
  89. // }
  90. // if (!guidsInFileMap.ContainsKey(filePath))
  91. // guidsInFileMap[filePath] = new List<string>();
  92. // if (!guidsInFileMap[filePath].Contains(oldGuid))
  93. // {
  94. // guidsInFileMap[filePath].Add(oldGuid);
  95. // }
  96. // }
  97. // counter++;
  98. // }
  99. // // Traverse the files again and replace the old GUIDs
  100. // counter = -1;
  101. // int guidsInFileMapKeysCount = guidsInFileMap.Keys.Count;
  102. // foreach (string filePath in guidsInFileMap.Keys)
  103. // {
  104. // EditorUtility.DisplayProgressBar("Regenerating GUIDs", MakeRelativePath(_assetsPath, filePath), counter / (float)guidsInFileMapKeysCount);
  105. // counter++;
  106. // string contents = File.ReadAllText(filePath);
  107. // foreach (string oldGuid in guidsInFileMap[filePath])
  108. // {
  109. // if (!ownGuids.Contains(oldGuid))
  110. // continue;
  111. // string newGuid = guidOldToNewMap[oldGuid];
  112. // if (string.IsNullOrEmpty(newGuid))
  113. // throw new NullReferenceException("newGuid == null");
  114. // contents = contents.Replace("guid: " + oldGuid, "guid: " + newGuid);
  115. // }
  116. // File.WriteAllText(filePath, contents);
  117. // }
  118. // EditorUtility.ClearProgressBar();
  119. // }
  120. // private static IEnumerable<string> GetGuids(string text)
  121. // {
  122. // const string guidStart = "guid: ";
  123. // const int guidLength = 32;
  124. // int textLength = text.Length;
  125. // int guidStartLength = guidStart.Length;
  126. // List<string> guids = new List<string>();
  127. // int index = 0;
  128. // while (index + guidStartLength + guidLength < textLength)
  129. // {
  130. // index = text.IndexOf(guidStart, index, StringComparison.Ordinal);
  131. // if (index == -1)
  132. // break;
  133. // index += guidStartLength;
  134. // string guid = text.Substring(index, guidLength);
  135. // index += guidLength;
  136. // if (IsGuid(guid))
  137. // {
  138. // guids.Add(guid);
  139. // }
  140. // }
  141. // return guids;
  142. // }
  143. // private static bool IsGuid(string text)
  144. // {
  145. // for (int i = 0; i < text.Length; i++)
  146. // {
  147. // char c = text[i];
  148. // if (
  149. // !((c >= '0' && c <= '9') ||
  150. // (c >= 'a' && c <= 'z'))
  151. // )
  152. // return false;
  153. // }
  154. // return true;
  155. // }
  156. // private static string MakeRelativePath(string fromPath, string toPath)
  157. // {
  158. // Uri fromUri = new Uri(fromPath);
  159. // Uri toUri = new Uri(toPath);
  160. // Uri relativeUri = fromUri.MakeRelativeUri(toUri);
  161. // string relativePath = Uri.UnescapeDataString(relativeUri.ToString());
  162. // return relativePath;
  163. // }
  164. // }
  165. //}