using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.Tilemaps;
namespace UnityEditor.Tilemaps
{
///
/// Class containing utility methods for AutoTile Template
///
public static class AutoTileTemplateUtility
{
///
/// Loads an AutoTileTemplate from an asset file with a File Panel.
///
/// AutoTIleTemplate from asset file.
public static AutoTileTemplate LoadTemplateFromFile()
{
var projectWindowUtilType = typeof(ProjectWindowUtil);
var getActiveFolderPath =
projectWindowUtilType.GetMethod("GetActiveFolderPath", BindingFlags.Static | BindingFlags.NonPublic);
var obj = getActiveFolderPath.Invoke(null, new object[0]);
var pathToCurrentFolder = obj.ToString();
var templatePath = EditorUtility.OpenFilePanel("Load AutoTile template", pathToCurrentFolder,
AutoTileTemplate.kExtension);
var relativePath = FileUtil.GetProjectRelativePath(templatePath);
var template = AssetDatabase.LoadAssetAtPath(relativePath);
if (template == null)
{
Debug.LogWarningFormat("{0} does not contain a valid AutoTileTemplate.", relativePath);
}
return template;
}
///
/// Applies an AutoTileTemplate to an AutoTile with a source Texture2D
///
/// AutoTileTemplate to apply.
/// Source Texture2D containing Sprites for the AutoTileTemplate.
/// Source Sprites to be used in the AutoTile.
/// AutoTile updated with AutoTileTemplate.
/// Match Sprites from Source exactly with positional data from AutoTileTemplate
/// or match based on relative positional size.
public static void ApplyTemplateToAutoTile(this AutoTileTemplate template
, Texture2D texture
, IEnumerable sprites
, AutoTile autoTile
, bool matchExact = false)
{
if (template == null || texture == null || autoTile == null)
return;
autoTile.m_MaskType = template.maskType;
if (autoTile.m_TextureList == null)
autoTile.m_TextureList = new List();
if (autoTile.m_TextureScaleList == null)
autoTile.m_TextureScaleList = new List();
autoTile.m_TextureList.Add(texture);
autoTile.m_TextureScaleList.Add(AutoTile.s_DefaultTextureScale);
foreach (var sprite in sprites)
{
foreach (var templateSprite in template.sprites)
{
var match = false;
if (matchExact)
{
match = Mathf.Approximately(templateSprite.x, sprite.rect.x)
&& Mathf.Approximately(templateSprite.y, sprite.rect.y);
}
else
{
match = Mathf.Approximately(templateSprite.x / template.width, sprite.rect.x / texture.width)
&& Mathf.Approximately(templateSprite.y / template.height, sprite.rect.y / texture.height);
}
if (match)
{
autoTile.AddSprite(sprite, texture, templateSprite.mask);
break;
}
}
}
}
///
/// Creates an AutoTileTemplate with the given parameters.
///
/// Width of original image.
/// Height of original image.
/// Mask Type to apply to AutoTile.
/// Positional Data for AutoTileTemplate based on original image.
/// AutoTileTemplate generated with the given parameters.
public static AutoTileTemplate CreateTemplate(int imageWidth
, int imageHeight
, AutoTile.AutoTileMaskType maskType
, List spriteData)
{
var template = ScriptableObject.CreateInstance();
template.width = imageWidth;
template.height = imageHeight;
template.maskType = maskType;
template.sprites = spriteData;
return template;
}
///
/// Creates and saves an AutoTileTemplate with a FilePanel.
///
/// Width of original image.
/// Height of original image.
/// Mask Type to apply to AutoTile.
/// Positional Data for AutoTileTemplate based on original image.
public static void SaveTemplateToFile(int imageWidth
, int imageHeight
, AutoTile.AutoTileMaskType maskType
, List spriteData)
{
var template = CreateTemplate(imageWidth, imageHeight, maskType, spriteData);
var path = EditorUtility.SaveFilePanelInProject("Save AutoTile template", "New AutoTile Template", AutoTileTemplate.kExtension, "");
if (!String.IsNullOrWhiteSpace(path))
AssetDatabase.CreateAsset(template, path);
}
}
}