LoginSignup
0
1

More than 1 year has passed since last update.

Unity Editorの拡張 Vector3の座標番号をシーンから確認できるようにする 2/3

Last updated at Posted at 2023-03-23

概要

前回の続きです。
今回は点を識別するためにシーン上にテキストを表示します。下画像参照

image.png

開発環境

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

コード

Waypoint.cs

// 座標を定義しているクラス、オブジェクトにアタッチする
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()
    {
        
    }
}
WaypointEditor.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

[CustomEditor(typeof(Waypoint))]
public class WaypointEditor : Editor
{
    private Waypoint Waypoint => target as Waypoint;
    private void OnSceneGUI()
    {
        
        Handles.color = Color.red;
        for (int i = 0; i < Waypoint.Points.Length; i++)
        {
            EditorGUI.BeginChangeCheck();
            
            // シーンのGUI上の座標
            // 例えば、Waypoint.Point[i]= new Vector3(0,0,0)だとすると、点が原点に密集し変更できない
            Vector3 currentWaypointPoint = Waypoint.Points[i];
            Vector3 newWaypointPoint = Handles.FreeMoveHandle(currentWaypointPoint,
                Quaternion.identity, 0.7f,new Vector3(0.3f,0.3f, 0.3f), Handles.SphereHandleCap);
            
            // テキストの作成           
+            GUIStyle textStyle = new GUIStyle();
+            textStyle.fontStyle = FontStyle.Bold;
+            textStyle.fontSize = 16;
+            textStyle.normal.textColor = Color.yellow;           
+            Handles.Label(Waypoint.Points[i], $"{i + 1}", textStyle);

            if (EditorGUI.EndChangeCheck())
            {
                // インスペクター上のベクトルの座標
                Waypoint.Points[i] = newWaypointPoint;
            }
        }
    }
}

参考

image.png

image.png

githubコミット分

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