unity编辑器中使用Gizmos绘制辅助线

前面给大家分享了一个关卡编辑器,但是我们的策划同学反映在摆道具的时候不够直观,坐标总是对不齐,于是就使用Gizmos给策划同学们写了一个辅助线的脚本,分享出来:

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

[ExecuteInEditMode]
public class MapEditor : MonoBehaviour {

    public Color sideColor = Color.white;
    public Color insideColor = Color.black;

	public float width = 10.0f;
	public float height = 1.0f;

	public int mapHeight = 10;
	public int mapWidth = 100;

	//public float autoDPos = 0.1f;

	// Use this for initialization
	void Start () {
	
	}
	
	void OnDrawGizmos()
	{
        DrawBGLine();
	}

    public void DrawBGLine(){
#if UNITY_EDITOR
        Gizmos.color = insideColor;
		
        for (float y = 0; y < mapHeight; y += height)
		{
			Gizmos.DrawLine(new Vector3(0, y , -1f),
			                new Vector3(mapWidth, y , -1f));
			
            Handles.Label(new Vector3(-1f,y+0.5f , 0f), "" + y);
		}
		
		for (float x = 0; x < mapWidth; x += width)
		{
            Gizmos.DrawLine(new Vector3(x,0, -1f),
                            new Vector3(x,mapHeight, -1f));
			
            Handles.Label(new Vector3(x,-0.2f, 0f), "" + x);
        }

        Gizmos.color = sideColor;
        
        Gizmos.DrawLine(new Vector3(0, 0 , -1f),new Vector3(mapWidth, 0 , -1f));
        Gizmos.DrawLine(new Vector3(mapWidth, 0 , -1f),new Vector3(mapWidth, mapHeight, -1f));
        Gizmos.DrawLine(new Vector3(mapWidth, mapHeight , -1f),new Vector3(0, mapHeight , -1f));
        Gizmos.DrawLine(new Vector3(0, mapHeight  , -1f),new Vector3(0, 0 , -1f));

#endif
	}

}

定义了几个可配置的参数:

unity编辑器中使用Gizmos绘制辅助线_第1张图片

其中widht和height是每一个小格的尺寸,MapHeight和MapWidht是绘制网格的总体尺寸。

最终效果:

unity编辑器中使用Gizmos绘制辅助线_第2张图片

你可能感兴趣的:(unity,编辑器)