static void ExtractMaterialsAndTextures(ScriptedImporter self) { if (string.IsNullOrEmpty(self.assetPath)) { return; } Action <Texture2D> addRemap = externalObject => { self.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject); }; Action <IEnumerable <string> > onCompleted = _ => { AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); self.ExtractMaterials(); AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); }; TextureExtractor.ExtractTextures(self.assetPath, GltfTextureEnumerator.Enumerate, self.GetSubAssets <UnityEngine.Texture2D>(self.assetPath).ToArray(), addRemap, onCompleted ); }
static void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser) { if (string.IsNullOrEmpty(self.assetPath)) { return; } Action <Texture2D> addRemap = externalObject => { self.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject); }; Action <IEnumerable <UnityPath> > onCompleted = _ => { AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); self.ExtractMaterials(); AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); }; var assetPath = UnityPath.FromFullpath(parser.TargetPath); var dirName = $"{assetPath.FileNameWithoutExtension}.Textures"; TextureExtractor.ExtractTextures(parser, assetPath.Parent.Child(dirName), GltfTextureEnumerator.Enumerate, self.GetSubAssets <UnityEngine.Texture2D>(self.assetPath).ToArray(), addRemap, onCompleted ); }
static void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser, EnumerateAllTexturesDistinctFunc enumTextures, Func <string, string> textureDir, Func <string, string> materialDir) { if (string.IsNullOrEmpty(self.assetPath)) { return; } Action <SubAssetKey, Texture2D> addRemap = (key, externalObject) => { self.AddRemap(new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), externalObject); }; Action <IEnumerable <UnityPath> > onCompleted = _ => { AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); self.ExtractMaterials(materialDir); AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); }; var assetPath = UnityPath.FromFullpath(parser.TargetPath); var dirName = textureDir(assetPath.Value); // $"{assetPath.FileNameWithoutExtension}.Textures"; TextureExtractor.ExtractTextures(parser, assetPath.Parent.Child(dirName), enumTextures, self.GetSubAssets <UnityEngine.Texture2D>(self.assetPath).ToArray(), addRemap, onCompleted ); }
public static void ExtractTextures(this ScriptedImporter importer, string dirName, Action onCompleted = null) { if (string.IsNullOrEmpty(importer.assetPath)) { return; } var path = string.Format("{0}/{1}.{2}", Path.GetDirectoryName(importer.assetPath), Path.GetFileNameWithoutExtension(importer.assetPath), dirName ); importer.SafeCreateDirectory(path); // Reload Model var extractor = new TextureExtractor(importer); foreach (var material in extractor.GLTF.materials) { foreach (var x in extractor.Parser.EnumerateTextures(material)) { extractor.Extract(x); } } EditorApplication.delayCall += () => { foreach (var extracted in extractor.Textures) { // TextureImporter var targetTextureImporter = AssetImporter.GetAtPath(extracted.Path) as TextureImporter; targetTextureImporter.sRGBTexture = extracted.sRGB; if (extracted.IsNormalMap) { targetTextureImporter.textureType = TextureImporterType.NormalMap; } targetTextureImporter.SaveAndReimport(); // remap var externalObject = AssetDatabase.LoadAssetAtPath <UnityEngine.Texture2D>(extracted.Path); importer.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject); } AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate); if (onCompleted != null) { onCompleted(); } }; }
public void ApplyRemap(ScriptedImporter importer) { foreach (var kv in m_editMap) { if (kv.Object != null) { importer.AddRemap(kv.ID, kv.Object); } else { importer.RemoveRemap(kv.ID); } } m_editMap.Clear(); AssetDatabase.WriteImportSettingsIfDirty(importer.assetPath); AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate); }
/// <summary> /// Apply /// </summary> protected override void Apply() { foreach (var kv in m_editMap) { if (kv.Object != null) { m_importer.AddRemap(kv.ID, kv.Object); } else { m_importer.RemoveRemap(kv.ID); } } m_editMap.Clear(); AssetDatabase.WriteImportSettingsIfDirty(m_importer.assetPath); AssetDatabase.ImportAsset(m_importer.assetPath, ImportAssetOptions.ForceUpdate); base.Apply(); }
void ExtractMaterialsAndTextures(ScriptedImporter self, GltfData data, ITextureDescriptorGenerator textureDescriptorGenerator, Func <string, string> textureDir, Func <string, string> materialDir) { if (string.IsNullOrEmpty(self.assetPath)) { return; } Action <SubAssetKey, Texture2D> addRemap = (key, externalObject) => { self.AddRemap(new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), externalObject); }; Action <IEnumerable <UnityPath> > onCompleted = _ => { // texture extract 後に importer 発動 AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); ExtractMaterials(self, materialDir); }; // subAsset を ExternalObject として投入する var subAssets = AssetDatabase.LoadAllAssetsAtPath(self.assetPath) .Select(x => x as Texture) .Where(x => x != null) .Select(x => (new SubAssetKey(x), x)) .ToDictionary(kv => kv.Item1, kv => kv.Item2) ; var assetPath = UnityPath.FromUnityPath(self.assetPath); var dirName = textureDir(assetPath.Value); // $"{assetPath.FileNameWithoutExtension}.Textures"; TextureExtractor.ExtractTextures( data, assetPath.Parent.Child(dirName), textureDescriptorGenerator, subAssets, addRemap, onCompleted ); }
public static void SetExternalUnityObject <T>(this ScriptedImporter self, UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object { self.AddRemap(sourceAssetIdentifier, obj); AssetDatabase.WriteImportSettingsIfDirty(self.assetPath); AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); }
public static void ExtractTextures(this ScriptedImporter importer, string dirName, Func <string, VrmLib.Model> CreateModel, Action onComplited = null) { if (string.IsNullOrEmpty(importer.assetPath)) { return; } var subAssets = importer.GetSubAssets <UnityEngine.Texture2D>(importer.assetPath); var path = string.Format("{0}/{1}.{2}", Path.GetDirectoryName(importer.assetPath), Path.GetFileNameWithoutExtension(importer.assetPath), dirName ); importer.SafeCreateDirectory(path); Dictionary <VrmLib.ImageTexture, string> targetPaths = new Dictionary <VrmLib.ImageTexture, string>(); // Reload Model var model = CreateModel(importer.assetPath); var mimeTypeReg = new System.Text.RegularExpressions.Regex("image/(?<mime>.*)$"); int count = 0; foreach (var texture in model.Textures) { var imageTexture = texture as VrmLib.ImageTexture; if (imageTexture == null) { continue; } var mimeType = mimeTypeReg.Match(imageTexture.Image.MimeType); var assetName = !string.IsNullOrEmpty(imageTexture.Name) ? imageTexture.Name : string.Format("{0}_img{1}", model.Root.Name, count); var targetPath = string.Format("{0}/{1}.{2}", path, assetName, mimeType.Groups["mime"].Value); imageTexture.Name = assetName; if (imageTexture.TextureType == VrmLib.Texture.TextureTypes.MetallicRoughness || imageTexture.TextureType == VrmLib.Texture.TextureTypes.Occlusion) { var subAssetTexture = subAssets.Where(x => x.name == imageTexture.Name).FirstOrDefault(); File.WriteAllBytes(targetPath, subAssetTexture.EncodeToPNG()); } else { File.WriteAllBytes(targetPath, imageTexture.Image.Bytes.ToArray()); } AssetDatabase.ImportAsset(targetPath); targetPaths.Add(imageTexture, targetPath); count++; } EditorApplication.delayCall += () => { foreach (var targetPath in targetPaths) { var imageTexture = targetPath.Key; var targetTextureImporter = AssetImporter.GetAtPath(targetPath.Value) as TextureImporter; targetTextureImporter.sRGBTexture = (imageTexture.ColorSpace == VrmLib.Texture.ColorSpaceTypes.Srgb); if (imageTexture.TextureType == VrmLib.Texture.TextureTypes.NormalMap) { targetTextureImporter.textureType = TextureImporterType.NormalMap; } targetTextureImporter.SaveAndReimport(); var externalObject = AssetDatabase.LoadAssetAtPath(targetPath.Value, typeof(UnityEngine.Texture2D)); importer.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), imageTexture.Name), externalObject); } //AssetDatabase.WriteImportSettingsIfDirty(assetPath); AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate); if (onComplited != null) { onComplited(); } }; }