Skip to content

Instantly share code, notes, and snippets.

@twsiyuan
Created December 10, 2016 04:44
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save twsiyuan/60e3a6f655d5d0855424b604763f9c05 to your computer and use it in GitHub Desktop.
Script GUIDs remapping (change) in Unity5
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;
}
}
}
@TimmermanV
Copy link

Can I use this code in my own project? What is the license for this code?
Thanks,
Tim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment