LoginSignup
1
1

More than 1 year has passed since last update.

Unity Editorの拡張 3/3 隣接する点を線でつなぐ

Last updated at Posted at 2023-03-25

概要

今回は各点を結ぶ線描画を加えます。線を加えることで移動経路がわかりやすくなります。
以下画像

image.png

開発環境

IDE:Rider
Unity:2020.3.42(LTS)
OS:Windows10

実装のポイント

GizmoクラスとMonoBehaviourクラスのOnDrawGizmosメソッドを使います。
Gizmosクラスはシーンの視覚的にデバッグしやすくするために使われます。
OnDrawGizmosメソッドはシーンのGUIをいじった時に実行されるメソッドです。再生、非再生問わずに実行されます。

コード部分

Waypoint.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

// 座標を定義しているクラス、オブジェクトにアタッチする
public class Waypoint : MonoBehaviour
{
    // Start is called before the first frame update
    [SerializeField] private Vector3[] points;
    public Vector3[] Points => points;

    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
    }

+    private void OnDrawGizmos()
+    {
+        for (int i = 0; i < points.Length; i++)
+        {
+            if (i < points.Length - 1)
+            {
+                Gizmos.color = Color.yellow;
+                // 隣接する2点間の距離を引く
+                Gizmos.DrawLine(points[i], points[i + 1]);
+            }
+        }
+    }
}


参考

Gizmoクラス

image.png

OnDrawGizmosメソッドについて

github コミット分

1
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
1
1