LoginSignup
4
5

More than 3 years have passed since last update.

【Unity】Gizmosで円や正多角形を描く

Posted at

バージョン

Unity 2019.1.9f1

はじめに

2Dゲームを作っているときなど、簡単な目印なんかのために、Gizmosで円や正三角形を描きたくなる時があるかもしれません。
そんなときには本クラスをどうぞ。

ソースコード

下記GitHubに上げてあります。
unity-gizmos-utility

使い方

こんな感じです。
screenshot.png

GizmosTest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GizmosTest : MonoBehaviour
{
    private void OnDrawGizmos()
    {
        Gizmos.matrix = transform.localToWorldMatrix;

        Gizmos.color = Color.green;
        // 円(2D)
        GizmosUtility.DrawWireCircle(Vector3.zero, 2f);
        // 正三角形(2D)
        GizmosUtility.DrawWireRegularPolygon(3, new Vector3(4f, 0f, 0f), 2f);
        // 正四角形(2D)
        GizmosUtility.DrawWireRegularPolygon(4, new Vector3(8f, 0f, 0f), 2f);
        // 正五角形(2D)
        GizmosUtility.DrawWireRegularPolygon(5, new Vector3(12f, 0f, 0f), 2f);

        Gizmos.color = Color.red;
        // 円(3D)
        GizmosUtility.DrawWireCircle(new Vector3(16f, 0f, 0f), Quaternion.LookRotation(new Vector3(-1f, 1f, 0f)), 2f);
        // 正三角形(3D)
        GizmosUtility.DrawWireRegularPolygon(3, new Vector3(20f, 0f, 0f), Quaternion.LookRotation(new Vector3(-1f, 1f, 0f)), 2f);
        // 正四角形(3D)
        GizmosUtility.DrawWireRegularPolygon(4, new Vector3(24f, 0f, 0f), Quaternion.LookRotation(new Vector3(-1f, 1f, 0f)), 2f);
        // 正五角形(3D)
        GizmosUtility.DrawWireRegularPolygon(5, new Vector3(28f, 0f, 0f), Quaternion.LookRotation(new Vector3(-1f, 1f, 0f)), 2f);
    }
}
4
5
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
4
5