testss
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityEditor.Tilemaps
|
||||
{
|
||||
internal static class DefaultAssetCreation
|
||||
{
|
||||
const int k_TilePaletteAssetMenuPriority = 4;
|
||||
|
||||
static internal Action<int, ProjectWindowCallback.EndNameEditAction, string, Texture2D, string> StartNewAssetNameEditingDelegate = ProjectWindowUtil.StartNameEditingIfProjectWindowExists;
|
||||
|
||||
[MenuItem("Assets/Create/2D/Tile Palette/Rectangular", priority = k_TilePaletteAssetMenuPriority)]
|
||||
static void MenuItem_AssetsCreate2DTilePaletteRectangular(MenuCommand menuCommand)
|
||||
{
|
||||
CreateAssetObject("Rectangular Palette", GridLayout.CellLayout.Rectangle, GridLayout.CellSwizzle.XYZ, GridPalette.CellSizing.Automatic, new Vector3(1, 1, 0));
|
||||
}
|
||||
|
||||
[MenuItem("Assets/Create/2D/Tile Palette/Hexagonal Flat-Top", priority = k_TilePaletteAssetMenuPriority)]
|
||||
static void MenuItem_AssetsCreate2DTilePaletteHexagonalFlatTop(MenuCommand menuCommand)
|
||||
{
|
||||
CreateAssetObject("Hexagon Flat-Top Palette", GridLayout.CellLayout.Hexagon, GridLayout.CellSwizzle.YXZ, GridPalette.CellSizing.Manual, new Vector3(0.8659766f, 1, 0));
|
||||
}
|
||||
|
||||
[MenuItem("Assets/Create/2D/Tile Palette/Hexagonal Pointed-Top", priority = k_TilePaletteAssetMenuPriority)]
|
||||
static void MenuItem_AssetsCreate2DTilePaletteHexagonalPointedTop(MenuCommand menuCommand)
|
||||
{
|
||||
CreateAssetObject("Hexagon Pointed-Top Palette", GridLayout.CellLayout.Hexagon, GridLayout.CellSwizzle.XYZ, GridPalette.CellSizing.Manual, new Vector3(0.8659766f, 1, 0));
|
||||
}
|
||||
|
||||
[MenuItem("Assets/Create/2D/Tile Palette/Isometric", priority = k_TilePaletteAssetMenuPriority)]
|
||||
static void MenuItem_AssetsCreate2DTilePaletteIsometric(MenuCommand menuCommand)
|
||||
{
|
||||
CreateAssetObject("Isometric Palette", GridLayout.CellLayout.Isometric, GridLayout.CellSwizzle.XYZ, GridPalette.CellSizing.Manual, new Vector3(1, 0.5f, 0));
|
||||
}
|
||||
|
||||
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, "");
|
||||
}
|
||||
|
||||
internal class CreateAssetEndNameEditAction : ProjectWindowCallback.EndNameEditAction
|
||||
{
|
||||
public GridLayout.CellLayout layout { get; set; }
|
||||
public GridLayout.CellSwizzle swizzle { get; set; }
|
||||
public Vector3 cellSize { get; set; }
|
||||
public GridPalette.CellSizing cellSizing { get; set; }
|
||||
|
||||
public override void Action(int instanceId, string pathName, string resourceFile)
|
||||
{
|
||||
GridPaletteUtility.CreateNewPalette(Path.GetDirectoryName(pathName), Path.GetFileName(pathName), layout,
|
||||
cellSizing, cellSize, swizzle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,124 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
namespace UnityEditor.Tilemaps
|
||||
{
|
||||
static class GameObjectCreation
|
||||
{
|
||||
private static class Styles
|
||||
{
|
||||
public static readonly string pointTopHexagonCreateUndo = L10n.Tr("Hexagonal Point Top Tilemap");
|
||||
public static readonly string flatTopHexagonCreateUndo = L10n.Tr("Hexagonal Flat Top Tilemap");
|
||||
public static readonly string isometricCreateUndo = L10n.Tr("Isometric Tilemap");
|
||||
public static readonly string isometricZAsYCreateUndo = L10n.Tr("Isometric Z As Y Tilemap");
|
||||
}
|
||||
|
||||
const int k_MenuPriority = 3;
|
||||
|
||||
[MenuItem("GameObject/2D Object/Tilemap/Rectangular", priority = k_MenuPriority)]
|
||||
internal static void CreateRectangularTilemap()
|
||||
{
|
||||
var root = FindOrCreateRootGrid();
|
||||
var uniqueName = GameObjectUtility.GetUniqueNameForSibling(root.transform, "Tilemap");
|
||||
var tilemapGO = ObjectFactory.CreateGameObject(uniqueName, typeof(Tilemap), typeof(TilemapRenderer));
|
||||
Undo.SetTransformParent(tilemapGO.transform, root.transform, "");
|
||||
tilemapGO.transform.position = Vector3.zero;
|
||||
|
||||
Selection.activeGameObject = tilemapGO;
|
||||
Undo.SetCurrentGroupName("Create Tilemap");
|
||||
}
|
||||
|
||||
[MenuItem("GameObject/2D Object/Tilemap/Hexagonal - Pointed-Top", priority = k_MenuPriority)]
|
||||
internal static void CreateHexagonalPointTopTilemap()
|
||||
{
|
||||
CreateHexagonalTilemap(GridLayout.CellSwizzle.XYZ, Styles.pointTopHexagonCreateUndo, new Vector3(0.8659766f, 1, 1));
|
||||
}
|
||||
|
||||
[MenuItem("GameObject/2D Object/Tilemap/Hexagonal - Flat-Top", priority = k_MenuPriority)]
|
||||
internal static void CreateHexagonalFlatTopTilemap()
|
||||
{
|
||||
CreateHexagonalTilemap(GridLayout.CellSwizzle.YXZ, Styles.flatTopHexagonCreateUndo, new Vector3(0.8659766f, 1, 1));
|
||||
}
|
||||
|
||||
[MenuItem("GameObject/2D Object/Tilemap/Isometric", priority = k_MenuPriority)]
|
||||
internal static void CreateIsometricTilemap()
|
||||
{
|
||||
CreateIsometricTilemap(GridLayout.CellLayout.Isometric, Styles.isometricCreateUndo);
|
||||
}
|
||||
|
||||
[MenuItem("GameObject/2D Object/Tilemap/Isometric Z as Y", priority = k_MenuPriority)]
|
||||
internal static void CreateIsometricZAsYTilemap()
|
||||
{
|
||||
CreateIsometricTilemap(GridLayout.CellLayout.IsometricZAsY, Styles.isometricZAsYCreateUndo);
|
||||
}
|
||||
|
||||
private static void CreateIsometricTilemap(GridLayout.CellLayout isometricLayout, 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>();
|
||||
// Case 1071703: Do not reset cell size if adding a new Tilemap to an existing Grid of the same layout
|
||||
if (isometricLayout != grid.cellLayout)
|
||||
{
|
||||
grid.cellLayout = isometricLayout;
|
||||
grid.cellSize = new Vector3(1.0f, 0.5f, 1.0f);
|
||||
}
|
||||
|
||||
var tilemapRenderer = tilemapGO.GetComponent<TilemapRenderer>();
|
||||
tilemapRenderer.sortOrder = TilemapRenderer.SortOrder.TopRight;
|
||||
|
||||
Selection.activeGameObject = tilemapGO;
|
||||
Undo.RegisterCreatedObjectUndo(tilemapGO, undoMessage);
|
||||
}
|
||||
|
||||
private static void CreateHexagonalTilemap(GridLayout.CellSwizzle swizzle, string undoMessage, Vector3 cellSize)
|
||||
{
|
||||
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;
|
||||
grid.cellSize = cellSize;
|
||||
var tilemap = tilemapGO.GetComponent<Tilemap>();
|
||||
tilemap.tileAnchor = Vector3.zero;
|
||||
Selection.activeGameObject = tilemapGO;
|
||||
Undo.RegisterCreatedObjectUndo(tilemapGO, undoMessage);
|
||||
}
|
||||
|
||||
private static GameObject FindOrCreateRootGrid()
|
||||
{
|
||||
GameObject gridGO = null;
|
||||
|
||||
// Check if active object has a Grid and can be a parent for the Tile Map
|
||||
if (Selection.activeObject is GameObject)
|
||||
{
|
||||
var go = (GameObject)Selection.activeObject;
|
||||
var parentGrid = go.GetComponentInParent<Grid>();
|
||||
if (parentGrid != null)
|
||||
{
|
||||
gridGO = parentGrid.gameObject;
|
||||
}
|
||||
}
|
||||
|
||||
// If neither the active object nor its parent has a grid, create a grid for the tilemap
|
||||
if (gridGO == null)
|
||||
{
|
||||
gridGO = ObjectFactory.CreateGameObject("Grid", typeof(Grid));
|
||||
gridGO.transform.position = Vector3.zero;
|
||||
|
||||
var grid = gridGO.GetComponent<Grid>();
|
||||
grid.cellSize = new Vector3(1.0f, 1.0f, 0.0f);
|
||||
Undo.SetCurrentGroupName("Create Grid");
|
||||
}
|
||||
|
||||
return gridGO;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user