Unity Editor Extensions – Handle 和Gizmos私人定制

孙广东  2015.6.20  

先贴一个 Grid网格吧。  可以标记一个对象的正方形范围等

拖拽到指定的对象就OK了。

using UnityEngine;
using System.Collections;
 
// DrawGizmoGrid.cs
// draws a useful reference grid in the editor in Unity. 
// 09/01/15 - Hayden Scott-Baron
// twitter.com/docky 
// no attribution needed, but please tell me if you like it ^_^
 
public class DrawGizmoGrid : MonoBehaviour
{
	// universal grid scale
	public float gridScale = 1f; 
 
	// extents of the grid
	public int minX = -15; 
	public int minY = -15; 
	public int maxX = 15; 
	public int maxY = 15; 
 
	// nudges the whole grid rel
	public Vector3 gridOffset = Vector3.zero; 
 
	// is this an XY or an XZ grid?
	public bool topDownGrid = true; 
 
	// choose a colour for the gizmos
	public int gizmoMajorLines = 5; 
	public Color gizmoLineColor = new Color (0.4f, 0.4f, 0.3f, 1f);  
 
	// rename + centre the gameobject upon first time dragging the script into the editor. 
	void Reset ()
	{
		if (name == "GameObject")
			name = "~~ GIZMO GRID ~~"; 
 
		transform.position = Vector3.zero; 
	}
 
	// draw the grid :) 
	void OnDrawGizmos ()
	{
		// orient to the gameobject, so you can rotate the grid independently if desired
		Gizmos.matrix = transform.localToWorldMatrix;
 
		// set colours
		Color dimColor = new Color(gizmoLineColor.r, gizmoLineColor.g, gizmoLineColor.b, 0.25f* gizmoLineColor.a); 
		Color brightColor = Color.Lerp (Color.white, gizmoLineColor, 0.75f); 
 
		// draw the horizontal lines
		for (int x = minX; x < maxX+1; x++)
		{
			// find major lines
			Gizmos.color = (x % gizmoMajorLines == 0 ? gizmoLineColor : dimColor); 
			if (x == 0)
				Gizmos.color = brightColor;
 
			Vector3 pos1 = new Vector3(x, minY, 0) * gridScale;  
			Vector3 pos2 = new Vector3(x, maxY, 0) * gridScale;  
 
			// convert to topdown/overhead units if necessary
			if (topDownGrid)
			{
				pos1 = new Vector3(pos1.x, 0, pos1.y); 
				pos2 = new Vector3(pos2.x, 0, pos2.y); 
			}
 
			Gizmos.DrawLine ((gridOffset + pos1), (gridOffset + pos2)); 
		}
 
		// draw the vertical lines
		for (int y = minY; y < maxY+1; y++)
		{
			// find major lines
			Gizmos.color = (y % gizmoMajorLines == 0 ? gizmoLineColor : dimColor); 
			if (y == 0)
				Gizmos.color = brightColor;
 
			Vector3 pos1 = new Vector3(minX, y, 0) * gridScale;  
			Vector3 pos2 = new Vector3(maxX, y, 0) * gridScale;  
 
			// convert to topdown/overhead units if necessary
			if (topDownGrid)
			{
				pos1 = new Vector3(pos1.x, 0, pos1.y); 
				pos2 = new Vector3(pos2.x, 0, pos2.y); 
			}
 
			Gizmos.DrawLine ((gridOffset + pos1), (gridOffset + pos2)); 
		}
	}
}


3Layingthe groundwork for our handles为我们处理奠定基础

4Creatinga handle

5Drawinglines in the scene view

6Coloringhandles

7Dynamicallysizing handles

 

public class TerrainPiece : MonoBehaviour 
{

                public SpriteRenderer spriteRenderer;
                public Vector3 mountPoint;
                void Awake () 
                {
                                if (spriteRenderer == null)

                                                spriteRenderer = GetComponentInChildren<SpriteRenderer>();
                }
}

 

下面就来定制这个类:

[CustomEditor(typeof(TerrainPiece))]

public class TerrainPieceEditor : Editor

{

                SerializedProperty mountPointProp;              // TerrainPiece. mountPoint

                TerrainPiece terrainPiece;                                                                                               // TerrainPiece

   

                void OnEnable()

                {

                                terrainPiece = (TerrainPiece) target;

                                mountPointProp = serializedObject.FindProperty("mountPoint");

                }
 
                void OnSceneGUI()

                {

                                serializedObject.Update();

 

                                Vector3 worldMountPt = terrainPiece.transform.TransformPoint(mountPointProp.vector3Value);     // 转成世界坐标系

 

                                float sizeFactor = HandleUtility.GetHandleSize(worldMountPt) * 0.25f;            // 这样就不会随着scene面板的远近而动态改变大小,一直不变。

 

                                Handles.color = Color.magenta;                                     // 设置颜色

                                worldMountPt = Handles.FreeMoveHandle(worldMountPt, Quaternion.identity, sizeFactor * 0.2f, Vector3.zero, Handles.RectangleCap);     // 拖动handle来改变值

 

                                Handles.DrawLine(worldMountPt - Vector3.up * sizeFactor, worldMountPt + Vector3.up * sizeFactor);

                                Handles.DrawLine(worldMountPt - Vector3.right * sizeFactor, worldMountPt + Vector3.right * sizeFactor);

 

                                Vector3 mountPointLocal = terrainPiece.transform.InverseTransformPoint(worldMountPt);     // 转成相对父级的本地坐标

                                mountPointLocal.z = 0;

                                mountPointProp.vector3Value = mountPointLocal;

                                 

                                serializedObject.ApplyModifiedProperties();     

                }

}


 

 

8Previewingthe status bar

9Sizinga status bar with handles

10

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值