2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

徐々に大きくなる正方形の順番に処理をする

2
Last updated at Posted at 2025-11-21

何を言ってる?

座標を入力とした処理を、画像の番号順で実行したかったのです。
image.png

コード

using System;
public static class GrowSquare
{
    public static void RunWithSquareIndex(int squareSize, Action<(int x, int y)> action)
    {
        for (int s = 0; s < squareSize; s++)
        {
            // 0 ~ s-1まで
            for (int y = 0; y < s; y++) action(new(s, y));
            // s ~ 0まで
            for (int x = s; x >= 0; x--) action(new(x, s));
        }
    }
    public static int GetIndexAt(int x, int y)
    {
        if (x == y) return x * (x + 1);
        else if (x > y) return x * x + y;
        else return y * (y + 2) - x;
    }
    public static (int x,int y) GetXYFromIndex(int index)
    {
        int s = (int)MathF.Sqrt(index);//正方形の一番右のx座標 (切り捨て変換)
        int s0 = s * s;// (s,0) のインデックス
        int ss = s0 + s;// (s,s)のインデックス
        if (index < ss) return (s, index - s0);
        else if (index > ss) return (s - (index - ss), s);
        else return (s, s);
        
    }
}

デバッグ用コード

UnityでなんかのGameObjectに張り付けてください。冒頭の画像と同じ画面になるはずです。

using UnityEngine;
using UnityEngine.InputSystem;

public class IntGridViewer : MonoBehaviour
{ 
    const int size = 64;
    const int cellSize = 28;

    Vector2 offset = Vector2.zero;  // GUI の移動量

    bool dragging = false;
    Vector2 lastMousePos;
    void Awake()
    {
        GrowSquare.RunWithSquareIndex(size,(cood) =>Debug.Log($"({cood.x}, {cood.y} => {GrowSquare.GetIndexAt(cood.x, cood.y)})"));
    }

    void Update()
    {
        var mouse = Mouse.current;
        if (mouse == null) return;

        // ★ マウスホイールスクロール(Input System)
        Vector2 scroll = mouse.scroll.ReadValue();  // (x=水平, y=垂直)

        if (Keyboard.current.shiftKey.isPressed)
        {
            // SHIFT + ホイール → 左右
            offset.x += scroll.y * 2f;
        }
        else
        {
            // 通常 → 上下
            offset.y += scroll.y * 2f;
        }

        // ★ 右ドラッグでパン
        if (mouse.rightButton.wasPressedThisFrame)
        {
            dragging = true;
            lastMousePos = mouse.position.ReadValue();
        }
        else if (mouse.rightButton.wasReleasedThisFrame)
        {
            dragging = false;
        }

        if (dragging)
        {
            Vector2 now = mouse.position.ReadValue();
            Vector2 delta = now - lastMousePos;
            offset += delta;
            lastMousePos = now;
        }
    }

    void OnGUI()
    {        // ★ GUI全体をオフセットさせる
        // ★ GUI 全体をオフセット(パン)
        Matrix4x4 old = GUI.matrix;
        GUI.matrix = Matrix4x4.Translate(offset) * old;

        // ★ GUIStyle を OnGUI 内で生成
        GUIStyle cellStyle = new GUIStyle(GUI.skin.box)
        {
            margin = new RectOffset(0, 0, 0, 0),
            padding = new RectOffset(0, 0, 0, 0),
            alignment = TextAnchor.MiddleCenter,
            fontSize = 12,
            fontStyle = FontStyle.Bold
        };
        cellStyle.normal.textColor = Color.black;

        float startX = 10;
        float startY = 10;

        GrowSquare.RunWithSquareIndex(size, (cood) =>
        {
            int x = cood.x;
            int y = cood.y;
            int v = GrowSquare.GetIndexAt(x,y);

            Rect r = new Rect(
                startX + x * cellSize,
                startY + y * cellSize,
                cellSize,
                cellSize
            );


            // 描画&クリック処理(あれば)
            if (GUI.Button(r, v.ToString(), cellStyle))
            {
                //クリック処理
            }
        });

        GUI.backgroundColor = Color.white;
        GUI.matrix = old;
    }
}

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?