Saver.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. using System.IO;
  8. using System;
  9. namespace LunarCatsStudio.SuperCombiner
  10. {
  11. /// <summary>
  12. /// Saver.
  13. /// This class is responsible for saving the combined resources in disk
  14. /// </summary>
  15. public class Saver
  16. {
  17. #if UNITY_EDITOR
  18. /// <summary>
  19. /// Saves the textures.
  20. /// </summary>
  21. /// <param name="combinedIndex">Combined _index.</param>
  22. /// <param name="folderDestination">Folder destination.</param>
  23. /// <param name="sessionName">Session name.</param>
  24. /// <param name="texturePacker">Texture packer.</param>
  25. public static void SaveTextures(int combinedIndex, string folderDestination, string sessionName, TexturePacker texturePacker)
  26. {
  27. // UI Progress bar display in Editor
  28. EditorUtility.DisplayProgressBar("Super Combiner", "Saving textures ...", 0.3f);
  29. if (!Directory.Exists(folderDestination + "/Textures"))
  30. {
  31. Directory.CreateDirectory(folderDestination + "/Textures");
  32. }
  33. texturePacker.SaveTextures(folderDestination, sessionName);
  34. Logger.Instance.AddLog("SuperCombiner", "Textures saved in '" + folderDestination + "/Textures/'");
  35. }
  36. /// <summary>
  37. /// Saves the _material.
  38. /// </summary>
  39. /// <param name="combinedIndex">Combined _index.</param>
  40. public static void SaveMaterial(int combinedIndex, string folderDestination, string sessionName, TexturePacker texturePacker)
  41. {
  42. // UI Progress bar display in Editor
  43. EditorUtility.DisplayProgressBar("Super Combiner", "Saving materials ...", 0.6f);
  44. if (!Directory.Exists(folderDestination + "/Materials"))
  45. {
  46. Directory.CreateDirectory(folderDestination + "/Materials");
  47. }
  48. Material materialToSave = AssetDatabase.LoadAssetAtPath<Material>(folderDestination + "/Materials/" + texturePacker._copyedMaterials.name + ".mat");
  49. if (materialToSave == null)
  50. {
  51. AssetDatabase.CreateAsset(texturePacker._copyedToSaveMaterials, folderDestination + "/Materials/" + texturePacker._copyedMaterials.name + ".mat");
  52. AssetDatabase.SaveAssets();
  53. AssetDatabase.Refresh();
  54. }
  55. else
  56. {
  57. EditorUtility.CopySerialized(texturePacker._copyedToSaveMaterials, materialToSave);
  58. }
  59. Material material = (Material)(AssetDatabase.LoadAssetAtPath(folderDestination + "/Materials/" + texturePacker._copyedMaterials.name + ".mat", typeof(Material)));
  60. foreach (KeyValuePair<string, Texture2D> keyValue in texturePacker._packedTextures)
  61. {
  62. if (keyValue.Value != null)
  63. {
  64. string textureName = keyValue.Key;
  65. texturePacker.TexturePropertyNames.TryGetValue(keyValue.Key, out textureName);
  66. material.SetTexture(keyValue.Key, (Texture2D)(AssetDatabase.LoadAssetAtPath(texturePacker.GetTextureFilePathName(folderDestination, sessionName, textureName, combinedIndex), typeof(Texture2D))));
  67. }
  68. }
  69. Logger.Instance.AddLog("SuperCombiner", "Materials saved in '" + folderDestination + "/Materials/'");
  70. }
  71. /// <summary>
  72. /// Saves the prefabs.
  73. /// </summary>
  74. public static void SavePrefabs(List<GameObject> toSavePrefabList, List<Mesh> toSaveMeshList, string folderDestination, string sessionName)
  75. {
  76. // UI Progress bar display in Editor
  77. EditorUtility.DisplayProgressBar("Super Combiner", "Saving Prefabs ...", 0.7f);
  78. if (!Directory.Exists(folderDestination + "/Prefabs"))
  79. {
  80. Directory.CreateDirectory(folderDestination + "/Prefabs");
  81. }
  82. // We have to save meshes first
  83. SaveMeshes(toSaveMeshList, folderDestination, sessionName);
  84. for (int i = 0; i < toSavePrefabList.Count; i++)
  85. {
  86. if (AssetDatabase.LoadAssetAtPath<GameObject>(folderDestination + "/Prefabs/" + sessionName + "_" + toSavePrefabList[i].name + ".prefab"))
  87. {
  88. // Asset already exist! We just have to update it
  89. GameObject asset = AssetDatabase.LoadAssetAtPath<GameObject>(folderDestination + "/Prefabs/" + sessionName + "_" + toSavePrefabList[i].name + ".prefab");
  90. asset.transform.position = toSavePrefabList[i].transform.position;
  91. asset.transform.localScale = toSavePrefabList[i].transform.localScale;
  92. asset.transform.rotation = toSavePrefabList[i].transform.rotation;
  93. AssetDatabase.SaveAssets();
  94. AssetDatabase.Refresh();
  95. }
  96. else
  97. {
  98. // This is a new asset, create it
  99. PrefabUtility.SaveAsPrefabAsset(toSavePrefabList[i], folderDestination + "/Prefabs/" + sessionName + "_" + toSavePrefabList[i].name + ".prefab");
  100. }
  101. }
  102. Logger.Instance.AddLog("SuperCombiner", "Prefabs saved in '" + folderDestination + "/Prefabs/'");
  103. }
  104. /// <summary>
  105. /// Saves the meshes.
  106. /// </summary>
  107. public static void SaveMeshes(List<Mesh> toSaveMeshList, string folderDestination, string sessionName)
  108. {
  109. // UI Progress bar display in Editor
  110. EditorUtility.DisplayProgressBar("Super Combiner", "Saving Meshes ...", 0.75f);
  111. if (!Directory.Exists(folderDestination + "/Meshes"))
  112. {
  113. Directory.CreateDirectory(folderDestination + "/Meshes");
  114. }
  115. // Check if all meshes have different name. This is important not to override a previously saved mesh
  116. HashSet<string> meshNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
  117. for (int i = 0; i < toSaveMeshList.Count; i++)
  118. {
  119. if (!meshNames.Contains(toSaveMeshList[i].name))
  120. {
  121. meshNames.Add(toSaveMeshList[i].name);
  122. }
  123. else
  124. {
  125. // A mesh with the same name has been found, rename it
  126. for (int n = 0; n < 9999; n++)
  127. {
  128. if (!meshNames.Contains(toSaveMeshList[i].name + "(" + n + ")"))
  129. {
  130. meshNames.Add(toSaveMeshList[i].name + "(" + n + ")");
  131. toSaveMeshList[i].name = toSaveMeshList[i].name + "(" + n + ")";
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. for (int i = 0; i < toSaveMeshList.Count; i++)
  138. {
  139. Mesh dummy = AssetDatabase.LoadAssetAtPath<Mesh>(folderDestination + "/Meshes/" + sessionName + "_" + toSaveMeshList[i].name + ".asset");
  140. if (dummy == null)
  141. {
  142. // This is a new mesh to create
  143. AssetDatabase.CreateAsset(toSaveMeshList[i], folderDestination + "/Meshes/" + sessionName + "_" + toSaveMeshList[i].name + ".asset");
  144. }
  145. else
  146. {
  147. // The mesh already exists, just uptade it
  148. dummy.Clear();
  149. EditorUtility.CopySerialized(toSaveMeshList[i], dummy);
  150. }
  151. AssetDatabase.SaveAssets();
  152. AssetDatabase.Refresh();
  153. }
  154. Logger.Instance.AddLog("SuperCombiner", "Meshes saved in '" + folderDestination + "/Meshes/'");
  155. }
  156. /// <summary>
  157. /// Saves the meshes object.
  158. /// </summary>
  159. public static void SaveMeshesObj(List<GameObject> combinedGameObjectFromMeshList, List<GameObject> combinedGameObjectFromSkinnedMeshList, string folderDestination)
  160. {
  161. // UI Progress bar display in Editor
  162. EditorUtility.DisplayProgressBar("Super Combiner", "Saving obj ...", 0.8f);
  163. if (!Directory.Exists(folderDestination + "/Objs"))
  164. {
  165. Directory.CreateDirectory(folderDestination + "/Objs");
  166. }
  167. for (int i = 0; i < combinedGameObjectFromMeshList.Count; i++)
  168. {
  169. LunarCatsStudio.SuperCombiner.ObjSaver.SaveObjFile(combinedGameObjectFromMeshList[i], false, folderDestination + "/Objs");
  170. }
  171. for (int i = 0; i < combinedGameObjectFromSkinnedMeshList.Count; i++)
  172. {
  173. LunarCatsStudio.SuperCombiner.ObjSaver.SaveObjFile(combinedGameObjectFromSkinnedMeshList[i], false, folderDestination + "/Objs");
  174. }
  175. }
  176. /// <summary>
  177. /// Saves the meshes fbx.
  178. /// </summary>
  179. public static void SaveMeshesFbx(string folderDestination)
  180. {
  181. // UI Progress bar display in Editor
  182. EditorUtility.DisplayProgressBar("Super Combiner", "Saving fbx ...", 0.9f);
  183. if (!Directory.Exists(folderDestination + "/Fbx"))
  184. {
  185. Directory.CreateDirectory(folderDestination + "/Fbx");
  186. }
  187. // TODO : save mesh to fbx !
  188. }
  189. /// <summary>
  190. /// Saves the combined results.
  191. /// </summary>
  192. public static void SaveCombinedResults(CombinedResult combinedResult, string folderDestination, string sessionName)
  193. {
  194. CombinedResult savedCombinedResult = AssetDatabase.LoadAssetAtPath<CombinedResult>(folderDestination + "/CombinedResults_" + sessionName + ".asset");
  195. if (savedCombinedResult != null)
  196. {
  197. AssetDatabase.DeleteAsset(folderDestination + "/CombinedResults_" + sessionName + ".asset");
  198. }
  199. CombinedResult toSaveCombinedResult = GameObject.Instantiate(combinedResult) as CombinedResult;
  200. for (int i = 0; i < combinedResult._combinedMaterials.Count; i++)
  201. {
  202. if (combinedResult._combinedMaterials[i].material != null)
  203. {
  204. toSaveCombinedResult._combinedMaterials[i].material = (Material)(AssetDatabase.LoadAssetAtPath(folderDestination + "/Materials/" + combinedResult._combinedMaterials[i].material.name + ".mat", typeof(Material)));
  205. }
  206. }
  207. string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(folderDestination + "/CombinedResults_" + sessionName + ".asset");
  208. AssetDatabase.CreateAsset(toSaveCombinedResult, assetPathAndName);
  209. AssetDatabase.SaveAssets();
  210. AssetDatabase.Refresh();
  211. }
  212. /// <summary>
  213. /// Saves the combined settings.
  214. /// </summary>
  215. public static void SaveCombinedSettings(SuperCombinerSettings combinedSettings, string folderDestination, string sessionName)
  216. {
  217. SuperCombinerSettings savedCombinedSettings = AssetDatabase.LoadAssetAtPath<SuperCombinerSettings>(folderDestination + "/CombinedSettings_" + sessionName + ".asset");
  218. if (savedCombinedSettings != null)
  219. {
  220. AssetDatabase.DeleteAsset(folderDestination + "/CombinedSettings_" + sessionName + ".asset");
  221. }
  222. string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(folderDestination + "/CombinedSettings_" + sessionName + ".asset");
  223. SuperCombinerSettings toSaveCombinedSettings = GameObject.Instantiate(combinedSettings) as SuperCombinerSettings;
  224. toSaveCombinedSettings.generalSettings = combinedSettings.generalSettings;
  225. toSaveCombinedSettings.textureSettings = combinedSettings.textureSettings;
  226. toSaveCombinedSettings.meshSettings = combinedSettings.meshSettings;
  227. toSaveCombinedSettings.materialSettings = combinedSettings.materialSettings;
  228. AssetDatabase.CreateAsset(toSaveCombinedSettings, assetPathAndName);
  229. AssetDatabase.SaveAssets();
  230. AssetDatabase.Refresh();
  231. }
  232. #endif
  233. }
  234. }