Nobinator
8/11/2017 - 3:19 PM

Custom Handles Unity

Custom Handles Unity

using UnityEditor;
using UnityEngine;

public class MyCap{

	static Texture2D _mainTexture;

	private static GUIContent _mainHandle;
	
	private static GUIContent mainHandle{
		get{ return _mainHandle ?? (_mainHandle = new GUIContent(Resources.Load<Texture2D>("GreenHandle"))); }
	}

	private static GUIStyle _handleStyle;

	private static GUIStyle handleStyle{
		get{
			if(_handleStyle==null)
				_handleStyle = new GUIStyle();
			_handleStyle.alignment = TextAnchor.MiddleCenter;
			_handleStyle.fixedWidth = 32;
			_handleStyle.fixedHeight = 32;
			return _handleStyle;
		}
	}

	public static void TextureHandleCap(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType){
		if (eventType != EventType.Layout){
			if (eventType == EventType.Repaint){
				Handles.Label(position, mainHandle, handleStyle);
			}
		}
		else{
			HandleUtility.AddControl(controlID, HandleUtility.DistanceToCircle(position,size*0.25f));
		}
	}

		
}






	
	/*
	public static void ArrowHandleCap(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
	{
		if (eventType != EventType.Layout)
		{
			if (eventType == EventType.Repaint)
			{
				Vector3 vector = rotation * Vector3.forward;
				Handles.ConeHandleCap(controlID, position + vector * size, Quaternion.LookRotation(vector), size * 0.2f, eventType);
				Handles.DrawLine(position, position + vector * size * 0.9f);
			}
		}
		else
		{
			Vector3 a = rotation * Vector3.forward;
			HandleUtility.AddControl(controlID, HandleUtility.DistanceToLine(position, position + a * size * 0.9f));
			HandleUtility.AddControl(controlID, HandleUtility.DistanceToCircle(position + a * size, size * 0.2f));
		}
	}*/
	
	/*
	
	Texture nodeTexture;
    static readonly GUIStyle handleStyle = new GUIStyle();
    
    void OnEnable(){
        nodeTexture = Resources.Load<Texture>("Handle");
        if (nodeTexture == null) nodeTexture = EditorGUIUtility.whiteTexture;
        handleStyle.alignment = TextAnchor.MiddleCenter;
        handleStyle.fixedWidth = 15;
        handleStyle.fixedHeight = 15;
    }
	
	void HandleFunc(int controlID, Vector3 position, Quaternion rotation, float size){
        // GUIUtility.hotControl означает, что мышь нажата на Cap
        if (controlID == GUIUtility.hotControl)
            GUI.color = Color.red;
        else
            GUI.color = Color.green;
        
        Handles.Label(position, new GUIContent(nodeTexture), handleStyle);
        GUI.color = Color.white;
    }
	*/
	
	/*
	internal static void RectangleHandleCap(int controlID, Vector3 position, Quaternion rotation, Vector2 size, EventType eventType)
	{
		if (eventType != EventType.Layout)
		{
			if (eventType == EventType.Repaint)
			{
				Vector3 b = rotation * new Vector3(size.x, 0f, 0f);
				Vector3 b2 = rotation * new Vector3(0f, size.y, 0f);
				Handles.s_RectangleHandlePointsCache[0] = position + b + b2;
				Handles.s_RectangleHandlePointsCache[1] = position + b - b2;
				Handles.s_RectangleHandlePointsCache[2] = position - b - b2;
				Handles.s_RectangleHandlePointsCache[3] = position - b + b2;
				Handles.s_RectangleHandlePointsCache[4] = position + b + b2;
				Handles.DrawPolyLine(Handles.s_RectangleHandlePointsCache);
			}
		}
		else
		{
			HandleUtility.AddControl(controlID, HandleUtility.DistanceToRectangleInternal(position, rotation, size));
		}
	}
	*/