/** * 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 = MeshBakerUtility.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); } UnityEditor.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); } }