private string GenerateGridBrushInstanceLibraryPath(Type brushType)
        {
            var path = FileUtil.CombinePaths(s_LibraryPath, brushType.ToString() + s_GridBrushExtension);

            path = FileUtil.NiceWinPath(path);
            return(path);
        }
Example #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);
            }
        }
Example #3
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);
            }
        }
Example #4
0
        private string GenerateGridBrushInstanceLibraryPath(Type brushType)
        {
            string unityPath = FileUtil.CombinePaths(new string[]
            {
                GridPaletteBrushes.s_LibraryPath,
                brushType.ToString() + GridPaletteBrushes.s_GridBrushExtension
            });

            return(FileUtil.NiceWinPath(unityPath));
        }
Example #5
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);
            }
        }
Example #6
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);
            }
        }
Example #7
0
        public static Dictionary <Vector2Int, TileBase> ConvertToTileSheet(Dictionary <Vector2Int, UnityEngine.Object> sheet)
        {
            Dictionary <Vector2Int, TileBase> dictionary = new Dictionary <Vector2Int, TileBase>();
            string text = (!ProjectBrowser.s_LastInteractedProjectBrowser) ? "Assets" : ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath();
            Dictionary <Vector2Int, TileBase> result;

            if (sheet.Values.ToList <UnityEngine.Object>().FindAll((UnityEngine.Object obj) => obj is TileBase).Count == sheet.Values.Count)
            {
                foreach (KeyValuePair <Vector2Int, UnityEngine.Object> current in sheet)
                {
                    dictionary.Add(current.Key, current.Value as TileBase);
                }
                result = dictionary;
            }
            else
            {
                TileDragAndDrop.UserTileCreationMode userTileCreationMode = TileDragAndDrop.UserTileCreationMode.Overwrite;
                string text2 = "";
                bool   flag  = sheet.Count > 1;
                if (flag)
                {
                    bool flag2 = false;
                    text2 = EditorUtility.SaveFolderPanel("Generate tiles into folder ", text, "");
                    text2 = FileUtil.GetProjectRelativePath(text2);
                    foreach (UnityEngine.Object current2 in sheet.Values)
                    {
                        if (current2 is Sprite)
                        {
                            string path = FileUtil.CombinePaths(new string[]
                            {
                                text2,
                                string.Format("{0}.{1}", current2.name, TileDragAndDrop.k_TileExtension)
                            });
                            if (File.Exists(path))
                            {
                                flag2 = true;
                                break;
                            }
                        }
                    }
                    if (flag2)
                    {
                        int num = EditorUtility.DisplayDialogComplex("Overwrite?", string.Format("Assets exist at {0}. Do you wish to overwrite existing assets?", text2), "Overwrite", "Create New Copy", "Reuse");
                        if (num != 0)
                        {
                            if (num != 1)
                            {
                                if (num == 2)
                                {
                                    userTileCreationMode = TileDragAndDrop.UserTileCreationMode.Reuse;
                                }
                            }
                            else
                            {
                                userTileCreationMode = TileDragAndDrop.UserTileCreationMode.CreateUnique;
                            }
                        }
                        else
                        {
                            userTileCreationMode = TileDragAndDrop.UserTileCreationMode.Overwrite;
                        }
                    }
                }
                else
                {
                    text2 = EditorUtility.SaveFilePanelInProject("Generate new tile", sheet.Values.First <UnityEngine.Object>().name, TileDragAndDrop.k_TileExtension, "Generate new tile", text);
                }
                if (string.IsNullOrEmpty(text2))
                {
                    result = dictionary;
                }
                else
                {
                    int num2 = 0;
                    EditorUtility.DisplayProgressBar(string.Concat(new object[]
                    {
                        "Generating Tile Assets (",
                        num2,
                        "/",
                        sheet.Count,
                        ")"
                    }), "Generating tiles", 0f);
                    foreach (KeyValuePair <Vector2Int, UnityEngine.Object> current3 in sheet)
                    {
                        string   text3 = "";
                        TileBase tileBase;
                        if (current3.Value is Sprite)
                        {
                            tileBase = TileDragAndDrop.CreateTile(current3.Value as Sprite);
                            text3    = ((!flag) ? text2 : FileUtil.CombinePaths(new string[]
                            {
                                text2,
                                string.Format("{0}.{1}", tileBase.name, TileDragAndDrop.k_TileExtension)
                            }));
                            if (userTileCreationMode != TileDragAndDrop.UserTileCreationMode.CreateUnique)
                            {
                                if (userTileCreationMode != TileDragAndDrop.UserTileCreationMode.Overwrite)
                                {
                                    if (userTileCreationMode == TileDragAndDrop.UserTileCreationMode.Reuse)
                                    {
                                        if (File.Exists(text3))
                                        {
                                            tileBase = AssetDatabase.LoadAssetAtPath <TileBase>(text3);
                                        }
                                        else
                                        {
                                            AssetDatabase.CreateAsset(tileBase, text3);
                                        }
                                    }
                                }
                                else
                                {
                                    AssetDatabase.CreateAsset(tileBase, text3);
                                }
                            }
                            else
                            {
                                if (File.Exists(text3))
                                {
                                    text3 = AssetDatabase.GenerateUniqueAssetPath(text3);
                                }
                                AssetDatabase.CreateAsset(tileBase, text3);
                            }
                        }
                        else
                        {
                            tileBase = (current3.Value as TileBase);
                        }
                        EditorUtility.DisplayProgressBar(string.Concat(new object[]
                        {
                            "Generating Tile Assets (",
                            num2,
                            "/",
                            sheet.Count,
                            ")"
                        }), "Generating " + text3, (float)num2++ / (float)sheet.Count);
                        dictionary.Add(current3.Key, tileBase);
                    }
                    EditorUtility.ClearProgressBar();
                    AssetDatabase.Refresh();
                    result = dictionary;
                }
            }
            return(result);
        }
        public static Dictionary <Vector2Int, TileBase> ConvertToTileSheet(Dictionary <Vector2Int, Object> sheet)
        {
            Dictionary <Vector2Int, TileBase> result = new Dictionary <Vector2Int, TileBase>();

            string defaultPath = ProjectBrowser.s_LastInteractedProjectBrowser
                ? ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath()
                : "Assets";

            // Early out if all objects are already tiles
            if (sheet.Values.ToList().FindAll(obj => obj is TileBase).Count == sheet.Values.Count)
            {
                foreach (KeyValuePair <Vector2Int, Object> item in sheet)
                {
                    result.Add(item.Key, item.Value as TileBase);
                }
                return(result);
            }

            UserTileCreationMode userTileCreationMode = UserTileCreationMode.Overwrite;
            string path          = "";
            bool   multipleTiles = sheet.Count > 1;

            if (multipleTiles)
            {
                bool userInterventionRequired = false;
                path = EditorUtility.SaveFolderPanel("Generate tiles into folder ", defaultPath, "");
                path = FileUtil.GetProjectRelativePath(path);

                // Check if this will overwrite any existing assets
                foreach (var item in sheet.Values)
                {
                    if (item is Sprite)
                    {
                        var tilePath = FileUtil.CombinePaths(path, String.Format("{0}.{1}", item.name, k_TileExtension));
                        if (File.Exists(tilePath))
                        {
                            userInterventionRequired = true;
                            break;
                        }
                    }
                }
                // There are existing tile assets in the folder with names matching the items to be created
                if (userInterventionRequired)
                {
                    var option = EditorUtility.DisplayDialogComplex("Overwrite?", String.Format("Assets exist at {0}. Do you wish to overwrite existing assets?", path), "Overwrite", "Create New Copy", "Reuse");
                    switch (option)
                    {
                    case 0:     // Overwrite
                    {
                        userTileCreationMode = UserTileCreationMode.Overwrite;
                    }
                    break;

                    case 1:     // Create New Copy
                    {
                        userTileCreationMode = UserTileCreationMode.CreateUnique;
                    }
                    break;

                    case 2:     // Reuse
                    {
                        userTileCreationMode = UserTileCreationMode.Reuse;
                    }
                    break;
                    }
                }
            }
            else
            {
                // Do not check if this will overwrite new tile as user has explicitly selected the file to save to
                path = EditorUtility.SaveFilePanelInProject("Generate new tile", sheet.Values.First().name, k_TileExtension, "Generate new tile", defaultPath);
            }

            if (string.IsNullOrEmpty(path))
            {
                return(result);
            }

            int i = 0;

            EditorUtility.DisplayProgressBar("Generating Tile Assets (" + i + "/" + sheet.Count + ")", "Generating tiles", 0f);
            foreach (KeyValuePair <Vector2Int, Object> item in sheet)
            {
                TileBase tile;
                string   tilePath = "";
                if (item.Value is Sprite)
                {
                    tile     = CreateTile(item.Value as Sprite);
                    tilePath = multipleTiles
                        ? FileUtil.CombinePaths(path, String.Format("{0}.{1}", tile.name, k_TileExtension))
                        : path;
                    switch (userTileCreationMode)
                    {
                    case UserTileCreationMode.CreateUnique:
                    {
                        if (File.Exists(tilePath))
                        {
                            tilePath = AssetDatabase.GenerateUniqueAssetPath(tilePath);
                        }
                        AssetDatabase.CreateAsset(tile, tilePath);
                    }
                    break;

                    case UserTileCreationMode.Overwrite:
                    {
                        AssetDatabase.CreateAsset(tile, tilePath);
                    }
                    break;

                    case UserTileCreationMode.Reuse:
                    {
                        if (File.Exists(tilePath))
                        {
                            tile = AssetDatabase.LoadAssetAtPath <TileBase>(tilePath);
                        }
                        else
                        {
                            AssetDatabase.CreateAsset(tile, tilePath);
                        }
                    }
                    break;
                    }
                }
                else
                {
                    tile = item.Value as TileBase;
                }
                EditorUtility.DisplayProgressBar("Generating Tile Assets (" + i + "/" + sheet.Count + ")", "Generating " + tilePath, (float)i++ / sheet.Count);
                result.Add(item.Key, tile);
            }
            EditorUtility.ClearProgressBar();

            AssetDatabase.Refresh();
            return(result);
        }