Created
December 10, 2016 04:44
Star
You must be signed in to star a gist
Script GUIDs remapping (change) in Unity5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Text.RegularExpressions; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
using UnityEditor; | |
public static class Utility | |
{ | |
[MenuItem("Utility/RemapScriptGuid")] | |
public static void RemapScriptGuid() | |
{ | |
var workingPath = Path.GetFullPath(Application.dataPath + "/App"); | |
var prefix = Path.GetFileNameWithoutExtension(EditorUtility.SaveFilePanel("Type prefix", string.Empty, "00", "hex")).ToLower(); | |
var prefixRegex = new Regex("[a-fA-F0-9]{1,15}"); | |
if (string.IsNullOrEmpty(prefix)) | |
{ | |
return; | |
} | |
else if (!prefixRegex.Match(prefix).Success) | |
{ | |
EditorUtility.DisplayDialog("Remapping GUID", string.Format("Prefix '{0}' is not valid.", prefix), "OK"); | |
return; | |
} | |
// Get script meta | |
var mappings = new List<ScriptMapping>(); | |
var scriptMetas = Directory.GetFiles(workingPath, "*.cs.meta", SearchOption.AllDirectories); | |
var guidRegx = new Regex("guid:\\s?([a-fA-F0-9]+)"); | |
foreach (var scriptMeta in scriptMetas) | |
{ | |
var metaContent = File.ReadAllText(scriptMeta); | |
var match = guidRegx.Match(metaContent); | |
if (match.Success) | |
{ | |
var guid = match.Groups[1].Value; | |
var mapping = new ScriptMapping(scriptMeta, guid); | |
mappings.Add(mapping); | |
} | |
} | |
// Assign new Guids | |
foreach (var mapping in mappings) | |
{ | |
var newGuid = prefix + mapping.OldGuid.Substring(prefix.Length); | |
if (newGuid == mapping.OldGuid) | |
{ | |
continue; | |
} | |
// Check new Guids is exist? | |
if (!string.IsNullOrEmpty(AssetDatabase.GUIDToAssetPath(newGuid))) | |
{ | |
while (true) | |
{ | |
// Generate new | |
newGuid = System.Guid.NewGuid().ToString("N"); | |
newGuid = prefix + newGuid.Substring(prefix.Length); | |
if (string.IsNullOrEmpty(AssetDatabase.GUIDToAssetPath(newGuid))) | |
{ | |
break; | |
} | |
} | |
} | |
mapping.NewGuid = newGuid; | |
} | |
// Find resources amd replace GUIDs | |
// *.assets ScriptableObject | |
// *unity Scene | |
// *.prefab Prefab | |
// *.controller Animator | |
// *.cs.meta Scripts | |
AssetDatabase.StartAssetEditing(); | |
var assets = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".assets") || s.EndsWith(".unity") || s.EndsWith(".prefab") || s.EndsWith(".controller") || s.EndsWith(".cs.meta")); | |
var assetsCount = assets.Count(); | |
var assetsIndex = 0; | |
foreach (var asset in assets) | |
{ | |
EditorUtility.DisplayProgressBar("Remapping", string.Format("Current: {0}", Path.GetFileName(asset)), Mathf.InverseLerp(0, assetsCount, assetsIndex)); | |
var content = File.ReadAllText(asset); | |
var replaced = false; | |
foreach (var mapping in mappings) | |
{ | |
if (mapping.NewGuid == null) | |
{ | |
continue; | |
} | |
var oldGuid = string.Format("guid: {0}", mapping.OldGuid); | |
var newGuid = string.Format("guid: {0}", mapping.NewGuid); | |
if (oldGuid.IndexOf(oldGuid) >= 0) | |
{ | |
content = content.Replace(oldGuid, newGuid); | |
replaced = true; | |
} | |
} | |
if (replaced) | |
{ | |
File.WriteAllText(asset, content); | |
} | |
assetsIndex += 1; | |
} | |
EditorUtility.ClearProgressBar(); | |
AssetDatabase.StopAssetEditing(); | |
AssetDatabase.Refresh(ImportAssetOptions.Default); | |
} | |
class ScriptMapping | |
{ | |
public ScriptMapping(string path, string guid) | |
{ | |
this.Path = path; | |
this.OldGuid = guid; | |
this.NewGuid = null; | |
} | |
public string Path | |
{ | |
get; | |
set; | |
} | |
public string OldGuid | |
{ | |
get; | |
set; | |
} | |
public string NewGuid | |
{ | |
get; | |
set; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can I use this code in my own project? What is the license for this code?
Thanks,
Tim