Advertisement
Guest User

Untitled

a guest
May 27th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. #if UNITY_EDITOR
  2.   /**
  3.    * AreaEditor
  4.    *
  5.    * Custom editor for Area scriptable object representing area of effect (for abilities and other effects).
  6.    */
  7.   [CustomEditor(typeof(Area))]
  8.   public class AreaEditor : UnityEditor.Editor {
  9.  
  10.     private VisualElement _root;
  11.     private PropertyField _needsLOSField;
  12.     private PropertyField _areaSizeField;
  13.     // private PropertyField affectedTilesField;
  14.     private VisualElement _areaMatrix;
  15.  
  16.     private void OnEnable() {
  17.       Undo.undoRedoPerformed += this.OnUndoRedo;
  18.     }
  19.  
  20.     private void OnDisable() {
  21.       Undo.undoRedoPerformed -= this.OnUndoRedo;
  22.     }
  23.  
  24.     public override VisualElement CreateInspectorGUI() {
  25.       this._root = new VisualElement();
  26.  
  27.       // needsLOS property
  28.       this._needsLOSField = new PropertyField(this.serializedObject.FindProperty("_needsLOS"));
  29.       this._needsLOSField.label = "Needs line of sight";
  30.       this._root.Add(this._needsLOSField);
  31.  
  32.       // area size
  33.       this._areaSizeField = new PropertyField(this.serializedObject.FindProperty("_areaSize"));
  34.       this._areaSizeField.label = "Area size";
  35.       this._areaSizeField.RegisterValueChangeCallback((evt) => {
  36.         this._areaMatrix.Clear();
  37.         this._areaMatrix.Add(this.CreateAreaMatrix());
  38.       });
  39.       this._root.Add(this._areaSizeField);
  40.      
  41.       // create a matrix of buttons for editing affected tiles
  42.       this._areaMatrix = new VisualElement();
  43.       this._areaMatrix.Add(this.CreateAreaMatrix());
  44.       this._root.Add(this._areaMatrix);
  45.      
  46.       return this._root;
  47.     }
  48.  
  49.     private VisualElement CreateAreaMatrix() {
  50.       VisualElement matrix = new VisualElement();
  51.       matrix.style.flexDirection = FlexDirection.Row;
  52.       matrix.style.flexWrap = Wrap.Wrap;
  53.  
  54.       Foldout foldout = new Foldout();
  55.       foldout.text = "Affected tiles";
  56.       foldout.Q<VisualElement>("unity-content").style.marginLeft = 3;
  57.       foldout.Add(matrix);
  58.  
  59.       Area area = (Area) this.target;
  60.       Vector2Int areaSize = area.areaSize;
  61.       Vector2Int centerSize = new Vector2Int(areaSize.x / 2, areaSize.y / 2);
  62.  
  63.       Color activeColor = Color.green;
  64.       Color centerColor = Color.blue;
  65.  
  66.       VisualElement row;
  67.  
  68.       for (int x = 0; x < areaSize.x; ++x) {
  69.         // create new row
  70.         row = new VisualElement();
  71.         matrix.Add(row);
  72.  
  73.         for (int y = 0; y < areaSize.y; ++y) {
  74.           int aX = x;
  75.           int aY = y;
  76.  
  77.           // for every column, create new button
  78.           Button button = new Button();
  79.           button.style.width = 20;
  80.           button.style.height = 20;
  81.           button.style.marginBottom = 0;
  82.           button.style.marginRight = 0;
  83.           button.style.marginTop = 0;
  84.           button.style.marginLeft = 0;
  85.           button.RegisterCallback<ClickEvent>((evt) => {
  86.             // skip if its center tile
  87.             if (new Vector2Int(aX, aY) == centerSize) {
  88.               return;
  89.             }
  90.  
  91.             this.HandleButtonPress(aX, aY, area);
  92.           });
  93.  
  94.           // if its center tile then always mark it as active
  95.           if (x == centerSize.x && y == centerSize.y) {
  96.             button.style.backgroundColor = centerColor;
  97.           } else {
  98.             // check if this tile is in affected tiles
  99.             Vector3Int tile = new Vector3Int(x - centerSize.x, 0, y - centerSize.y);
  100.             if (area.affectedTiles != null && area.affectedTiles.Contains(tile)) {
  101.               button.style.backgroundColor = activeColor;
  102.             }
  103.           }
  104.  
  105.           row.Add(button);
  106.         }
  107.  
  108.       }
  109.  
  110.       return foldout;
  111.     }
  112.  
  113.     private void HandleButtonPress(int aX, int aY, Area area) {
  114.       Vector2Int areaSize = area.areaSize;
  115.       Vector2Int centerSize = new Vector2Int(areaSize.x / 2, areaSize.y / 2);
  116.       Vector3Int tile = new Vector3Int(aX - centerSize.x, 0, aY - centerSize.y);
  117.  
  118.       List<Vector3Int> affectedTiles = area.affectedTiles.ToList();
  119.       if (affectedTiles.Contains(tile)) {
  120.         affectedTiles.Remove(tile);
  121.       } else {
  122.         affectedTiles.Add(tile);
  123.       }
  124.  
  125.       Undo.RecordObject(area, "Change affected tiles");
  126.       area.SetAffectedTiles(affectedTiles);
  127.       EditorUtility.SetDirty(area);
  128.  
  129.       this._areaMatrix.Clear();
  130.       this._areaMatrix.Add(this.CreateAreaMatrix());
  131.  
  132.       this.serializedObject.Update();
  133.     }
  134.  
  135.     private void OnUndoRedo() {
  136.       this._areaMatrix.Clear();
  137.       this._areaMatrix.Add(this.CreateAreaMatrix());
  138.     }
  139.  
  140.   }
  141.  #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement