Use the RuleTile editor for a custom tool

Dear developpers,

I am working on a custom inspector to serialize a custom CardEffect class. One of my effect types allows the player to chose a target in a certain range of cells, the range being determined by an EditorGUILayout.IntSlider .

I would really like to have a preview of this range as it is done in the RuleTile inspector, like this.8257935--1080984--upload_2022-7-6_15-55-18.png

Since we can now use most of the Editor’s API, like nodes and other graph views, is there any way to mimic this view ? I could not find anything in the documentation.

Best regards !

I actually found a solution !

8258373--1081065--upload_2022-7-6_18-18-4.png

Range = EditorGUILayout.IntSlider("Targeting zone range", Range, 1, 10);

            //Container rect, needed because GetRect will override width and match the view size
            var containerRect = GUILayoutUtility.GetRect(300, 300);

            //Actual grid rect, that will center the grid
            var gridRect = new Rect(new Vector2(containerRect.width / 3, containerRect.position.y), new Vector2(280, 280));

            RuleMatrixOnGUI(gridRect, new BoundsInt(Vector3Int.zero, new Vector3Int(1 + Range * 2, 1 + Range * 2, 0)));
        }

        private void RuleMatrixOnGUI(Rect rect, BoundsInt bounds)
        {
            // Grid
            float d = rect.width / (bounds.size.x + bounds.size.y);
            for (int y = 0; y <= bounds.size.y; y++)
            {
                float left = rect.xMin + d * y;
                float top = rect.yMin + d * y;
                float right = rect.xMax - d * (bounds.size.y - y);
                float bottom = rect.yMax - d * (bounds.size.y - y);
                Handles.DrawLine(new Vector3(left, bottom), new Vector3(right, top));
            }
            for (int x = 0; x <= bounds.size.x; x++)
            {
                float left = rect.xMin + d * x;
                float top = rect.yMax - d * x;
                float right = rect.xMax - d * (bounds.size.x - x);
                float bottom = rect.yMin + d * (bounds.size.x - x);
                Handles.DrawLine(new Vector3(left, bottom), new Vector3(right, top));
            }

            Handles.color = Color.white;
        }
1 Like