http://wiki.unity3d.com/index.php/Tip_of_the_day
Dec 2012
DrawGizmo 属性
DrawGizmo 属性により MonoBehavior クラス以外での Gizmo 描画ロジックをセットアップ出来る。次のコードにより Editor クラス内でのメソッドでの DrawGizmo 属性使用例を紹介する。
/// <summary>
/// Provides a editor for the <see cref="TileMap"/> component
/// </summary>
[CustomEditor(typeof(TileMap))]
public class TileMapEditor : Editor
{
/// The RenderMapGizmo method will be called if the map is selected.
[DrawGizmo(GizmoType.Selected | GizmoType.Active)]
static void RenderMapGizmo(TileMap map, GizmoType gizmoType)
{
// store map width, height and position
var mapWidth = map.Columns * map.CellWidth;
var mapHeight = map.Rows * map.CellHeight;
var position = map.transform.position;
var activelayerHeight = map.ActiveLayer * map.Depth;
if (map.drawGridLines)
{
// draw layer border
Gizmos.color = Color.white;
Gizmos.DrawLine(
position + new Vector3(0, activelayerHeight, 0), position + new Vector3(mapWidth, activelayerHeight, 0));
Gizmos.DrawLine(
position + new Vector3(0, activelayerHeight, 0), position + new Vector3(0, activelayerHeight, mapHeight));
Gizmos.DrawLine(
position + new Vector3(mapWidth, activelayerHeight, 0),
position + new Vector3(mapWidth, activelayerHeight, mapHeight));
Gizmos.DrawLine(
position + new Vector3(0, activelayerHeight, mapHeight),
position + new Vector3(mapWidth, activelayerHeight, mapHeight));
// more draw logic here
}
}
別の方法としては OnDrawGizmosSelected と呼ばれるメソッドを自身の MonoBehavior コードの中に用意することだ。
/// <summary>
/// Provides a component for tile mapping.
/// </summary>
public class TileMap : MonoBehaviour
{
/// <summary>
/// When the game object is selected this will draw the gizmos
/// </summary>
/// <remarks>Only called when in the Unity editor.</remarks>
private void OnDrawGizmosSelected()
{
// gizmo draw code goes here
}
}
しかし Gizmo 描画ロジックの典型的な使用例は Unity Editor の中で使用する場合であるため、 DrawGizmo 属性により描画ロジックをより適切な場所に配置できる。
---------
Unity のエディタスクリプトの知識を積み重ねようぜ!

0 件のコメント:
コメントを投稿