static UInt64 GetPersistentPrefabOrVariantFileIdentifier(GameObject gameObject) { var handle = PrefabUtility.GetPrefabInstanceHandle(gameObject); if (handle != null) { return(Unsupported.GetLocalIdentifierInFileForPersistentObject(handle)); } return(Unsupported.GetLocalIdentifierInFileForPersistentObject(gameObject)); }
static void DoFixMissingScript() { if ((brokenAsset == null || brokenAsset.Count == 0) && (brokenScene == null || brokenScene.Count == 0)) { Debug.Log("No prefab / scene to fix"); return; } if (brokenAsset != null) { int count = 0; foreach (var prefabPath in brokenAsset) { count++; EditorUtility.DisplayProgressBar("Loading", "Fixing prefabs & assets", brokenAsset.Count / count); var projectAbsPath = Path.GetDirectoryName(Application.dataPath); var prefabAbsPath = projectAbsPath + "/" + prefabPath; string result = null; using (var streamReader = new StreamReader(prefabAbsPath)) { string scriptLine = ""; var text = streamReader.ReadToEnd(); var lines = text.Split('\n'); for (int i = 0; i < lines.Length; i++) { var line = lines[i]; if (line.StartsWith("---")) { scriptLine = ""; } if (line.StartsWith(" m_Script:")) { scriptLine = line; } if (scriptLine.Length > 0) { if (line.StartsWith(" m_Script:")) { var token = line.Split(','); try { int identifierIndex = 0; int guidIndex = 0; string identifier = null; string assetGuid = null; for (int x = 0; x < token.Length; x++) { var str = token[x]; if (str.StartsWith(" m_Script: {fileID:")) { identifierIndex = x; identifier = str.Replace(" m_Script: {fileID:", "").Trim(); } if (str.StartsWith(" guid:")) { guidIndex = x; assetGuid = str.Replace(" guid: ", ""); } } if (identifier != null && assetGuid != null) { if (string.IsNullOrEmpty(AssetDatabase.GUIDToAssetPath(assetGuid))) { foreach (var a in missingMap) { if (a.Key.Equals(assetGuid) && a.Value.ContainsKey(identifier) && TypeSerializer.Deserialize(a.Value[identifier], false) != null) { Type t = TypeSerializer.Deserialize(a.Value[identifier], false); if (t == null) { continue; } MonoScript m = null; foreach (var s in uNodeEditorUtility.MonoScripts) { if (s.GetClass() == t) { m = s; break; } } if (m == null) { continue; } string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m)); if (!string.IsNullOrEmpty(guid)) { token[identifierIndex] = token[identifierIndex].Replace(identifier, ((long)Unsupported.GetLocalIdentifierInFileForPersistentObject(m)).ToString()); token[guidIndex] = token[guidIndex].Replace(assetGuid, guid); break; } break; } } } if (string.IsNullOrEmpty(AssetDatabase.GUIDToAssetPath(assetGuid))) { foreach (var a in missingMap) { if (a.Key.Equals(assetGuid)) { if (a.Value.ContainsKey(identifier) && TypeSerializer.Deserialize(a.Value[identifier], false) != null) { } } } } lines[i] = string.Join(",", token); } } catch { } } } } result = string.Join("\n", lines); } if (!string.IsNullOrEmpty(result)) { File.WriteAllText(prefabAbsPath, result); AssetDatabase.Refresh(); } } } if (brokenScene != null) { int count = 0; foreach (var scene in brokenScene) { count++; EditorUtility.DisplayProgressBar("Loading", "Fixing scenes", brokenScene.Count / count); var p = AssetDatabase.GetAssetPath(scene); if (string.IsNullOrEmpty(p)) { continue; } var projectAbsPath = Path.GetDirectoryName(Application.dataPath); var prefabAbsPath = projectAbsPath + "/" + p; string result = null; using (var streamReader = new StreamReader(prefabAbsPath)) { var text = streamReader.ReadToEnd(); var lines = text.Split('\n'); string scriptLine = ""; for (int i = 0; i < lines.Length; i++) { var line = lines[i]; if (line.StartsWith("---")) { scriptLine = ""; } if (line.StartsWith("MonoBehaviour:")) { scriptLine = line; } if (scriptLine.Length > 0) { if (line.Trim().StartsWith("m_Script:")) { var token = line.Split(','); try { int identifierIndex = 0; int guidIndex = 0; string identifier = null; string assetGuid = null; for (int x = 0; x < token.Length; x++) { var str = token[x]; if (str.StartsWith(" m_Script: {fileID:")) { identifierIndex = x; identifier = str.Replace(" m_Script: {fileID:", "").Trim(); } if (str.StartsWith(" guid:")) { guidIndex = x; assetGuid = str.Replace(" guid: ", ""); } } if (identifier != null && assetGuid != null) { if (string.IsNullOrEmpty(AssetDatabase.GUIDToAssetPath(assetGuid))) { foreach (var a in missingMap) { if (a.Key.Equals(assetGuid) && a.Value.ContainsKey(identifier) && TypeSerializer.Deserialize(a.Value[identifier], false) != null) { Type t = TypeSerializer.Deserialize(a.Value[identifier], false); if (t == null) { continue; } MonoScript m = null; foreach (var s in uNodeEditorUtility.MonoScripts) { if (s.GetClass() == t) { m = s; break; } } if (m == null) { continue; } string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m)); if (!string.IsNullOrEmpty(guid)) { token[identifierIndex] = token[identifierIndex].Replace(identifier, ((long)Unsupported.GetLocalIdentifierInFileForPersistentObject(m)).ToString()); token[guidIndex] = token[guidIndex].Replace(assetGuid, guid); break; } break; } } } if (string.IsNullOrEmpty(AssetDatabase.GUIDToAssetPath(assetGuid))) { foreach (var a in missingMap) { if (a.Key.Equals(assetGuid)) { if (a.Value.ContainsKey(identifier) && TypeSerializer.Deserialize(a.Value[identifier], false) != null) { } } } } lines[i] = string.Join(",", token); } } catch { } } } } result = string.Join("\n", lines); } if (!string.IsNullOrEmpty(result)) { File.WriteAllText(prefabAbsPath, result); AssetDatabase.Refresh(); } } } missingMap = null; EditorUtility.ClearProgressBar(); }