🎃

UNITY editor の拡張 開発環境

2024/03/20に公開

editor

RAYCAST 視覚化 Debug.DrawRay

https://kan-kikuchi.hatenablog.com/entry/RayCast3

if (Input.GetMouseButtonDown (0)) {
  float distance = 100; // 飛ばす&表示するRayの長さ
  float duration = 3;   // 表示期間(秒)

  Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  Debug.DrawRay (ray.origin, ray.direction * distance, Color.red, duration, false);

  RaycastHit hit = new RaycastHit ();
  if (Physics.Raycast (ray, out hit, distance)) {
    GameObject hitObject = hit.collider.gameObject;
    // (以下略)
  }
}

https://fineworks-fine.hatenablog.com/entry/2022/11/17/073000

スクリプトからGizmosを描画する

https://fineworks-fine.hatenablog.com/entry/2023/06/17/073000

OnDrawGizmos 常に描画したい Gizmos を描画する場合
OnDrawGizmosSelected オブジェクトが選択されている場合に Gizmos を描画する

DrawCube Gizmos.DrawCube 中心とサイズを持つキューブを描画

public static void DrawCube (Vector3 center, Vector3 size);
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void OnDrawGizmosSelected()
    {
        // Draw a semitransparent blue cube at the transforms position
        Gizmos.color = new Color(1, 0, 0, 0.5f);
        Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1));
    }
}

Gizmos.DrawFrustum 角錐を描画

//centerは頂点の位置, fovは頂点の角度, maxRangeは離れた平面までの距離, minRangeは近くの平面までの距離, aspectは幅と高さの比
public static void DrawFrustum (Vector3 center, float fov, float maxRange, float minRange, float aspect);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GizmosTest : MonoBehaviour
{
    void OnDrawGizmosSelected()
    {
        Gizmos.color = new Color(1, 0, 0, 0.5f);
        Gizmos.DrawFrustum(transform.position, 60, 200, 0f, 1.6f);
    }
}

Gizmos.DrawGUITexture 指定した Textureを表示

public static void DrawGUITexture (Rect screenRect, Texture texture, Material mat= null);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GizmosTest : MonoBehaviour
{
    public Texture myTexture;

    void OnDrawGizmosSelected()
    {
        // Draw a semitransparent blue cube at the transforms position
        Gizmos.DrawGUITexture(new Rect(10, 10, 20, 20), myTexture);
    }
}

Gizmos.DrawIcon アイコン描画

アイコンに使用したい画像は Assets/Gizmos フォルダに置いておく必要があります。※第3引数が true の場合、近づくと拡大、遠ざかると縮小されます。false の場合、アイコンのサイズは変わりません。

//第1引数はアイコンの位置, 第2引数はアイコンに使用する画像の名前, 第3引数はスケーリング可能かどうか
public static void DrawIcon (Vector3 center, string name, bool allowScaling= true);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GizmosTest : MonoBehaviour
{
    void OnDrawGizmosSelected()
    {
        //第2引数は画像の名前
        //第3引数はスケーリング可能かどうか
        Gizmos.DrawIcon(transform.position, "Chara_solo1", true);
    }
}

Gizmos.DrawLine

第1引数で指定した位置から第2引数で指定した位置に向かって線分を描画

public static void DrawLine (Vector3 from, Vector3 to);
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public Transform target;

    void OnDrawGizmosSelected()
    {
        if (target != null)
        {
            // Draws a blue line from this transform to the target
            Gizmos.color = Color.blue;
            Gizmos.DrawLine(transform.position, target.position);
        }
    }
}

Gizmos.DrawMesh メッシュを描画

public static void DrawMesh (Mesh mesh, Vector3 position= Vector3.zero, Quaternion rotation= Quaternion.identity, Vector3 scale= Vector3.one);

※指定したメッシュによって、形が変わります。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GizmosTest : MonoBehaviour
{
    public Mesh mesh;

    void OnDrawGizmosSelected()
    {
        // Draws a blue line from this transform to the target
        Gizmos.color = Color.blue;
        Gizmos.DrawMesh(mesh, transform.position, Quaternion.identity, Vector3.one);
    }
}

Gizmos.DrawSphere 球を描画

引数から球の中心と半径を指定します。

public static void DrawSphere (Vector3 center, float radius);
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void OnDrawGizmosSelected()
    {
        // Draw a yellow sphere at the transform's position
        Gizmos.color = Color.yellow;
        Gizmos.DrawSphere(transform.position, 1);
    }
}

DrawWireCube、DrawWireMesh、DrawWireSphere

ワイヤーで形を表します。引数は DrawCube、DrawMesh、DrawSphere と同じです。
https://docs.unity3d.com/jp/2018.4/ScriptReference/Gizmos.html

UnityFreeCam

https://baba-s.hatenablog.com/entry/2018/01/31/230200

ScriptableObject のアイコンを変更する

using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;

[CustomEditor( typeof( Example ) )]
public class ExampleEditor : Editor
{
    public override Texture2D RenderStaticPreview
    ( 
        string assetPath, 
        Object[] subAssets, 
        int width, 
        int height 
    )
    {
        var obj = target as Example;
        var icon = obj.icon;

        if ( icon == null )
        {
            return base.RenderStaticPreview( assetPath, subAssets, width, height );
        }

        var preview = AssetPreview.GetAssetPreview( icon );
        var final = new Texture2D( width, height );

        EditorUtility.CopySerialized( preview, final );

        return final;
    }
}

#endif

[CreateAssetMenu]
public class Example : ScriptableObject
{
    public Sprite icon;
}

https://baba-s.hatenablog.com/entry/2018/01/17/133100

グリッドに合わせてオブジェクトをスナップできるGrids MX - The Definitive Snapping Solution

https://baba-s.hatenablog.com/entry/2018/04/12/093700

Scene ビューでオブジェクトをグリッド単位で移動・拡縮できるようにする

https://baba-s.hatenablog.com/entry/2018/08/31/215500

Scene ビューに手早くギズモを表示「FastGizmosUnity」

https://baba-s.hatenablog.com/entry/2019/04/05/090000

シーン

Unity Simple Grid Framework」を Unity プロジェクトに導入することでScene ビューにカスタマイズ可能なグリッドを表示できる
https://baba-s.hatenablog.com/entry/2017/12/28/084300

Scene ビューに 3D オブジェクトの法線・接線・従法線を表示できる「NormalView」
https://baba-s.hatenablog.com/entry/2021/11/12/090000

Prefab

Unity3D-PrefabEditor
https://github.com/remibodin/Unity3D-PrefabEditor
「Assets>Edit prefab」を選択する前にシーンを保存しておかないと
プレハブの編集画面から戻ってきた時にシーンの変更が失われてしまいます
https://baba-s.hatenablog.com/entry/2017/11/06/083000

インスペクター改造

https://baba-s.hatenablog.com/entry/2017/12/27/210600

https://bluebirdofoz.hatenablog.com/entry/2023/10/08/235543

https://bluebirdofoz.hatenablog.com/entry/2022/07/27/224731

https://bluebirdofoz.hatenablog.com/entry/2023/10/09/233359

https://bluebirdofoz.hatenablog.com/entry/2023/11/24/081242

ChildObjectMakerEditor.cs

Discussion