TiledTerrain/Assets/Tile Terrain/Editor/TerrainFloorRendererEditor.cs

82 lines
2.1 KiB
C#

using System;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace BitGames.Terrain
{
[CustomEditor(typeof(TerrainFloorRenderer))]
public class TerrainFloorRendererEditor : Editor
{
private SerializedProperty _asset;
private TerrainFloorRenderer _target => target as TerrainFloorRenderer;
private static readonly string[] _textureSizeOptions = RuntimeGlobals.SizeOptions.Select(x => $"{x.TextureSize}x{x.TextureSize}").ToArray();
private int _textureSizeOption = 1;
private void OnEnable()
{
_asset = serializedObject.FindProperty(nameof(TerrainFloorRenderer.Asset));
_target.HideMeshFilterRenderer();
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(_asset);
if (_target.Asset != null)
RenderEditGUI();
else
RenderCreateGUI();
}
private void RenderEditGUI()
{
}
private void RenderCreateGUI()
{
_textureSizeOption = EditorGUILayout.Popup("Size:", _textureSizeOption, _textureSizeOptions);
if (GUILayout.Button("Create Terrain Asset"))
{
CreateAsset();
_target.OnValidate();
}
}
private SizeOption GetSizeOption()
{
var options = RuntimeGlobals.SizeOptions;
return options[Math.Clamp(_textureSizeOption, 0, options.Length - 1)];
}
void CreateAsset()
{
Scene scene = _target.gameObject.scene;
var scenePath = Path.GetDirectoryName(scene.path);
if (string.IsNullOrEmpty(scenePath))
scenePath = Application.dataPath;
var sceneName = scene.name;
if (string.IsNullOrEmpty(sceneName))
sceneName = "SceneFloorMap";
var path = EditorUtility.SaveFilePanel(
"Save terrain asset",
scenePath,
sceneName + "_Terrain.asset",
"asset");
var relativePath = Path.GetRelativePath(".", path);
if (path.Length == 0)
return;
_target.Asset = TerrainAsset.Create(relativePath, GetSizeOption().TextureSize);
}
}
}