4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unity Editor拡張】Camera選択時にSceneビューにグリッドを描画する

Posted at

Sceneビューにグリッドを出したくなったので作りました。

image

ソースコード

CameraEditor.cs
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Camera))]
public class CameraEditor : Editor
{
    void OnSceneGUI()
    {
        //SceneビューのCamera取得
        Camera camera = SceneView.GetAllSceneCameras()[0];

        //グリッド線の中心座標
        Vector3 pos = camera.transform.position;

        //Debug.DrawLine()は原点から離れすぎるとなぜか表示されなくなるので中心を原点に近づける
        pos += camera.transform.forward * pos.magnitude;

        float size = 99999f;
        Debug.DrawLine(pos - camera.transform.right * size, pos + camera.transform.right * size, Color.yellow);
        Debug.DrawLine(pos - camera.transform.up * size,    pos  + camera.transform.up * size, Color.yellow);

        //Sceneビュー更新
        EditorUtility.SetDirty(target);
    }

    //Cameraからフォーカスが外れたときに実行
    void OnDisable()
    {
        //Sceneビュー更新
        EditorUtility.SetDirty(target);
    }
}

参考

UnityのSceneViewにラベルやボタンを表示する - テラシュールブログ
http://tsubakit1.hateblo.jp/entry/2014/07/26/220458

けいごのなんとか - SceneViewクラスについて
http://anchan828.hatenablog.jp/entry/2013/03/13/004726

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?