private void OnCheckIgnore() { _ignoreList.Clear(); if (HitManager.GetAllManagers().Count <= 0) { return; } foreach (UISpriteData data in _targetAtlas.spriteList) { HitManager mgr = HitManager.GetManager(data.name); if (mgr != null && mgr.hitTotal > 0) { continue; } foreach (string suffix in _ignoreSuffixs) { if (data.name.EndsWith(suffix)) { string prefix = data.name.Replace(suffix, ""); foreach (HitManager manager in HitManager.GetAllManagers()) { if (manager.hitTotal > 0 && manager.spriteName != data.name && manager.spriteName.StartsWith(prefix)) { _ignoreList.Add(data.name); } } } } } }
private void OnCheckAtlas() { _selectHit = null; _selectSpriteName = ""; HitManager.Clear(); List <Object> prefabList = GetPrefabs(); foreach (Object obj in prefabList) { GameObject go = obj as GameObject; List <UISprite> sprites = PrefabSpriteManager.GetAllSprite(go); foreach (UISprite sprite in sprites) { if (sprite.atlas != _targetAtlas) { continue; } foreach (UISpriteData data in _targetAtlas.spriteList) { HitManager managr = HitManager.GetManager(data.name); if (sprite.spriteName != data.name) { continue; } string path = GameObjectExt.GetHierarchyWithRoot(sprite.transform, go.transform); managr.SetEditPrefab(go); managr.AddHit(path); } } } }
/// <summary> /// 检查全部 /// </summary> private void OnCheckSameFile(List <string> validList = null) { InitSame(); UpdateSizeHitDct(validList); foreach (List <string> list in _size2Filenames.Values) { if (list != null && list.Count > 1) { UpdateHashHitFiles(list, validList); } } HitManager.Clear(); //初始化 PrefabSpriteManager.InitAndCheck(); foreach (List <string> list in _hash2Filenames.Values) { if (list == null || list.Count <= 1) { continue; } foreach (string filename in list) { string filePath = filename; if (!PrefabSpriteManager.pngName2Refs.ContainsKey(filePath)) { continue; } var spriteDataList = PrefabSpriteManager.pngName2Refs[filePath]; HitManager managr = HitManager.GetManager(filePath); foreach (var spriteData in spriteDataList) { managr.SetEditPrefab(spriteData.prefabName, spriteData.uiPath); managr.AddHit(spriteData.spriteUIPath); } } } _sameCount = 0; foreach (List <string> list in _hash2Filenames.Values) { if (list != null && list.Count > 1) { _sameCount++; } } Debug.Log(string.Format("重复数为:{0}", _sameCount)); _refCount = 0; foreach (var manager in HitManager.GetAllManagers()) { _refCount += manager.hitTotal; } Debug.Log(string.Format("UI引用数为:{0}", _refCount)); }
private int GetRefUICount(string filename) { HitManager manager = HitManager.GetManager(filename); return(manager.hitTotal); /* * if (!_filename2UIPaths.ContainsKey(filename)) return 0; * List<string> list = _filename2UIPaths[filename]; * return list.Count; */ }
/// <summary> /// 确认转移 /// </summary> /// <param name="hash">要处理文件的hash值</param> private void CommitResetSame(string hash) { if (_areadyDealList.Contains(hash)) { EditorUtility.DisplayDialog("确认", "此条目已处理", "确认"); return; } if (_replaceAtlas == null || string.IsNullOrEmpty(_replaceName)) { EditorUtility.DisplayDialog("错误", "请设置好目标图集", "确认"); return; } //需要执行的图集 List <string> compileList = new List <string>(); //处理移入一个全新图集的情况 string atlasPath = AssetDatabase.GetAssetPath(_replaceAtlas); bool inEditAtlas = false; foreach (string filename in _hash2Filenames[hash]) { string path = _filename2AtlasPath[filename]; if (path.Equals(atlasPath)) { inEditAtlas = true; break; } } if (!inEditAtlas) { string srcPng = _hash2Filenames[hash][0]; string srcPath = _filename2AtlasPath[srcPng]; string srcFolder = srcPath.Substring(0, srcPath.LastIndexOf("/")); string destPath = atlasPath; string destFolder = atlasPath.Substring(0, atlasPath.LastIndexOf("/")); string destPng = destFolder + "/imgs/" + _replaceName + ".png"; if (File.Exists(destPng)) { EditorUtility.DisplayDialog("错误", "目标图集有同名sprite", "确认"); return; } AssetDatabase.MoveAsset(srcPng, destPng); compileList.Add(destPath); Debug.Log(string.Format("移动{0}到{1}", srcPng, destPng)); } List <string> filenames = _hash2Filenames[hash]; foreach (string filename in filenames) { //忽略 if (_ignoreResetList.Contains(filename)) { Debug.Log("忽略检测:" + filename); continue; } HitManager manager = HitManager.GetManager(filename); foreach (HitData data in manager.hitDataList) { GameObject go = AssetDatabase.LoadMainAssetAtPath(data.prefabPath) as GameObject; if (go == null) { Debug.LogError("找不到prefab:" + data.prefabName); continue; } foreach (string path in data.hitPathList) { var com = go.transform.Find(path); if (com == null) { Debug.LogError(string.Format("{0}找不到对应的SpriteGameObject:{1},请检查", go.name, path)); continue; } var sprite = com.GetComponent <UISprite>(); if (sprite == null) { Debug.LogError(string.Format("{0}找不到对应的Sprite:{1},请检查", go.name, path)); continue; } sprite.atlas = _replaceAtlas; sprite.spriteName = _replaceName; EditorUtility.SetDirty(go); //要添加替换log Debug.Log(string.Format("替换了{0}.prefab的{1}", go.name, path)); } } } //矫正UI.prefab string png = "/imgs/" + _replaceName + ".png"; foreach (string filename in _hash2Filenames[hash]) { if (_ignoreResetList.Contains(filename)) { Debug.Log("忽略移除png:" + filename); continue; } string path = _filename2AtlasPath[filename]; if (path.Equals(atlasPath)) { Debug.Log("本身不做移除操作" + filename); continue; } AssetDatabase.MoveAssetToTrash(filename); Debug.Log("移除png:" + filename); if (!compileList.Contains(path)) { compileList.Add(path); } } //执行tps foreach (string path in compileList) { int index = path.LastIndexOf("/"); int index2 = path.LastIndexOf("."); string folder = path.Substring(0, index); string atlasName = path.Substring(index + 1, index2 - index - 1); DoTexturePacker(folder, atlasName); Debug.Log("执行tps:" + atlasName); } //转移完毕 //正式移除检测 Debug.Log("转移完毕,hash:" + hash); _areadyDealList.Add(hash); _replaceAtlas = null; _replaceName = null; _selFilename = ""; EditorUtility.DisplayDialog("转移结束", "转移结束,记得在编辑器Alt+S保存", "确认"); }
private List <string> _areadyDealList;//已处理的hash /// <summary> /// 重复图集检测 /// </summary> private void OnSpriteCheckGUI() { GUILayout.BeginHorizontal(); _ImgRoot = EditorGUILayout.TextField("根目录:", _ImgRoot, GUILayout.Width(300)); if (GUILayout.Button("检测全部", GUILayout.Height(50f))) { OnCheckSameFile(); } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); _editPrefab = EditorGUILayout.ObjectField("图集:", _editPrefab, typeof(UIAtlas), false, GUILayout.Width(300)) as UIAtlas; if (GUILayout.Button("根据图集检测", GUILayout.Height(50f))) { OnCheckSameFile(_editPrefab); } GUILayout.EndHorizontal(); if (_sameFolderExpand == null) { _sameFolderExpand = new Dictionary <string, bool>(); } if (_selResultExpand == null) { _selResultExpand = new Dictionary <string, bool>(); } if (_ignoreResetList == null) { _ignoreResetList = new List <string>(); } if (_areadyDealList == null) { _areadyDealList = new List <string>(); } if (_sameCount > 0) { EditorGUILayout.BeginVertical("HelpBox", GUILayout.Width(700f)); { GUILayout.Space(10f); GUILayout.Label(string.Format("Result: 重复总条目数:{0} UI引用总数:{1} 已处理条目数:{2}", _sameCount, _refCount, _areadyDealList.Count)); GUILayout.Space(5f); _sameHashScroll = EditorGUILayout.BeginScrollView(_sameHashScroll, GUILayout.MinHeight(350f)); foreach (string hash in _hash2Filenames.Keys) { List <string> filenames = _hash2Filenames[hash]; if (filenames.Count <= 1) { continue; } if (!_sameFolderExpand.ContainsKey(hash)) { _sameFolderExpand.Add(hash, false); } bool bDeal = _areadyDealList.Contains(hash); string name = string.Format("png数:{0} UI引用总数:{1}", filenames.Count, GetRefUICount(filenames)); if (bDeal) { name += " 已处理"; } GUI.backgroundColor = bDeal ? Color.green : Color.white; GUI.color = bDeal ? Color.green : Color.white; if (EditorGUILayout.Foldout(_sameFolderExpand[hash], name)) { //收起 if (_preSelectFolder != null && hash != _preSelectFolder && _sameFolderExpand.ContainsKey(_preSelectFolder)) { _sameFolderExpand[_preSelectFolder] = false; _replaceAtlas = null; _replaceName = null; _selFilename = ""; } _preSelectFolder = hash; _sameFolderExpand[hash] = true; } else { _sameFolderExpand[hash] = false; } GUI.backgroundColor = Color.white; GUI.color = Color.white; if (_sameFolderExpand[hash]) { foreach (var png in filenames) { GUILayout.Space(-1f); GUI.backgroundColor = !png.Equals(_selFilename) ? Color.white : new Color(0.8f, 0.8f, 0.8f); GUILayout.BeginHorizontal("AS TextArea", GUILayout.MinHeight(20f)); GUI.backgroundColor = Color.white; string sText = png.Replace(_ImgRoot + "/", "") + " " + string.Format("UI引用数:{0}", GetRefUICount(png)); //GUILayout.Label(sText); if (GUILayout.Button(sText, "OL TextField", GUILayout.Height(20f))) { _selFilename = png; } if (GUILayout.Button("复制sprite", GUILayout.Width(85f))) { string spriteName = png.Substring(png.LastIndexOf("/") + 1, png.LastIndexOf(".") - png.LastIndexOf("/") - 1); NGUITools.clipboard = spriteName; } GUI.backgroundColor = _ignoreResetList.Contains(png) ? Color.green : Color.white; sText = _ignoreResetList.Contains(png) ? "取消忽略" : "忽略"; if (GUILayout.Button(sText, GUILayout.Width(85f))) { if (_ignoreResetList.Contains(png)) { _ignoreResetList.Remove(png); } else { _ignoreResetList.Add(png); } } GUI.backgroundColor = Color.white; if (GUILayout.Button("设置为目标", GUILayout.Width(100f))) { Debug.Log("图集目录:" + _filename2AtlasPath[png]); _replaceAtlas = AssetDatabase.LoadAssetAtPath(_filename2AtlasPath[png], typeof(UIAtlas)) as UIAtlas; _replaceName = GetSpriteName(png); } GUILayout.EndHorizontal(); } GUILayout.BeginHorizontal(); GUILayout.Space(15f); _replaceAtlas = (UIAtlas)EditorGUILayout.ObjectField("目标图集:", _replaceAtlas, typeof(UIAtlas), false); _replaceName = EditorGUILayout.TextField("spriteName:", _replaceName); if (GUILayout.Button("确认转移", GUILayout.Width(100f))) { CommitResetSame(hash); } GUILayout.EndHorizontal(); } } EditorGUILayout.EndScrollView(); } EditorGUILayout.EndVertical(); if (!string.IsNullOrEmpty(_selFilename)) { var manager = HitManager.GetManager(_selFilename); string sResult = string.Format("UI引用{0} {1}", manager.hitTotal, _selFilename); GUILayout.BeginVertical(); if (EditorHelper.DrawHeader(sResult, "pngResult", false, false)) { _pngResultPos = EditorGUILayout.BeginScrollView(_pngResultPos, GUILayout.MinHeight(120f)); foreach (HitData data in manager.hitDataList) { string keyName = data.prefabName + _selFilename; if (!_selResultExpand.ContainsKey(keyName)) { _selResultExpand.Add(keyName, false); } string sText = data.prefabName + string.Format(" 引用数:{0}", data.hitCount); if (EditorGUILayout.Foldout(_selResultExpand[keyName], sText)) { GUILayout.BeginVertical(); foreach (string path in data.hitPathList) { GUILayout.BeginHorizontal("AS TextArea", GUILayout.MinHeight(20f)); GUILayout.Space(30f); GUILayout.Label(path); GUILayout.EndHorizontal(); } _selResultExpand[keyName] = true; GUILayout.EndVertical(); } else { _selResultExpand[keyName] = false; } } EditorGUILayout.EndScrollView(); } Texture img = AssetDatabase.LoadAssetAtPath(_selFilename, typeof(Texture)) as Texture; GUILayout.Space(20); GUILayout.BeginHorizontal(); GUILayout.Space(60); GUILayout.Box(img); GUILayout.EndHorizontal(); GUILayout.EndVertical(); } } GUILayout.BeginVertical(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.LabelField("只处理图集目录下的imgs目录,以其它名字命名的不处理,比如img"); EditorGUILayout.LabelField("含有忽略的目录名,见_ignoreFolderList列表。"); EditorGUILayout.LabelField("需自行处理的:"); EditorGUILayout.LabelField("1、确定代码中的引用"); EditorGUILayout.LabelField("2、UI.prefab的替换为优化depth,可能是draw call增加"); GUI.color = Color.red; EditorGUILayout.LabelField("3、九宫切图"); GUI.color = Color.white; GUILayout.EndVertical(); }