Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if UNITY_EDITOR
- /**
- * AreaEditor
- *
- * Custom editor for Area scriptable object representing area of effect (for abilities and other effects).
- */
- [CustomEditor(typeof(Area))]
- public class AreaEditor : UnityEditor.Editor {
- private VisualElement _root;
- private PropertyField _needsLOSField;
- private PropertyField _areaSizeField;
- // private PropertyField affectedTilesField;
- private VisualElement _areaMatrix;
- private void OnEnable() {
- Undo.undoRedoPerformed += this.OnUndoRedo;
- }
- private void OnDisable() {
- Undo.undoRedoPerformed -= this.OnUndoRedo;
- }
- public override VisualElement CreateInspectorGUI() {
- this._root = new VisualElement();
- // needsLOS property
- this._needsLOSField = new PropertyField(this.serializedObject.FindProperty("_needsLOS"));
- this._needsLOSField.label = "Needs line of sight";
- this._root.Add(this._needsLOSField);
- // area size
- this._areaSizeField = new PropertyField(this.serializedObject.FindProperty("_areaSize"));
- this._areaSizeField.label = "Area size";
- this._areaSizeField.RegisterValueChangeCallback((evt) => {
- this._areaMatrix.Clear();
- this._areaMatrix.Add(this.CreateAreaMatrix());
- });
- this._root.Add(this._areaSizeField);
- // create a matrix of buttons for editing affected tiles
- this._areaMatrix = new VisualElement();
- this._areaMatrix.Add(this.CreateAreaMatrix());
- this._root.Add(this._areaMatrix);
- return this._root;
- }
- private VisualElement CreateAreaMatrix() {
- VisualElement matrix = new VisualElement();
- matrix.style.flexDirection = FlexDirection.Row;
- matrix.style.flexWrap = Wrap.Wrap;
- Foldout foldout = new Foldout();
- foldout.text = "Affected tiles";
- foldout.Q<VisualElement>("unity-content").style.marginLeft = 3;
- foldout.Add(matrix);
- Area area = (Area) this.target;
- Vector2Int areaSize = area.areaSize;
- Vector2Int centerSize = new Vector2Int(areaSize.x / 2, areaSize.y / 2);
- Color activeColor = Color.green;
- Color centerColor = Color.blue;
- VisualElement row;
- for (int x = 0; x < areaSize.x; ++x) {
- // create new row
- row = new VisualElement();
- matrix.Add(row);
- for (int y = 0; y < areaSize.y; ++y) {
- int aX = x;
- int aY = y;
- // for every column, create new button
- Button button = new Button();
- button.style.width = 20;
- button.style.height = 20;
- button.style.marginBottom = 0;
- button.style.marginRight = 0;
- button.style.marginTop = 0;
- button.style.marginLeft = 0;
- button.RegisterCallback<ClickEvent>((evt) => {
- // skip if its center tile
- if (new Vector2Int(aX, aY) == centerSize) {
- return;
- }
- this.HandleButtonPress(aX, aY, area);
- });
- // if its center tile then always mark it as active
- if (x == centerSize.x && y == centerSize.y) {
- button.style.backgroundColor = centerColor;
- } else {
- // check if this tile is in affected tiles
- Vector3Int tile = new Vector3Int(x - centerSize.x, 0, y - centerSize.y);
- if (area.affectedTiles != null && area.affectedTiles.Contains(tile)) {
- button.style.backgroundColor = activeColor;
- }
- }
- row.Add(button);
- }
- }
- return foldout;
- }
- private void HandleButtonPress(int aX, int aY, Area area) {
- Vector2Int areaSize = area.areaSize;
- Vector2Int centerSize = new Vector2Int(areaSize.x / 2, areaSize.y / 2);
- Vector3Int tile = new Vector3Int(aX - centerSize.x, 0, aY - centerSize.y);
- List<Vector3Int> affectedTiles = area.affectedTiles.ToList();
- if (affectedTiles.Contains(tile)) {
- affectedTiles.Remove(tile);
- } else {
- affectedTiles.Add(tile);
- }
- Undo.RecordObject(area, "Change affected tiles");
- area.SetAffectedTiles(affectedTiles);
- EditorUtility.SetDirty(area);
- this._areaMatrix.Clear();
- this._areaMatrix.Add(this.CreateAreaMatrix());
- this.serializedObject.Update();
- }
- private void OnUndoRedo() {
- this._areaMatrix.Clear();
- this._areaMatrix.Add(this.CreateAreaMatrix());
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement