//public Texture2D LoadTexture( string path ) //{//载入纹理(支持载入磁盘任意位置的纹理) // _TouchTempDir(); // string fileName = Path.GetFileName(path); // string extension = Path.GetExtension(path); // string fileNameWithoutext = Path.GetFileNameWithoutExtension(path); // string zoomedName = null; // bool isNeedRename = false; // TempTextureInfo retTexInfo = null; // //是纹理文件 // if (!IsEnableTextureFile(fileName)) // { // return null; // } // //文件必需存在 // if (!File.Exists(path)) // { // if( textureCache.ContainsKey(path)) // { // textureCache.Remove(path); // EditorUtility.UnloadUnusedAssets(); // } // if (_IsTextureAssetAlreadyExistsInTempFolder(fileName, out isNeedRename)) // { // AssetDatabase.DeleteAsset(UIAtlasEditorConfig.TempPath + fileName); // AssetDatabase.DeleteAsset(UIAtlasEditorConfig.TempPath + m_zoomStr + extension); // } // return null; // } // bool needCopy = false; // if (_IsTextureAssetAlreadyExistsInTempFolder(path, out isNeedRename)) // {//纹理资源已经存在于缓存文件夹中 // if(!_IsTextureAssetSameModTime(path)) // { // needCopy = true; // } // } // else // { // needCopy = true; // } // if (needCopy) // { // if (isNeedRename) // { // fileName = fileNameWithoutext + "副本" + extension; // zoomedName = fileNameWithoutext + "副本" + m_zoomStr + extension; // } // else // { // zoomedName = fileNameWithoutext + m_zoomStr + extension; // } // UniversalEditorUtility.MakeFileWriteable(UIAtlasEditorConfig.AbsTempPath + fileName); // File.Copy(path, UIAtlasEditorConfig.AbsTempPath + fileName, true); // UniversalEditorUtility.MakeFileWriteable(UIAtlasEditorConfig.AbsTempPath + zoomedName); // File.Copy(path, UIAtlasEditorConfig.AbsTempPath + zoomedName, true); // AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); // } // if (!textureCache.ContainsKey(path)) // {//还未载入内存 // AssetDatabase.ImportAsset(UIAtlasEditorConfig.TempPath + fileName); // MakeTextureReadable(UIAtlasEditorConfig.TempPath + fileName, false); // TempTextureInfo newTexInfo = new TempTextureInfo(); // newTexInfo.SourcePath = path; // newTexInfo.TempPath = UIAtlasEditorConfig.TempPath + fileName; // newTexInfo.TempPathZoom = UIAtlasEditorConfig.TempPath + zoomedName; // newTexInfo.Texture = AssetDatabase.LoadAssetAtPath(newTexInfo.TempPath, typeof(Texture)) as Texture2D; // AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); // if (newTexInfo.Texture != null) // { // newTexInfo.ZoomTexture = newTexInfo.Texture; // textureCache.Add(path, newTexInfo); // } // } // if (textureCache.ContainsKey(path)) // { // textureCache.TryGetValue(path, out retTexInfo); // } // return retTexInfo.ZoomTexture; //} public bool UnloadTexture(string spritePath, string atlasPath) { bool bRet = true; if (string.IsNullOrEmpty(spritePath) || string.IsNullOrEmpty(atlasPath)) { return(false); } TempAtlasTextureInfo tempAtlasTexture = null; TempTextureInfo tempSpriteTexture = null; if (m_AllTextureCache.ContainsKey(atlasPath)) { if (m_AllTextureCache.TryGetValue(atlasPath, out tempAtlasTexture)) { if (tempAtlasTexture.SpriteTextureInfo.ContainsKey(spritePath)) { if (tempAtlasTexture.SpriteTextureInfo.TryGetValue(spritePath, out tempSpriteTexture)) { AssetDatabase.DeleteAsset(tempSpriteTexture.TempPath); AssetDatabase.DeleteAsset(tempSpriteTexture.TempPathZoom); tempAtlasTexture.SpriteTextureInfo.Remove(spritePath); } } } } return(bRet); }
private string CreateAtlasTempDir(string atlasPath) { if (string.IsNullOrEmpty(atlasPath)) { return(null); } string tempAtlasDir = string.Empty; //string tempAbsAtlasDir = string.Empty; tempAtlasDir = GetAtlasTempDir(atlasPath); if (string.IsNullOrEmpty(tempAtlasDir)) { TempAtlasTextureInfo tempAtlasTexture = new TempAtlasTextureInfo(); string atlasName = Path.GetFileNameWithoutExtension(atlasPath); string strGUID = Guid.NewGuid().ToString(); //tempAbsAtlasDir = UIAtlasEditorConfig.AbsTempPath + atlasName + "_" + strGUID + "/"; tempAtlasDir = UIAtlasEditorConfig.TempPath + atlasName + "_" + strGUID + "/"; tempAtlasTexture.TempAtlasDir = tempAtlasDir; m_AllTextureCache.Add(atlasPath, tempAtlasTexture); } if (!Directory.Exists(tempAtlasDir)) { Directory.CreateDirectory(tempAtlasDir); } return(tempAtlasDir); }
//public Texture2D GetSpriteTexture(string path) //{//获取原始纹理 // Texture2D tex = null; // if (path == null) // { // return null; // } // foreach (var textureInfo in textureCache) // { // if (textureInfo.Key == path) // { // tex = textureInfo.Value.Texture; // break; // } // } // return tex; //} public Texture2D GetSpriteZoomTexture(string spritePath, string atlasPath) { Texture2D tex = null; if (string.IsNullOrEmpty(spritePath) || string.IsNullOrEmpty(atlasPath)) { return(null); } TempAtlasTextureInfo tempAtlasTexture = null; TempTextureInfo tempSpriteTexture = null; if (m_AllTextureCache.ContainsKey(atlasPath)) { if (m_AllTextureCache.TryGetValue(atlasPath, out tempAtlasTexture)) { if (tempAtlasTexture.SpriteTextureInfo.ContainsKey(spritePath)) { if (tempAtlasTexture.SpriteTextureInfo.TryGetValue(spritePath, out tempSpriteTexture)) { tex = tempSpriteTexture.ZoomTexture; } } } } return(tex); }
private bool IsSpriteInfoInCache(string spritePath, string atlasPath, out TempTextureInfo spriteInfo) { spriteInfo = null; if (string.IsNullOrEmpty(spritePath) || string.IsNullOrEmpty(atlasPath)) { return(false); } bool bRet = false; TempAtlasTextureInfo tempAtlasTexture = null; TempTextureInfo tempSpriteTexture = null; if (m_AllTextureCache.ContainsKey(atlasPath)) { if (m_AllTextureCache.TryGetValue(atlasPath, out tempAtlasTexture)) { if (tempAtlasTexture.SpriteTextureInfo.ContainsKey(spritePath)) { if (tempAtlasTexture.SpriteTextureInfo.TryGetValue(spritePath, out tempSpriteTexture)) { spriteInfo = tempSpriteTexture; } bRet = true; } } } return(bRet); }
//public void Update() //{//更新全部纹理 // foreach( var texInfo in textureCache ) // { // LoadTexture(texInfo.Key); // } //} public void Update() { foreach (var textureItem in m_AllTextureCache) { TempAtlasTextureInfo textureInfo = null; if (m_AllTextureCache.TryGetValue(textureItem.Key, out textureInfo)) { foreach (var spriteItem in textureInfo.SpriteTextureInfo) { LoadTexture(spriteItem.Key, textureItem.Key, spriteItem.Value.ZoomScale); } } } }
private string GetAtlasTempDir(string atlasPath) { string tempAtlasDir = string.Empty; TempAtlasTextureInfo tempAtlasTexture = null; if (m_AllTextureCache.ContainsKey(atlasPath)) { if (m_AllTextureCache.TryGetValue(atlasPath, out tempAtlasTexture)) { tempAtlasDir = tempAtlasTexture.TempAtlasDir; } } return(tempAtlasDir); }
private bool IsSpriteInTempFloder(string spritePath, string atlasPath, out bool isNeedRename) { isNeedRename = false; if (string.IsNullOrEmpty(spritePath) || string.IsNullOrEmpty(atlasPath)) { return(false); } bool bRet = false; string fileName = Path.GetFileName(spritePath); TempAtlasTextureInfo tempAtlasTexture = null; //该Sprite对应Atlas文件夹不存在 if (!m_AllTextureCache.ContainsKey(atlasPath)) { isNeedRename = false; return(false); } if (m_AllTextureCache.TryGetValue(atlasPath, out tempAtlasTexture)) { if (File.Exists(tempAtlasTexture.TempAtlasDir + fileName)) { isNeedRename = true; bRet = false; foreach (var spriteTexture in tempAtlasTexture.SpriteTextureInfo) { if (spriteTexture.Key == spritePath) { bRet = true; isNeedRename = false; break; } } } else { isNeedRename = false; bRet = false; } } return(bRet); }
private void AddSpriteInCache(string spritePath, string atlasPath, TempTextureInfo spriteInfo) { if (string.IsNullOrEmpty(spritePath) || string.IsNullOrEmpty(atlasPath)) { return; } TempAtlasTextureInfo tempAtlasTexture = null; if (m_AllTextureCache.ContainsKey(atlasPath)) { if (m_AllTextureCache.TryGetValue(atlasPath, out tempAtlasTexture)) { if (!tempAtlasTexture.SpriteTextureInfo.ContainsKey(spritePath)) { tempAtlasTexture.SpriteTextureInfo.Add(spritePath, spriteInfo); } } } }
public void Clear(string atlasPath) { if (string.IsNullOrEmpty(atlasPath)) { return; } TempAtlasTextureInfo tempAtlasTexture = null; //List<string> assetPaths = new List<string>(); if (m_AllTextureCache.ContainsKey(atlasPath)) { if (m_AllTextureCache.TryGetValue(atlasPath, out tempAtlasTexture)) { //foreach (var spriteInfo in tempAtlasTexture.SpriteTextureInfo) //{ // string fileName = Path.GetFileName(spriteInfo.Key); // assetPaths.Add(tempAtlasTexture.TempAtlasDir + fileName); //} //foreach (var path in assetPaths) //{ // AssetDatabase.DeleteAsset(path); //} tempAtlasTexture.SpriteTextureInfo.Clear(); //删除临时文件夹 if (Directory.Exists(tempAtlasTexture.TempAtlasDir)) { DirectoryInfo info = new DirectoryInfo(tempAtlasTexture.TempAtlasDir); DeleteFileByDirectory(info); AssetDatabase.DeleteAsset(tempAtlasTexture.TempAtlasDir); } } } }
//public Texture2D GetSpriteZoomTexture(string path) //{//获取缩放纹理 // Texture2D tex = null; // if(path == null) // { // return null; // } // foreach (var textureInfo in textureCache) // { // if (textureInfo.Key == path) // { // tex = textureInfo.Value.ZoomTexture; // break; // } // } // return tex; //} public List <Texture2D> GetAllSpriteZoomTexture2D(string atlasPath) {//获取Atlas的全部Sprite压缩纹理(2D) if (string.IsNullOrEmpty(atlasPath)) { return(null); } List <Texture2D> textureList = new List <Texture2D>(); TempAtlasTextureInfo tempAtlasTexture = null; if (m_AllTextureCache.ContainsKey(atlasPath)) { if (m_AllTextureCache.TryGetValue(atlasPath, out tempAtlasTexture)) { foreach (KeyValuePair <string, TempTextureInfo> spriteInfo in tempAtlasTexture.SpriteTextureInfo) { textureList.Add(spriteInfo.Value.ZoomTexture); } } } return(textureList); }
//private void _TouchTempDir(string atlasPath) //{ // //查看缓存文件夹是否存在 // if (!Directory.Exists(atlasPath)) // { // Directory.CreateDirectory(atlasPath); // } //} private void DeleteUnuseAsset(string spritePath, string atlasPath) { if (string.IsNullOrEmpty(spritePath) || string.IsNullOrEmpty(atlasPath)) { return; } bool isNeedRename = false; string fileName = Path.GetFileName(spritePath); string extension = Path.GetExtension(spritePath); TempAtlasTextureInfo tempAtlasTexture = null; if (m_AllTextureCache.ContainsKey(atlasPath)) { if (m_AllTextureCache.TryGetValue(atlasPath, out tempAtlasTexture)) { if (tempAtlasTexture.SpriteTextureInfo.ContainsKey(spritePath)) { tempAtlasTexture.SpriteTextureInfo.Remove(spritePath); #if (UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9) EditorUtility.UnloadUnusedAssets(); #else EditorUtility.UnloadUnusedAssetsImmediate(); #endif } } if (IsSpriteInTempFloder(spritePath, atlasPath, out isNeedRename)) { AssetDatabase.DeleteAsset(tempAtlasTexture.TempAtlasDir + fileName); AssetDatabase.DeleteAsset(tempAtlasTexture.TempAtlasDir + m_zoomStr + extension); } } }