//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); }
//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 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); }
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 bool UnloadTexture(string path) //{//卸载纹理 // bool bRet = true; // if(path == null) // { // return false; // } // foreach (var textureInfo in textureCache) // { // if (textureInfo.Key == path) // { // AssetDatabase.DeleteAsset(textureInfo.Value.TempPath); // AssetDatabase.DeleteAsset(textureInfo.Value.TempPathZoom); // textureCache.Remove(path); // bRet = true; // break; // } // } // return bRet; //} public Texture2D ZoomTexture(string spritePath, string atlasPath, float scaleFactor) {//缩放纹理 Texture2D tex = null; TempTextureInfo spriteInfo = null; if ((string.IsNullOrEmpty(spritePath)) || (string.IsNullOrEmpty(atlasPath)) || ((-0.000001f < scaleFactor) && (scaleFactor < 0.000001f))) { return(null); } if (IsSpriteInfoInCache(spritePath, atlasPath, out spriteInfo)) { spriteInfo.ZoomTexture = ScaleTextureBilinear(spriteInfo.Texture, scaleFactor); spriteInfo.ZoomScale = scaleFactor; byte[] bytes = spriteInfo.ZoomTexture.EncodeToPNG(); string newPath = Path.GetFileNameWithoutExtension(spriteInfo.TempPath); string extension = Path.GetExtension(spriteInfo.TempPath); newPath = GetAtlasTempDir(atlasPath) + newPath + m_zoomStr + extension; UniversalEditorUtility.MakeFileWriteable(newPath); System.IO.File.WriteAllBytes(newPath, bytes); bytes = null; AssetDatabase.ImportAsset(newPath); MakeTextureReadable(newPath, false); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); spriteInfo.ZoomTexture = AssetDatabase.LoadAssetAtPath(newPath, typeof(Texture)) as Texture2D; tex = spriteInfo.ZoomTexture; } return(tex); }
public Texture2D LoadTexture(string spritePath, string atlasPath, float scaleFactor) { if ( string.IsNullOrEmpty(spritePath) || string.IsNullOrEmpty(atlasPath) || ((-0.000001f < scaleFactor) && (scaleFactor < 0.000001f)) ) { return(null); } _TouchTempDir(); string tempAtlasDir = CreateAtlasTempDir(atlasPath); if (string.IsNullOrEmpty(tempAtlasDir)) { return(null); } TempTextureInfo spriteInfo = null; string fileName = Path.GetFileName(spritePath); string extension = Path.GetExtension(spritePath); string fileNameWithoutext = Path.GetFileNameWithoutExtension(spritePath); string zoomedName = string.Empty; bool isNeedRename = false; bool needCopy = false; if (!IsEnableTextureFile(fileName)) { return(null); } if (!File.Exists(spritePath)) { DeleteUnuseAsset(spritePath, atlasPath); return(null); } if (IsSpriteInTempFloder(spritePath, atlasPath, out isNeedRename)) { if (!IsSouceSpriteFileNoChange(spritePath, atlasPath)) { needCopy = true; } } else { needCopy = true; } if (needCopy) { if (isNeedRename) { fileName = fileNameWithoutext + "副本" + extension; zoomedName = fileNameWithoutext + "副本" + m_zoomStr + extension; } else { zoomedName = fileNameWithoutext + m_zoomStr + extension; } string absTempAtlasDir = EditorHelper.GetProjectPath() + tempAtlasDir; UniversalEditorUtility.MakeFileWriteable(absTempAtlasDir + fileName); File.Copy(spritePath, absTempAtlasDir + fileName, true); UniversalEditorUtility.MakeFileWriteable(absTempAtlasDir + zoomedName); File.Copy(spritePath, absTempAtlasDir + zoomedName, true); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); } //TempTextureInfo retTexInfo = null; if (!IsSpriteInfoInCache(spritePath, atlasPath, out spriteInfo)) {//还未载入内存 AssetDatabase.ImportAsset(tempAtlasDir + fileName); MakeTextureReadable(tempAtlasDir + fileName, false); TempTextureInfo newTexInfo = new TempTextureInfo(); newTexInfo.SourcePath = spritePath; newTexInfo.TempPath = tempAtlasDir + fileName; newTexInfo.TempPathZoom = tempAtlasDir + zoomedName; newTexInfo.Texture = AssetDatabase.LoadAssetAtPath(newTexInfo.TempPath, typeof(Texture)) as Texture2D; newTexInfo.ZoomScale = scaleFactor; AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); if (newTexInfo.Texture != null) { newTexInfo.ZoomTexture = newTexInfo.Texture; AddSpriteInCache(spritePath, atlasPath, newTexInfo); if (scaleFactor != 1.0f) { ZoomTexture(spritePath, atlasPath, scaleFactor); } } } return(GetSpriteTexture(spritePath, atlasPath)); }