/** * pass in System.IO.File.WriteAllBytes for parameter fileSaveFunction. This is necessary because on Web Player file saving * functions only exist for Editor classes */ public void SaveAtlasToAssetDatabase(Texture2D atlas, string texPropertyName, int atlasNum, Material resMat) { string prefabPth = AssetDatabase.GetAssetPath(resMat); if (prefabPth == null || prefabPth.Length == 0) { Debug.LogError("Could save atlas. Could not find result material in AssetDatabase."); return; } string baseName = Path.GetFileNameWithoutExtension(prefabPth); string folderPath = prefabPth.Substring(0, prefabPth.Length - baseName.Length - 4); string fullFolderPath = Application.dataPath + folderPath.Substring("Assets".Length, folderPath.Length - "Assets".Length); string pth = fullFolderPath + baseName + "-" + texPropertyName + "-atlas" + atlasNum + ".png"; Debug.Log("Created atlas for: " + texPropertyName + " at " + pth); //need to create a copy because sometimes the packed atlases are not in ARGB32 format Texture2D newTex = MB_Utility.createTextureCopy(atlas); int size = Mathf.Max(newTex.height, newTex.width); byte[] bytes = newTex.EncodeToPNG(); Editor.DestroyImmediate(newTex); System.IO.File.WriteAllBytes(pth, bytes); AssetDatabase.Refresh(); string relativePath = folderPath + baseName + "-" + texPropertyName + "-atlas" + atlasNum + ".png"; SetTextureSize((Texture2D)(AssetDatabase.LoadAssetAtPath(relativePath, typeof(Texture2D))), size); SetMaterialTextureProperty(resMat, texPropertyName, relativePath); }
internal Texture2D _createTextureCopy(Texture2D t) { Texture2D tx = MB_Utility.createTextureCopy(t); _temporaryTextures.Add(tx); return(tx); }
internal Texture2D _createTextureCopy(string propertyName, Texture2D t) { Texture2D tx = MB_Utility.createTextureCopy(t); tx.name = string.Format("tmpCopy{0}_{1}x{2}", _temporaryTextures.Count, tx.width, tx.height); TemporaryTexture txx = new TemporaryTexture(propertyName, tx); _temporaryTextures.Add(txx); return(tx); }
/** * pass in System.IO.File.WriteAllBytes for parameter fileSaveFunction. This is necessary because on Web Player file saving * functions only exist for Editor classes */ public void SaveAtlasToAssetDatabase(Texture2D atlas, ShaderTextureProperty texPropertyName, int atlasNum, Material resMat) { if (atlas == null) { SetMaterialTextureProperty(resMat, texPropertyName, null); } else { string prefabPth = AssetDatabase.GetAssetPath(resMat); if (prefabPth == null || prefabPth.Length == 0) { Debug.LogError("Could save atlas. Could not find result material in AssetDatabase."); return; } string baseName = Path.GetFileNameWithoutExtension(prefabPth); string folderPath = prefabPth.Substring(0, prefabPth.Length - baseName.Length - 4); string fullFolderPath = Application.dataPath + folderPath.Substring("Assets".Length, folderPath.Length - "Assets".Length); string pth = fullFolderPath + baseName + "-" + texPropertyName.name + "-atlas" + atlasNum; string relativePath = folderPath + baseName + "-" + texPropertyName.name + "-atlas" + atlasNum; //need to create a copy because sometimes the packed atlases are not in ARGB32 format Texture2D newTex = MB_Utility.createTextureCopy(atlas); int size = Mathf.Max(newTex.height, newTex.width); if (SAVE_FORMAT == saveTextureFormat.png) { pth += ".png"; relativePath += ".png"; byte[] bytes = newTex.EncodeToPNG(); System.IO.File.WriteAllBytes(pth, bytes); } else { pth += ".tga"; relativePath += ".tga"; if (File.Exists(pth)) { File.Delete(pth); } //Create the file. FileStream fs = File.Create(pth); MB_TGAWriter.Write(newTex.GetPixels(), newTex.width, newTex.height, fs); } Editor.DestroyImmediate(newTex); AssetDatabase.Refresh(); Debug.Log(String.Format("Wrote atlas for {0} to file:{1}", texPropertyName.name, pth)); Texture2D txx = (Texture2D)(AssetDatabase.LoadAssetAtPath(relativePath, typeof(Texture2D))); SetTextureSize(txx, size); SetMaterialTextureProperty(resMat, texPropertyName, relativePath); } }