Esempio n. 1
0
        internal static void ExtractMaterialsFromAsset(UnityEngine.Object[] targets, string destinationPath)
        {
            HashSet <string> hashSet = new HashSet <string>();

            for (int i = 0; i < targets.Length; i++)
            {
                UnityEngine.Object @object                  = targets[i];
                ModelImporter      modelImporter            = @object as ModelImporter;
                IEnumerable <UnityEngine.Object> enumerable = from x in AssetDatabase.LoadAllAssetsAtPath(modelImporter.assetPath)
                                                              where x.GetType() == typeof(Material)
                                                              select x;
                foreach (UnityEngine.Object current in enumerable)
                {
                    string text = FileUtil.CombinePaths(new string[]
                    {
                        destinationPath,
                        current.name
                    }) + ".mat";
                    text = AssetDatabase.GenerateUniqueAssetPath(text);
                    string value = AssetDatabase.ExtractAsset(current, text);
                    if (string.IsNullOrEmpty(value))
                    {
                        hashSet.Add(modelImporter.assetPath);
                    }
                }
            }
            foreach (string current2 in hashSet)
            {
                AssetDatabase.WriteImportSettingsIfDirty(current2);
                AssetDatabase.ImportAsset(current2, ImportAssetOptions.ForceUpdate);
            }
        }
Esempio n. 2
0
        internal static void ExtractMaterialsFromAsset(Object[] targets, string destinationPath)
        {
            var assetsToReload = new HashSet <string>();

            foreach (var t in targets)
            {
                var importer = t as ModelImporter;

                var materials = AssetDatabase.LoadAllAssetsAtPath(importer.assetPath).Where(x => x.GetType() == typeof(Material));

                foreach (var material in materials)
                {
                    var newAssetPath = FileUtil.CombinePaths(destinationPath, material.name) + kMaterialExtension;
                    newAssetPath = AssetDatabase.GenerateUniqueAssetPath(newAssetPath);

                    var error = AssetDatabase.ExtractAsset(material, newAssetPath);
                    if (string.IsNullOrEmpty(error))
                    {
                        assetsToReload.Add(importer.assetPath);
                    }
                }
            }

            foreach (var path in assetsToReload)
            {
                AssetDatabase.WriteImportSettingsIfDirty(path);
                AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
            }
        }
Esempio n. 3
0
        internal static void ExtractSelectedObjectsFromPrefab()
        {
            var    assetsToReload = new HashSet <string>();
            string folder         = null;

            foreach (var selectedObj in Selection.objects)
            {
                var path = AssetDatabase.GetAssetPath(selectedObj);

                // use the first selected element as the basis for the folder path where all the materials will be extracted
                if (folder == null)
                {
                    folder = EditorUtility.SaveFolderPanel("Select Materials Folder", FileUtil.DeleteLastPathNameComponent(path), "");
                    if (string.IsNullOrEmpty(folder))
                    {
                        // cancel the extraction if the user did not select a folder
                        return;
                    }

                    folder = FileUtil.GetProjectRelativePath(folder);
                }

                // TODO: [bogdanc 3/6/2017] if we want this function really generic, we need to know what extension the new asset files should have
                var extension    = selectedObj is Material ? kMaterialExtension : string.Empty;
                var newAssetPath = FileUtil.CombinePaths(folder, selectedObj.name) + extension;
                newAssetPath = AssetDatabase.GenerateUniqueAssetPath(newAssetPath);

                var error = AssetDatabase.ExtractAsset(selectedObj, newAssetPath);
                if (string.IsNullOrEmpty(error))
                {
                    assetsToReload.Add(path);
                }
            }

            foreach (var path in assetsToReload)
            {
                AssetDatabase.WriteImportSettingsIfDirty(path);
                AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
            }
        }
Esempio n. 4
0
        internal static void ExtractSelectedObjectsFromPrefab()
        {
            HashSet <string> hashSet = new HashSet <string>();
            string           text    = null;

            UnityEngine.Object[] objects = Selection.objects;
            for (int i = 0; i < objects.Length; i++)
            {
                UnityEngine.Object @object   = objects[i];
                string             assetPath = AssetDatabase.GetAssetPath(@object);
                if (text == null)
                {
                    text = EditorUtility.SaveFolderPanel("Select Materials Folder", FileUtil.DeleteLastPathNameComponent(assetPath), "");
                    if (string.IsNullOrEmpty(text))
                    {
                        return;
                    }
                    text = FileUtil.GetProjectRelativePath(text);
                }
                string str   = (!(@object is Material)) ? string.Empty : ".mat";
                string text2 = FileUtil.CombinePaths(new string[]
                {
                    text,
                    @object.name
                }) + str;
                text2 = AssetDatabase.GenerateUniqueAssetPath(text2);
                string value = AssetDatabase.ExtractAsset(@object, text2);
                if (string.IsNullOrEmpty(value))
                {
                    hashSet.Add(assetPath);
                }
            }
            foreach (string current in hashSet)
            {
                AssetDatabase.WriteImportSettingsIfDirty(current);
                AssetDatabase.ImportAsset(current, ImportAssetOptions.ForceUpdate);
            }
        }