Ejemplo n.º 1
0
        /// <summary>
        /// Creates a Palette Asset at the given folder path.
        /// </summary>
        /// <param name="folderPath">Folder Path of the Palette Asset.</param>
        /// <param name="name">Name of the Palette Asset.</param>
        /// <param name="layout">Grid Layout of the Palette Asset.</param>
        /// <param name="cellSizing">Cell Sizing of the Palette Asset.</param>
        /// <param name="cellSize">Cell Size of the Palette Asset.</param>
        /// <param name="swizzle">Cell Swizzle of the Palette.</param>
        /// <param name="sortMode">Transparency Sort Mode for the Palette</param>
        /// <param name="sortAxis">Transparency Sort Axis for the Palette</param>
        /// <returns>The created Palette Asset if successful.</returns>
        public static GameObject CreateNewPalette(string folderPath
                                                  , string name
                                                  , GridLayout.CellLayout layout
                                                  , GridPalette.CellSizing cellSizing
                                                  , Vector3 cellSize
                                                  , GridLayout.CellSwizzle swizzle
                                                  , TransparencySortMode sortMode
                                                  , Vector3 sortAxis)
        {
            GameObject temporaryGO = new GameObject(name);
            Grid       grid        = temporaryGO.AddComponent <Grid>();

            // We set size to kEpsilon to mark this as new uninitialized palette
            // Nice default size can be decided when first asset is dragged in
            grid.cellSize    = cellSize;
            grid.cellLayout  = layout;
            grid.cellSwizzle = swizzle;
            CreateNewLayer(temporaryGO, "Layer1", layout);

            string path = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + name + ".prefab");

            Object      prefab  = PrefabUtility.SaveAsPrefabAssetAndConnect(temporaryGO, path, InteractionMode.AutomatedAction);
            GridPalette palette = CreateGridPalette(cellSizing, sortMode, sortAxis);

            AssetDatabase.AddObjectToAsset(palette, prefab);
            PrefabUtility.ApplyPrefabInstance(temporaryGO, InteractionMode.AutomatedAction);
            AssetDatabase.Refresh();

            Object.DestroyImmediate(temporaryGO);
            return(AssetDatabase.LoadAssetAtPath <GameObject>(path));
        }
Ejemplo n.º 2
0
        public static Vector3 InverseSwizzle(GridLayout.CellSwizzle swizzle, Vector3 position)
        {
            Vector3 result;

            Grid.InverseSwizzle_Injected(swizzle, ref position, out result);
            return(result);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates a Palette Asset at the given folder path.
 /// </summary>
 /// <param name="folderPath">Folder Path of the Palette Asset.</param>
 /// <param name="name">Name of the Palette Asset.</param>
 /// <param name="layout">Grid Layout of the Palette Asset.</param>
 /// <param name="cellSizing">Cell Sizing of the Palette Asset.</param>
 /// <param name="cellSize">Cell Size of the Palette Asset.</param>
 /// <param name="swizzle">Cell Swizzle of the Palette.</param>
 /// <returns>The created Palette Asset if successful.</returns>
 public static GameObject CreateNewPalette(string folderPath
                                           , string name
                                           , GridLayout.CellLayout layout
                                           , GridPalette.CellSizing cellSizing
                                           , Vector3 cellSize
                                           , GridLayout.CellSwizzle swizzle)
 {
     return(CreateNewPalette(folderPath, name, layout, cellSizing, cellSize, swizzle,
                             TransparencySortMode.Default, defaultSortAxis));
 }
Ejemplo n.º 4
0
        private static void CreateHexagonalTilemap(GridLayout.CellSwizzle swizzle, string undoMessage)
        {
            var root       = FindOrCreateRootGrid();
            var uniqueName = GameObjectUtility.GetUniqueNameForSibling(root.transform, "Tilemap");
            var tilemapGO  = ObjectFactory.CreateGameObject(uniqueName, typeof(Tilemap), typeof(TilemapRenderer));

            tilemapGO.transform.SetParent(root.transform);
            tilemapGO.transform.position = Vector3.zero;
            var grid = root.GetComponent <Grid>();

            grid.cellLayout  = Grid.CellLayout.Hexagon;
            grid.cellSwizzle = swizzle;
            var tilemap = tilemapGO.GetComponent <Tilemap>();

            tilemap.tileAnchor = Vector3.zero;
            Undo.RegisterCreatedObjectUndo(tilemapGO, undoMessage);
        }
Ejemplo n.º 5
0
        public void CreatePalette_ReturnsValidPalettesWithGridAndChildTilemap(Grid.CellLayout layout,
                                                                              GridLayout.CellSwizzle swizzle)
        {
            int paletteCount = GridPalettes.palettes.Count;

            GameObject palette1 = GridPaletteUtility.CreateNewPalette(path, name, layout,
                                                                      GridPalette.CellSizing.Automatic, Vector3.one, swizzle);
            GameObject palette2 = GridPaletteUtility.CreateNewPalette(path2, name2, layout,
                                                                      GridPalette.CellSizing.Automatic, Vector3.one, swizzle);

            GridPalettes.CleanCache();
            Assert.AreEqual(paletteCount + 2, GridPalettes.palettes.Count);

            Assert.NotNull(palette1.GetComponent <Grid>());
            Assert.NotNull(palette1.GetComponentInChildren <Tilemap>());
            Assert.NotNull(palette2.GetComponent <Grid>());
            Assert.NotNull(palette2.GetComponentInChildren <Tilemap>());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a Palette Asset at the current selected folder path. This will show a popup allowing you to choose
        /// a different folder path for saving the Palette Asset if required.
        /// </summary>
        /// <param name="name">Name of the Palette Asset.</param>
        /// <param name="layout">Grid Layout of the Palette Asset.</param>
        /// <param name="cellSizing">Cell Sizing of the Palette Asset.</param>
        /// <param name="cellSize">Cell Size of the Palette Asset.</param>
        /// <param name="swizzle">Cell Swizzle of the Palette.</param>
        /// <param name="sortMode">Transparency Sort Mode for the Palette</param>
        /// <param name="sortAxis">Transparency Sort Axis for the Palette</param>
        /// <returns>The created Palette Asset if successful.</returns>
        public static GameObject CreateNewPaletteAtCurrentFolder(string name
                                                                 , GridLayout.CellLayout layout
                                                                 , GridPalette.CellSizing cellSizing
                                                                 , Vector3 cellSize
                                                                 , GridLayout.CellSwizzle swizzle
                                                                 , TransparencySortMode sortMode
                                                                 , Vector3 sortAxis)
        {
            string defaultPath = ProjectBrowser.s_LastInteractedProjectBrowser ? ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath() : "Assets";
            string folderPath  = EditorUtility.SaveFolderPanel("Create palette into folder ", defaultPath, "");

            folderPath = FileUtil.GetProjectRelativePath(folderPath);

            if (string.IsNullOrEmpty(folderPath))
            {
                return(null);
            }

            return(CreateNewPalette(folderPath, name, layout, cellSizing, cellSize, swizzle, sortMode, sortAxis));
        }
Ejemplo n.º 7
0
 internal static void PreferencesGUI()
 {
     using (new SettingsWindow.GUIScope())
     {
         EditorGUI.BeginChangeCheck();
         EditorGUI.BeginChangeCheck();
         var newCellSize = EditorGUILayout.Vector3Field(OpenTilemapInPrefabModeProperties.cellSizeLabel, cellSize);
         if (EditorGUI.EndChangeCheck())
         {
             cellSize = newCellSize;
         }
         EditorGUI.BeginChangeCheck();
         var newCellGap = EditorGUILayout.Vector3Field(OpenTilemapInPrefabModeProperties.cellGapLabel, cellGap);
         if (EditorGUI.EndChangeCheck())
         {
             cellGap = newCellGap;
         }
         EditorGUI.BeginChangeCheck();
         var newCellLayout = (GridLayout.CellLayout)EditorGUILayout.EnumPopup(OpenTilemapInPrefabModeProperties.cellLayoutLabel, cellLayout);
         if (EditorGUI.EndChangeCheck())
         {
             cellLayout = newCellLayout;
         }
         EditorGUI.BeginChangeCheck();
         var newCellSwizzle = (GridLayout.CellSwizzle)EditorGUILayout.EnumPopup(OpenTilemapInPrefabModeProperties.cellSwizzleLabel, cellSwizzle);
         if (EditorGUI.EndChangeCheck())
         {
             cellSwizzle = newCellSwizzle;
         }
         if (EditorGUI.EndChangeCheck() && m_Grid != null)
         {
             m_Grid.cellSize    = newCellSize;
             m_Grid.cellGap     = newCellGap;
             m_Grid.cellLayout  = newCellLayout;
             m_Grid.cellSwizzle = newCellSwizzle;
             SceneView.RepaintAll();
         }
     }
 }
Ejemplo n.º 8
0
        public void PalettePrefabIsUpdated_DoesNotCreateAnInstanceInScene(Grid.CellLayout layout,
                                                                          GridLayout.CellSwizzle swizzle)
        {
            var palettePrefab = GridPaletteUtility.CreateNewPalette(path, name, layout,
                                                                    GridPalette.CellSizing.Automatic, Vector3.one, swizzle);

            var w = CreatePaletteWindow();

            w.palette = palettePrefab;

            Object[] objs = GameObject.FindObjectsOfType <Grid>();
            Assert.AreEqual(0, objs.Length,
                            "There should be 0 Grids in this test as Palette instances have HideAndDontSave hide flags");

            var sceneGameObject = (GameObject)PrefabUtility.InstantiatePrefab(palettePrefab);
            var grid            = sceneGameObject.GetComponent <Grid>();

            Object[] objsAfterInstantiatePrefab = GameObject.FindObjectsOfType <Grid>();
            Assert.AreEqual(1, objsAfterInstantiatePrefab.Length,
                            "There should be 1 Grid in this test which is the instantiated prefab.");
            Assert.AreEqual(objsAfterInstantiatePrefab[0], grid,
                            "The Grid found should be the Grid instantiated from the prefab.");

            grid.cellGap = new Vector3(2.0f, 2.0f, 0.0f);

            PrefabUtility.SaveAsPrefabAssetAndConnect(sceneGameObject, AssetDatabase.GetAssetPath(palettePrefab),
                                                      InteractionMode.AutomatedAction);

            Object[] objsAfterReplacePrefab = GameObject.FindObjectsOfType <Grid>();
            Assert.AreEqual(1, objsAfterReplacePrefab.Length,
                            "There should be 1 Grid in this test which is the instantiated prefab after replacing the prefab.");
            Assert.AreEqual(objsAfterReplacePrefab[0], grid,
                            "The Grid found should be the Grid instantiated from the prefab after replacing the prefab.");

            // Clean up
            Object.DestroyImmediate(sceneGameObject);
        }
Ejemplo n.º 9
0
        public void PaintDifferentSizedTilesOnAutomaticCellSizePalette_UpdatesPaletteCellSize(Grid.CellLayout layout,
                                                                                              GridLayout.CellSwizzle swizzle, int tileX, int tileY, float expectedCellXSize, float expectedCellYSize)
        {
            var smallTile = AssetDatabase.LoadAssetAtPath <Tile>(tilePath);
            var bigTile   = AssetDatabase.LoadAssetAtPath <Tile>(bigTilePath);

            var newPalette = GridPaletteUtility.CreateNewPalette(path, name, layout, GridPalette.CellSizing.Automatic,
                                                                 Vector3.one, swizzle);
            var tilemap = newPalette.GetComponentInChildren <Tilemap>();

            tilemap.SetTile(new Vector3Int(tileX, tileY, 0), bigTile);

            var w = CreatePaletteWindow();

            w.palette = newPalette;
            w.clipboardView.previewUtility.camera.orthographicSize = 1000;
            w.clipboardView.ClampZoomAndPan();
            Assert.GreaterOrEqual(GridPaletteBrushes.brushes.Count, 1);
            GridPaintingState.defaultBrush.Init(new Vector3Int(1, 1, 1));
            GridPaintingState.defaultBrush.SetTile(Vector3Int.zero, smallTile);
            w.clipboardView.unlocked = true;
            TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));

            Event ev = new Event();

            ev.mousePosition = w.clipboardView.GridToScreen(new Vector2(tileX + 1.5f, tileY + 1.5f)) +
                               new Vector2(0, paletteHeaderHeight);
            ev.type = EventType.MouseMove;
            w.SendEvent(ev);
            ev.type = EventType.MouseDown;
            w.SendEvent(ev);
            ev.type = EventType.MouseUp;
            w.SendEvent(ev);
            w.clipboardView.unlocked = false;

            var grid = w.palette.GetComponent <Grid>();

            Assert.AreEqual(expectedCellXSize, grid.cellSize.x);
            Assert.AreEqual(expectedCellYSize, grid.cellSize.y);
        }
Ejemplo n.º 10
0
 private static extern void InverseSwizzle_Injected(GridLayout.CellSwizzle swizzle, ref Vector3 position, out Vector3 ret);
Ejemplo n.º 11
0
 public extern static Vector3 InverseSwizzle(GridLayout.CellSwizzle swizzle, Vector3 position);
        static void CreateAssetObject(string defaultAssetName, GridLayout.CellLayout layout, GridLayout.CellSwizzle swizzle, GridPalette.CellSizing cellSizing, Vector3 cellSize)
        {
            var assetSelectionPath = AssetDatabase.GetAssetPath(Selection.activeObject);
            var isFolder           = false;

            if (!string.IsNullOrEmpty(assetSelectionPath))
            {
                isFolder = File.GetAttributes(assetSelectionPath).HasFlag(FileAttributes.Directory);
            }
            var path = ProjectWindowUtil.GetActiveFolderPath();

            if (isFolder)
            {
                path = assetSelectionPath;
            }

            var destName = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(path, defaultAssetName));
            var icon     = EditorGUIUtility.IconContent <GameObject>().image as Texture2D;
            CreateAssetEndNameEditAction action = ScriptableObject.CreateInstance <CreateAssetEndNameEditAction>();

            action.swizzle    = swizzle;
            action.layout     = layout;
            action.cellSize   = cellSize;
            action.cellSizing = cellSizing;
            StartNewAssetNameEditingDelegate(0, action, destName, icon, "");
        }