Files
Main/Assets/Editor/DIY/JiaJuEditor/JiaJuEditor.cs
2025-01-25 04:38:09 +08:00

137 lines
4.4 KiB
C#

using UnityEngine;
using UnityEditor;
public class JiaJuEditor : EditorWindow
{
[MenuItem("Ares/家具编辑器")]
static void DoIt()
{
var win = (JiaJuEditor)EditorWindow.GetWindow(typeof(JiaJuEditor));
win.Show();
}
private string _outText = string.Empty;
private int _colCount = 1;
private int _rowCount = 1;
private float _posX = 0;
private float _posZ = 0;
private float _rotY = 0;
private float _cellSize = 1;
private bool[] _cellBlocks = null;
private void OnGUI()
{
EditorGUILayout.BeginVertical();
GUILayout.TextField(_outText);
EditorGUILayout.BeginHorizontal();
GUILayout.Label("行");
_rowCount = EditorGUILayout.IntField(_rowCount);
GUILayout.Label("列");
_colCount = EditorGUILayout.IntField(_colCount);
GUILayout.Label("格子大小");
_cellSize = EditorGUILayout.FloatField(_cellSize);
EditorGUILayout.EndHorizontal();
var modelGo = GameObject.Find("[JiaJuEditorModel]");
if (modelGo == null)
{
modelGo = new GameObject("[JiaJuEditorModel]");
}
EditorGUILayout.BeginHorizontal();
_posX = EditorGUILayout.FloatField("X", _posX);
_posZ = EditorGUILayout.FloatField("Z", _posZ);
_rotY = EditorGUILayout.FloatField("旋转", _rotY);
EditorGUILayout.EndHorizontal();
modelGo.transform.localPosition = new Vector3(_posX, 0, _posZ);
modelGo.transform.localEulerAngles = new Vector3(0, _rotY, 0);
var spr = modelGo.GetComponent<GizmosMono>();
if (spr == null)
{
spr = modelGo.AddComponent<GizmosMono>();
}
spr.parent = this;
if (_colCount <= 0)
{
_colCount = 1;
}
if (_rowCount <= 0)
{
_rowCount = 1;
}
if (_cellBlocks == null || _cellBlocks.Length != _colCount * _rowCount)
{
_cellBlocks = new bool[_colCount * _rowCount];
for(int i = 0; i < _cellBlocks.Length; ++i)
{
_cellBlocks[i] = true;
}
}
for(int row = _rowCount - 1; row >= 0; --row)
{
EditorGUILayout.BeginHorizontal();
for(int col = 0; col < _colCount; ++col)
{
var index = row * _colCount + col;
if (_cellBlocks[index])
{
GUI.color = Color.red;
if (GUILayout.Button(index.ToString(), GUILayout.Width(50), GUILayout.Height(50)))
{
_cellBlocks[index] = false;
}
}
else
{
GUI.color = Color.white;
if (GUILayout.Button(index.ToString(), GUILayout.Width(50), GUILayout.Height(50)))
{
_cellBlocks[index] = true;
}
}
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
_outText = string.Format("{0}_{1}_{2}_{3}_{4}", _rowCount, _colCount, _posX, _posZ, _rotY);
for(int i = 0; i < _cellBlocks.Length; ++i)
{
if(_cellBlocks[i] == false)
{
_outText = _outText + string.Format("_{0}", i);
}
}
}
public class GizmosMono : MonoBehaviour
{
public JiaJuEditor parent = null;
void OnDrawGizmos()
{
if (parent == null)
return;
float hsize = parent._cellSize * 0.9f;
Vector3 blockSize = new Vector3(hsize, hsize / 10.0f, hsize);
Gizmos.matrix = Matrix4x4.identity;
for (int i = 0; i < parent._rowCount; ++i)
{
for (int j = 0; j < parent._colCount; ++j)
{
var index = i * parent._colCount + j;
if(parent._cellBlocks[index])
{
Gizmos.color = Color.red;
}
else
{
Gizmos.color = Color.gray;
}
Gizmos.DrawCube(new Vector3(j * parent._cellSize + parent._cellSize / 2, 0, i * parent._cellSize + parent._cellSize / 2), blockSize);
}
}
}
}
}