/// <summary> /// Exports the texture to the selected folder /// </summary> /// <returns>The texture.</returns> /// <param name="exportedTexture">Exported texture.</param> /// <param name="textureName">Texture name.</param> /// <param name="exportPath">Export path.</param> /// <param name="isNormal">If set to <c>true</c> the texture is imported as a normal map.</param> private Texture ExportTexture(Texture exportedTexture, string textureName, string exportPath, bool isNormal) { using (System.IO.FileStream f = new System.IO.FileStream(exportPath + textureName + ".png", System.IO.FileMode.OpenOrCreate)) { using (System.IO.BinaryWriter b = new System.IO.BinaryWriter(f)) { b.Write(((Texture2D)exportedTexture).EncodeToPNG()); } } AssetPostprocessor proc = new AssetPostprocessor(); AssetDatabase.Refresh(); proc.assetPath = _exportPath + textureName + ".png"; TextureImporter importer = proc.assetImporter as TextureImporter; importer.anisoLevel = _textureAtlasProperties.anisoLevel; importer.normalmap = isNormal; importer.filterMode = _textureAtlasProperties.filterMode; importer.isReadable = _textureAtlasProperties.readableTexture; importer.maxTextureSize = _textureAtlasProperties.maxTextureSize; importer.wrapMode = _textureAtlasProperties.wrapMode; AssetDatabase.ImportAsset(_exportPath + textureName + ".png", ImportAssetOptions.ForceUpdate); exportedTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(_exportPath + textureName + ".png", typeof(Texture2D)); AssetDatabase.Refresh(); return(exportedTexture); }
void ReimportNonReadonlyTextures(IEnumerable <Material> allMaterials) { IEnumerable <Texture> allTextures = allMaterials.SelectMany(X => ShaderPropertyUtility.GetAllTextures(X)).Distinct(); AssetPostprocessor postProcessor = new AssetPostprocessor(); float currentCount = 0.0f; int targetCount = allTextures.Count(); foreach (Texture t in allTextures) { EditorUtility.DisplayProgressBar("Turning Off Readonly", "Analyzing texture " + ((int)currentCount + 1).ToString() + " of " + targetCount.ToString(), currentCount / targetCount); currentCount += 1; string assetPath = AssetDatabase.GetAssetPath(t); if (assetPath != null) { postProcessor.assetPath = assetPath; TextureImporter importer = (TextureImporter)postProcessor.assetImporter; if (importer != null && !importer.isReadable) { importer.isReadable = true; importer.textureFormat = TextureImporterFormat.ARGB32; AssetDatabase.ImportAsset(assetPath); AssetDatabase.Refresh(); } } } EditorUtility.ClearProgressBar(); }
/// <summary> /// Exports the combined meshes to the folder selected in the Editor Window /// </summary> /// <param name="realAssetPath">The real asset path to be used by System.IO</param> private void ExportMeshes(string realAssetPath) { foreach (MeshFilter mf in _hiddenCombinedObject.GetComponentsInChildren <MeshFilter>()) { MeshExporter.MeshToFile(mf.sharedMesh, realAssetPath + mf.name.Replace('.', '_') + ".obj"); AssetPostprocessor proc = new AssetPostprocessor(); AssetDatabase.Refresh(); proc.assetPath = AssetDatabase.GetAssetPath(_exportFolder) + "/" + mf.name.Replace('.', '_') + ".obj"; ModelImporter importer = proc.assetImporter as ModelImporter; importer.generateSecondaryUV = _isGeneratingLightmapUVs; importer.generateAnimations = ModelImporterGenerateAnimations.None; importer.optimizeMesh = true; importer.importMaterials = false; AssetDatabase.ImportAsset(_exportPath + mf.name.Replace('.', '_') + ".obj", ImportAssetOptions.ForceUpdate); mf.sharedMesh = (Mesh)AssetDatabase.LoadAssetAtPath(_exportPath + mf.name.Replace('.', '_') + ".obj", typeof(Mesh)); AssetDatabase.DeleteAsset(_exportPath + "/Materials/" + mf.sharedMesh.name); if (AssetDatabase.LoadAllAssetsAtPath(_exportPath + "/Materials/").Length == 0) { FileUtil.DeleteFileOrDirectory(_exportPath + "/Materials"); } } AssetDatabase.Refresh(); PrefabUtility.CreatePrefab(_exportPath + "CombinedObject.prefab", _hiddenCombinedObject, ReplacePrefabOptions.ConnectToPrefab); }
void ReimportNonReadableMeshes(IEnumerable <Mesh> allMeshes) { allMeshes = allMeshes.Distinct(); AssetPostprocessor postProcessor = new AssetPostprocessor(); float currentCount = 0.0f; int targetCount = allMeshes.Count(); foreach (Mesh m in allMeshes) { EditorUtility.DisplayProgressBar("Turning Off Readonly", "Analyzing mesh " + ((int)currentCount + 1).ToString() + " of " + targetCount.ToString(), currentCount / targetCount); currentCount += 1; string assetPath = AssetDatabase.GetAssetPath(m); if (assetPath != null) { postProcessor.assetPath = assetPath; ModelImporter importer = (ModelImporter)postProcessor.assetImporter; if (importer != null && !importer.isReadable) { importer.isReadable = true; AssetDatabase.ImportAsset(assetPath); AssetDatabase.Refresh(); } } } EditorUtility.ClearProgressBar(); }
/// <summary> /// This function is for post processing project imported audio assets. /// </summary> /// <param name="importConfiguration">Takes in the import configuration asset.</param> /// <param name="assetPostprocessor">Takes in the asset postprocessor class.</param> public static void ProcessAudioAsset(ConfigurationAsset importConfiguration, AssetPostprocessor assetPostprocessor) { // Trying to modify the audio importer settings. try { // Getting the audio asset impoprter settings var audioImporter = assetPostprocessor.assetImporter as AudioImporter; // Getting the default audio importer sample settings from the new audio imported. AudioImporterSampleSettings audioConfiguration = audioImporter.defaultSampleSettings; // Applying the configured audio settings data to the imported audio asset. audioConfiguration.loadType = importConfiguration.AudioImportConfiguration.LoadType; audioConfiguration.sampleRateSetting = importConfiguration.AudioImportConfiguration.SampleRate; audioConfiguration.compressionFormat = importConfiguration.AudioImportConfiguration.CompressionFormat; // Assigning configured audio importer configurations. audioImporter.defaultSampleSettings = audioConfiguration; // Checking for active audio overide platorm option. bool platformOverideEnabled = importConfiguration.AudioImportConfiguration.AudioOveridePlatormOption != PlatformOption.None; // Checking if platform settings overide is enabled. if (platformOverideEnabled) { // Overiding platform settings for selected runtime platform. audioImporter.SetOverrideSampleSettings(importConfiguration.AudioImportConfiguration.AudioOveridePlatormOption.ToString(), audioConfiguration); } } catch (Exception e) { // Throwing a new system exception. Debugger.ThrowException(e); } }
private bool SaveTexture(Texture2D tex, string textureName) { using (FileStream f = new FileStream(realExportPath + textureName, FileMode.Create)) { using (BinaryWriter b = new BinaryWriter(f)) { b.Write(((Texture2D)tex).EncodeToPNG()); } } AssetPostprocessor proc = new AssetPostprocessor(); AssetDatabase.Refresh(); proc.assetPath = _exportPath + textureName; TextureImporter importer = proc.assetImporter as TextureImporter; if (importer == null) { Debug.LogError("TextureImporter is null"); return(false); } importer.mipmapEnabled = false; importer.maxTextureSize = 2048; importer.textureFormat = TextureImporterFormat.AutomaticTruecolor; AssetDatabase.ImportAsset(_exportPath + textureName, ImportAssetOptions.ForceUpdate); AssetDatabase.LoadAssetAtPath(_exportPath + textureName, typeof(Texture2D)); AssetDatabase.Refresh(); return(true); }
void ReimportNonReadableMeshes() { IEnumerable <Mesh> allMeshes = drawCallTarget.GetComponentsInChildren <MeshFilter> ().Select(X => X.sharedMesh).Distinct(); AssetPostprocessor postProcessor = new AssetPostprocessor(); float currentCount = 0.0f; int targetCount = allMeshes.Count(); foreach (Mesh m in allMeshes) { EditorUtility.DisplayProgressBar("Allowing mesh to be readable", "Analyzing mesh " + ((int)currentCount + 1).ToString() + " of " + targetCount.ToString(), currentCount / targetCount); currentCount += 1; string assetPath = AssetDatabase.GetAssetPath(m); if (assetPath != null) { postProcessor.assetPath = assetPath; ModelImporter importer = (ModelImporter)postProcessor.assetImporter; if (importer != null && !importer.isReadable) { importer.isReadable = true; AssetDatabase.ImportAsset(assetPath); AssetDatabase.Refresh(); } } } EditorUtility.ClearProgressBar(); }
/// <summary> /// Exports the combined meshes to the folder selected in the Editor Window /// </summary> /// <param name="realAssetPath">The real asset path to be used by System.IO</param> private void ExportMeshes(string realAssetPath) { string materialsPath = Path.Combine(_exportPath, "Materials"); char[] invalidChars = Path.GetInvalidFileNameChars(); foreach (MeshFilter mf in _hiddenCombinedObject.GetComponentsInChildren <MeshFilter>()) { string filename = mf.name; foreach (char c in invalidChars) { filename.Replace(c, '_'); } filename += ".obj"; if (MeshExporter.MeshToFile(mf.sharedMesh, realAssetPath + filename)) { AssetPostprocessor proc = new AssetPostprocessor(); AssetDatabase.Refresh(); //even though it cant get here, should double check that export folder is null or not proc.assetPath = Path.Combine(AssetDatabase.GetAssetPath(_exportFolder), filename); ModelImporter importer = proc.assetImporter as ModelImporter; importer.generateSecondaryUV = _isGeneratingLightmapUVs; importer.generateAnimations = ModelImporterGenerateAnimations.None; importer.optimizeMesh = true; importer.importMaterials = false; AssetDatabase.ImportAsset(Path.Combine(_exportPath, filename), ImportAssetOptions.ForceUpdate); mf.sharedMesh = (Mesh)AssetDatabase.LoadAssetAtPath(Path.Combine(_exportPath, filename), typeof(Mesh)); AssetDatabase.DeleteAsset(Path.Combine(materialsPath, mf.sharedMesh.name)); if (AssetDatabase.LoadAllAssetsAtPath(materialsPath).Length == 0) { FileUtil.DeleteFileOrDirectory(materialsPath); } } else { Debug.LogError("There was an issue exporting the mesh to " + realAssetPath + filename + ". Please double check your paths, names, and files. If this occurs frequently, create an issue, or vote for the existing issue if it already exists at (https://bitbucket.org/purdyjo/draw-call-minimizer/issues) and please include all information related to this issue to make the repair as fast and seamless as possible"); } } AssetDatabase.Refresh(); PrefabUtility.CreatePrefab(_exportPath + "CombinedObject.prefab", _hiddenCombinedObject, ReplacePrefabOptions.ConnectToPrefab); }
/// <summary> /// This function is for post processing project imported texture assets. /// </summary> /// <param name="importConfiguration">Takes in the import configuration asset.</param> /// <param name="assetPostprocessor">Takes in the asset postprocessor class.</param> public static void ProcessTextureAsset(ConfigurationAsset importConfiguration, AssetPostprocessor assetPostprocessor) { // Trying to modify the texture importer with import configuration. try { // Converting texture overide platorm options to a string. string runtimePlatformName = importConfiguration.TextureImportConfiguration.m_TextureOveridePlatormOption.ToString(); // Getting the texture impoprter settings from the imported asset. var textureImporter = assetPostprocessor.assetImporter as TextureImporter; // Getting the max size enum and converting it to a int value. int maximumTextureSize = (int)importConfiguration.TextureImportConfiguration.MaximumTextureSize; // Assigning texture importer platform settings maximum texture size. textureImporter.maxTextureSize = maximumTextureSize; // Assigning new settings. textureImporter.anisoLevel = importConfiguration.TextureImportConfiguration.AnisotropicFilteringLevel; // Getting current platform texture settings. var platformTextureSettings = textureImporter.GetPlatformTextureSettings(runtimePlatformName); // Checking if platform overide settings is enabled. platformTextureSettings.overridden = importConfiguration.TextureImportConfiguration.m_TextureOveridePlatormOption != PlatformOption.None; // Overiding setting for selected runtime platform. platformTextureSettings.maxTextureSize = maximumTextureSize; // Assigning final configured settings to the texture importer. textureImporter.SetPlatformTextureSettings(platformTextureSettings); } catch (Exception e) { // Throwing a new system exception. Debugger.ThrowException(e); } }
private static void CallJavascript(AssetPostprocessor proc, string funcName, params object[] args) { var rt = EditorRuntime.GetRuntime(); if (rt != null) { var context = rt.GetMainContext(); var ctx = (JSContext)context; var globalThis = context.GetGlobalObject(); var func = JSApi.JS_GetProperty(ctx, globalThis, context.GetAtom(funcName)); if (JSApi.JS_IsFunction(ctx, func) == 1) { var arglist = new List <JSValue>(); do { if (proc != null) { var val = Values.js_push_var(ctx, proc); if (val.IsException()) { ctx.print_exception(); break; } arglist.Add(val); } var err = false; for (var i = 0; i < args.Length; i++) { var val = Values.js_push_var(ctx, args[i]); if (val.IsException()) { ctx.print_exception(); err = true; break; } arglist.Add(val); } if (err) { break; } var argv = arglist.ToArray(); var rval = JSApi.JS_Call(ctx, func, globalThis, argv.Length, argv); if (rval.IsException()) { ctx.print_exception(); } JSApi.JS_FreeValue(ctx, rval); } while (false); for (var i = 0; i < arglist.Count; i++) { JSApi.JS_FreeValue(ctx, arglist[i]); } } JSApi.JS_FreeValue(ctx, func); JSApi.JS_FreeValue(ctx, globalThis); } }