🎃
UNITY editor の拡張 開発環境
editor
RAYCAST 視覚化 Debug.DrawRay
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;
// (以下略)
}
}
スクリプトからGizmosを描画する
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 と同じです。
UnityFreeCam
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;
}
グリッドに合わせてオブジェクトをスナップできるGrids MX - The Definitive Snapping Solution
Scene ビューでオブジェクトをグリッド単位で移動・拡縮できるようにする
Scene ビューに手早くギズモを表示「FastGizmosUnity」
シーン
Unity Simple Grid Framework」を Unity プロジェクトに導入することでScene ビューにカスタマイズ可能なグリッドを表示できる
Scene ビューに 3D オブジェクトの法線・接線・従法線を表示できる「NormalView」
Prefab
Unity3D-PrefabEditor
プレハブの編集画面から戻ってきた時にシーンの変更が失われてしまいます
インスペクター改造
ChildObjectMakerEditor.cs
Discussion