private void GenerateAtlasSpritePrefab(string atlasDir) { Dictionary <string, string> md5Map = new Dictionary <string, string>(); FileInfo[] resFiles = BMEditUtility.GetAllFiles(atlasDir, "*.*"); for (int j = 0; j < resFiles.Length; j++) { FileInfo info = resFiles[j]; if (info.FullName.EndsWith(".meta", StringComparison.Ordinal) || info.FullName.EndsWith(".txt", StringComparison.Ordinal)) { continue; } string dirName = Path.GetFileName(info.DirectoryName); string outPath = $"{_settings.atlasSpritePrefabDir}{dirName}/"; string spriteFileName = Path.GetFileName(info.FullName); string spriteName = Path.GetFileNameWithoutExtension(info.FullName); string texPath = BMEditUtility.Absolute2Relativity(info.FullName); string md5 = HashHelper.ComputeMD5(texPath); md5Map.Add(texPath, md5); TextureImporter ti = AssetImporter.GetAtPath(texPath) as TextureImporter; if (ti) { if (!Directory.Exists(outPath)) { Directory.CreateDirectory(outPath); } GameObject spritePrefab = new GameObject(spriteName); SpriteRenderer spriteRenderer = spritePrefab.AddComponent <SpriteRenderer>(); Vector4 border = ti.spriteBorder; ti.spritePackingTag = "Atlas_" + dirName; ti.SaveAndReimport(); spriteRenderer.sprite = AssetDatabase.LoadAssetAtPath <Sprite>(texPath);; //spritePrefab.hideFlags = HideFlags.HideInHierarchy; var savePath = outPath + spriteName + ".prefab"; if (File.Exists(savePath)) { File.Delete(savePath); } PrefabUtility.SaveAsPrefabAsset(spritePrefab, savePath); //var texture2D = AssetDatabase.LoadAssetAtPath<Texture2D>(rPath); //var sprite = Sprite.Create(texture2D, new Rect(0,0,texture2D.width,texture2D.height), new Vector2(0.5f,0.5f), 400, 0,SpriteMeshType.FullRect,border); DestroyImmediate(spritePrefab); } } //Save md5 BMEditUtility.SaveDictionary(Path.Combine(atlasDir, "manifest.txt"), md5Map); }
private void UpdatePackingTag(string dir) { FileInfo[] resFiles = BMEditUtility.GetAllFiles(dir, "*.*"); for (int i = 0; i < resFiles.Length; i++) { FileInfo info = resFiles[i]; string rPath = BMEditUtility.Absolute2Relativity(info.FullName); if (Path.GetExtension(rPath) == ".meta") { continue; } string dirName = Path.GetFileName(Path.GetDirectoryName(rPath)); TextureImporter ti = AssetImporter.GetAtPath(rPath) as TextureImporter; if (ti) { ti.spritePackingTag = "UISprite_" + dirName; ti.SaveAndReimport(); } } }
public override void OnInspectorGUI() { base.OnInspectorGUI(); if (!string.IsNullOrEmpty(_settings.scenesVersionFile)) { EditorGUILayout.Space(); foreach (var folder in _settings.scenesFolderList) { FileInfo[] sceneFiles = BMEditUtility.GetAllFiles(folder, _settings.scenesPattern); for (int i = 0; i < sceneFiles.Length; i++) { string path = BMEditUtility.Absolute2Relativity(sceneFiles[i].DirectoryName) + "/" + sceneFiles[i].Name; //相对路径 if (_scenesVersionDict.TryGetValue(path, out string version)) { var oldVer = version; version = EditorGUILayout.TextField(path.Replace(_settings.resDir, ""), version); if (version != oldVer) { _scenesVersionDict[path] = version; BMEditUtility.SaveDictionary(_settings.scenesVersionFile, _scenesVersionDict); } } else { EditorGUILayout.TextField(path.Replace(_settings.resDir, ""), "0"); } } } if (GUILayout.Button("Save Scene Version")) { BMEditUtility.SaveDictionary(_settings.scenesVersionFile, _scenesVersionDict); } //重制所有场景版本号 if (GUILayout.Button("Reset All Scenes Version")) { _scenesVersionDict = new Dictionary <string, string>(); BMEditUtility.SaveDictionary(_settings.scenesVersionFile, _scenesVersionDict); // _settings.sceneVersions = new List<int>(); // _settings.scenePaths = new List<string>(); // serializedObject.ApplyModifiedProperties(); // BMEditUtility.SaveSetting(_settings); } } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Generate Atlas Sprite")) { GenerateAtlasSprite(false); } if (GUILayout.Button("Force Generate Atlas Sprite")) { GenerateAtlasSprite(true); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); //uiSpriteDir _settings.uiSpriteDir = EditorGUILayout.TextField("UI Sprite", _settings.uiSpriteDir); if (!string.IsNullOrEmpty(_settings.uiSpriteDir)) { if (GUILayout.Button("Update All Packing Tag")) { UpdatePackingTag(_settings.uiSpriteDir); } } }
//======================= // 工具函数 //======================= //获取Build信息 static BuildInfo FetchBuildInfo(string resDir, string searchPattern, BuildType buildType) { BuildInfo buildInfo = new BuildInfo(); buildInfo.assetPaths = new List <string>(); FileInfo[] resFiles = BMEditUtility.GetAllFiles(resDir, searchPattern); for (int i = 0; i < resFiles.Length; i++) { FileInfo fileInfo = resFiles[i]; string lowerName = fileInfo.Name.ToLower(); string dirPath = BMEditUtility.Absolute2Relativity(fileInfo.DirectoryName) + "/"; //过滤一些文件 if ((fileInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } if ((fileInfo.Directory.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } if (IgnoreFile(lowerName, dirPath)) { continue; } string path = dirPath + fileInfo.Name; //相对路径 if (buildType == BuildType.Lua) { string temp = path + "." + BMUtility.EncryptWithMD5(path) + ".bytes"; if (File.Exists(temp)) { File.Delete(temp); } if (isLuaEncoded) { if (!EncodeLuaFile(path, temp)) { Debug.Log("Encode lua file fail!"); break; } } else { File.Copy(path, temp); } buildInfo.assetPaths.Add(temp); tempLuaPaths.Add(temp); } else { buildInfo.assetPaths.Add(path); } //Logger.Log("path:{0}", path); EditorUtility.DisplayCancelableProgressBar("Fetch Build Info...", path, (float)(i + 1.0f) / (float)resFiles.Length); } //EditorUtility.ClearProgressBar(); if (buildType == BuildType.Lua) { AssetDatabase.Refresh(); } return(buildInfo); }