private void ExtractTexturesGUI()
        {
            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.PrefixLabel(Styles.Textures);

                using (
                    new EditorGUI.DisabledScope(!m_HasEmbeddedTextures.boolValue &&
                                                !m_HasEmbeddedTextures.hasMultipleDifferentValues))
                {
                    if (GUILayout.Button(Styles.ExtractEmbeddedTextures))
                    {
                        // when extracting textures, we must handle the case when multiple selected assets could generate textures with the same name at the user supplied path
                        // we proceed as follows:
                        // 1. each asset extracts the textures in a separate temp folder
                        // 2. we remap the extracted assets to the respective asset importer
                        // 3. we generate unique names for each asset and move them to the user supplied path
                        // 4. we re-import all the assets to have the internal materials linked to the newly extracted textures

                        List <Tuple <Object, string> > outputsForTargets = new List <Tuple <Object, string> >();
                        // use the first target for selecting the destination folder, but apply that path for all targets
                        string destinationPath = (target as ModelImporter).assetPath;
                        destinationPath = EditorUtility.SaveFolderPanel("Select Textures Folder",
                                                                        FileUtil.DeleteLastPathNameComponent(destinationPath), "");
                        if (string.IsNullOrEmpty(destinationPath))
                        {
                            // cancel the extraction if the user did not select a folder
                            return;
                        }
                        destinationPath = FileUtil.GetProjectRelativePath(destinationPath);

                        try
                        {
                            // batch the extraction of the textures
                            AssetDatabase.StartAssetEditing();

                            foreach (var t in targets)
                            {
                                var tempPath = FileUtil.GetUniqueTempPathInProject();
                                tempPath = tempPath.Replace("Temp", UnityEditorInternal.InternalEditorUtility.GetAssetsFolder());
                                outputsForTargets.Add(Tuple.Create(t, tempPath));

                                var importer = t as ModelImporter;
                                importer.ExtractTextures(tempPath);
                            }
                        }
                        finally
                        {
                            AssetDatabase.StopAssetEditing();
                        }

                        try
                        {
                            // batch the remapping and the reimport of the assets
                            AssetDatabase.Refresh();
                            AssetDatabase.StartAssetEditing();

                            foreach (var item in outputsForTargets)
                            {
                                var importer = item.Item1 as ModelImporter;

                                var guids = AssetDatabase.FindAssets("t:Texture", new string[] { item.Item2 });

                                foreach (var guid in guids)
                                {
                                    var path = AssetDatabase.GUIDToAssetPath(guid);
                                    var tex  = AssetDatabase.LoadAssetAtPath <Texture>(path);
                                    if (tex == null)
                                    {
                                        continue;
                                    }

                                    importer.AddRemap(new AssetImporter.SourceAssetIdentifier(tex), tex);

                                    var newPath = Path.Combine(destinationPath, FileUtil.UnityGetFileName(path));
                                    newPath = AssetDatabase.GenerateUniqueAssetPath(newPath);
                                    AssetDatabase.MoveAsset(path, newPath);
                                }

                                AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);

                                AssetDatabase.DeleteAsset(item.Item2);
                            }
                        }
                        finally
                        {
                            AssetDatabase.StopAssetEditing();
                        }
                    }
                }
            }
        }
Example #2
0
 private void ExtractTexturesGUI()
 {
     using (new EditorGUILayout.HorizontalScope(new GUILayoutOption[0]))
     {
         EditorGUILayout.PrefixLabel(ModelImporterMaterialEditor.Styles.Textures);
         using (new EditorGUI.DisabledScope(!this.m_HasEmbeddedTextures.boolValue && !this.m_HasEmbeddedTextures.hasMultipleDifferentValues))
         {
             if (GUILayout.Button(ModelImporterMaterialEditor.Styles.ExtractEmbeddedTextures, new GUILayoutOption[0]))
             {
                 List <Tuple <UnityEngine.Object, string> > list = new List <Tuple <UnityEngine.Object, string> >();
                 string text = (base.target as ModelImporter).assetPath;
                 text = EditorUtility.SaveFolderPanel("Select Textures Folder", FileUtil.DeleteLastPathNameComponent(text), "");
                 if (!string.IsNullOrEmpty(text))
                 {
                     text = FileUtil.GetProjectRelativePath(text);
                     try
                     {
                         AssetDatabase.StartAssetEditing();
                         UnityEngine.Object[] targets = base.targets;
                         for (int i = 0; i < targets.Length; i++)
                         {
                             UnityEngine.Object @object = targets[i];
                             string             text2   = FileUtil.GetUniqueTempPathInProject();
                             text2 = text2.Replace("Temp", InternalEditorUtility.GetAssetsFolder());
                             list.Add(Tuple.Create <UnityEngine.Object, string>(@object, text2));
                             ModelImporter modelImporter = @object as ModelImporter;
                             modelImporter.ExtractTextures(text2);
                         }
                     }
                     finally
                     {
                         AssetDatabase.StopAssetEditing();
                     }
                     try
                     {
                         AssetDatabase.Refresh();
                         AssetDatabase.StartAssetEditing();
                         foreach (Tuple <UnityEngine.Object, string> current in list)
                         {
                             ModelImporter modelImporter2 = current.Item1 as ModelImporter;
                             string[]      array          = AssetDatabase.FindAssets("t:Texture", new string[]
                             {
                                 current.Item2
                             });
                             string[] array2 = array;
                             for (int j = 0; j < array2.Length; j++)
                             {
                                 string  guid    = array2[j];
                                 string  text3   = AssetDatabase.GUIDToAssetPath(guid);
                                 Texture texture = AssetDatabase.LoadAssetAtPath <Texture>(text3);
                                 if (!(texture == null))
                                 {
                                     modelImporter2.AddRemap(new AssetImporter.SourceAssetIdentifier(texture), texture);
                                     string text4 = Path.Combine(text, FileUtil.UnityGetFileName(text3));
                                     text4 = AssetDatabase.GenerateUniqueAssetPath(text4);
                                     AssetDatabase.MoveAsset(text3, text4);
                                 }
                             }
                             AssetDatabase.ImportAsset(modelImporter2.assetPath, ImportAssetOptions.ForceUpdate);
                             AssetDatabase.DeleteAsset(current.Item2);
                         }
                     }
                     finally
                     {
                         AssetDatabase.StopAssetEditing();
                     }
                 }
             }
         }
     }
 }