Unity 重置项目资源的guid
来源:互联网 发布:数据健康度 编辑:程序博客网 时间:2024/06/10 00:28
有时需要将多个项目的资源合并到一个项目中,但有可能有些资源是共用的,它的guid是一样的,这样合并到一个项目时,可能会超成冲突。
如果要让每个资源的guid都不相同,就需要重新生成guid,并且更新引用。下面是步骤
1. UNITY菜单-> Editor > Project Setting - > Editor
设置 Version Controller 为Visible Meta Files
2. Asset Serialization 设置为 Force Text
3. 下面是代码
using System;using System.Collections.Generic;using System.IO;using UnityEditor;namespace UnityGuidRegenerator { public class UnityGuidRegeneratorMenu { [MenuItem("Tools/Regenerate asset GUIDs")] public static void RegenerateGuids() { if (EditorUtility.DisplayDialog("GUIDs regeneration", "You are going to start the process of GUID regeneration. This may have unexpected results. \n\n MAKE A PROJECT BACKUP BEFORE PROCEEDING!", "Regenerate GUIDs", "Cancel")) { try { AssetDatabase.StartAssetEditing(); string path = Path.GetFullPath(".") + Path.DirectorySeparatorChar + "Assets/_Game";//要更新guid的文件夹 UnityGuidRegenerator regenerator = new UnityGuidRegenerator(path); regenerator.RegenerateGuids(); } finally { AssetDatabase.StopAssetEditing(); EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(); } } } } internal class UnityGuidRegenerator { private static readonly string[] kDefaultFileExtensions = { // "*.meta", // "*.mat", // "*.anim", // "*.prefab", // "*.unity", // "*.asset" "*.*" }; private readonly string _assetsPath; public UnityGuidRegenerator(string assetsPath) { _assetsPath = assetsPath; } public void RegenerateGuids(string[] regeneratedExtensions = null) { if (regeneratedExtensions == null) { regeneratedExtensions = kDefaultFileExtensions; } // Get list of working files List<string> filesPaths = new List<string>(); foreach (string extension in regeneratedExtensions) { filesPaths.AddRange( Directory.GetFiles(_assetsPath, extension, SearchOption.AllDirectories) ); } // Create dictionary to hold old-to-new GUID map Dictionary<string, string> guidOldToNewMap = new Dictionary<string, string>(); Dictionary<string, List<string>> guidsInFileMap = new Dictionary<string, List<string>>(); // We must only replace GUIDs for Resources present in Assets. // Otherwise built-in resources (shader, meshes etc) get overwritten. HashSet<string> ownGuids = new HashSet<string>(); // Traverse all files, remember which GUIDs are in which files and generate new GUIDs int counter = 0; foreach (string filePath in filesPaths) { EditorUtility.DisplayProgressBar("Scanning Assets folder", MakeRelativePath(_assetsPath, filePath), counter / (float) filesPaths.Count); string contents = File.ReadAllText(filePath); IEnumerable<string> guids = GetGuids(contents); bool isFirstGuid = true; foreach (string oldGuid in guids) { // First GUID in .meta file is always the GUID of the asset itself if (isFirstGuid && Path.GetExtension(filePath) == ".meta") { ownGuids.Add(oldGuid); isFirstGuid = false; } // Generate and save new GUID if we haven't added it before if (!guidOldToNewMap.ContainsKey(oldGuid)) { string newGuid = Guid.NewGuid().ToString("N"); guidOldToNewMap.Add(oldGuid, newGuid); } if (!guidsInFileMap.ContainsKey(filePath)) guidsInFileMap[filePath] = new List<string>(); if (!guidsInFileMap[filePath].Contains(oldGuid)) { guidsInFileMap[filePath].Add(oldGuid); } } counter++; } // Traverse the files again and replace the old GUIDs counter = -1; int guidsInFileMapKeysCount = guidsInFileMap.Keys.Count; foreach (string filePath in guidsInFileMap.Keys) { EditorUtility.DisplayProgressBar("Regenerating GUIDs", MakeRelativePath(_assetsPath, filePath), counter / (float) guidsInFileMapKeysCount); counter++; string contents = File.ReadAllText(filePath); foreach (string oldGuid in guidsInFileMap[filePath]) { if (!ownGuids.Contains(oldGuid)) continue; string newGuid = guidOldToNewMap[oldGuid]; if (string.IsNullOrEmpty(newGuid)) throw new NullReferenceException("newGuid == null"); contents = contents.Replace("guid: " + oldGuid, "guid: " + newGuid); } File.WriteAllText(filePath, contents); } EditorUtility.ClearProgressBar(); } private static IEnumerable<string> GetGuids(string text) { const string guidStart = "guid: "; const int guidLength = 32; int textLength = text.Length; int guidStartLength = guidStart.Length; List<string> guids = new List<string>(); int index = 0; while (index + guidStartLength + guidLength < textLength) { index = text.IndexOf(guidStart, index, StringComparison.Ordinal); if (index == -1) break; index += guidStartLength; string guid = text.Substring(index, guidLength); index += guidLength; if (IsGuid(guid)) { guids.Add(guid); } } return guids; } private static bool IsGuid(string text) { for (int i = 0; i < text.Length; i++) { char c = text[i]; if ( !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) ) return false; } return true; } private static string MakeRelativePath(string fromPath, string toPath) { Uri fromUri = new Uri(fromPath); Uri toUri = new Uri(toPath); Uri relativeUri = fromUri.MakeRelativeUri(toUri); string relativePath = Uri.UnescapeDataString(relativeUri.ToString()); return relativePath; } }}
阅读全文
0 0
- Unity 重置项目资源的guid
- 【Unity资源】(完整项目/样板)
- Unity GUID替换Editor
- Visual Studio项目类型的Guid列表
- 常见Visual Studio项目类型的GUID
- 常见Visual Studio项目类型的GUID
- 学习unity的资源
- Unity项目资源加载与管理
- Ubuntu 重置unity
- unity动画重置
- 如何重置Ubuntu的Unity、启动图标和Compiz
- unity-场景重置,游戏对象的初始化改变以及复制
- unity老版本的一个游戏5.5版本重置
- Unity资源之间的关系
- unity默认资源的文件夹
- Unity 获得资源的缩略图
- Unity美术资源的优化
- unity小白的资源
- 二叉树的三种遍历非递归实现
- webService-cxf(3)
- CyrusOne公司的诺沃克数据中心
- 第七篇 窗口系统机制之KEYGUARD机制
- 论文发表费用价格
- Unity 重置项目资源的guid
- 深入理解python super
- Django中配置异常显示页面==路由
- SpringBoot学习-(十九)SpringBoot定时器#Schedule
- ThoughtWorks 2018校园招聘作业题目 -- 出租车
- 去除ios移动端alert/confirm的网址(url)
- C++ primer第五版“Sale_item.h”头文件
- Zookeeper数据与存储
- Warm up:C input and output