UnityのHandlesクラス

45895 ワード

HandlesはSceneViewにユーザの操作に応答できる幾何学的パターンを描き,指定した変数の数値に影響を与える.
Handlesクラスのインタフェースは大きく分けて以下のようになります
1.Draw:点、線、面などのメタジオメトリを描画するDrawAAPolyLine Draw anti-aliased line specified with point array and width.DrawBezier Draw textured bezier line through start and end points with the given tangents. To get an anti-aliased effect use a texture that is 1x2 pixels with one transparent white pixel and one opaque white pixel. The bezier curve will be swept using this texture. DrawLine Draw a line from p1 to p2. DrawPolyLine Draw a line going through the list of all points. DrawSolidArc Draw a circular sector (pie piece) in 3D space. DrawSolidDisc Draw a solid flat disc in 3D space. DrawSolidRectangleWithOutline Draw a solid outlined rectangle in 3D space. DrawWireArc Draw a circular arc in 3D space. DrawWireDisc Draw the outline of a flat disc in 3D space.
2.Handle:Vector 3,Vector 2,floatなどのDoPositionHandleのようなオブジェクト移動の3方向オペレータDoRotationHandleのようなオブジェクト回転の3方向リングオペレータDoScaleHandleのようなオブジェクトスケーリングの3軸オペレータFreeMoveHandle Make an unconstrained movement handleのような可視化された操作値.FreeRotateHandle Make an unconstrained rotation handle. PositionHandle Make a 3D Scene view position handle. RadiusHandle Make a Scene view radius handle. RotationHandle Make a Scene view rotation handle. ScaleHandle Make a Scene view scale handle. ScaleValueHandle Make a single-float draggable handle.
3.Caps:四角形、点精霊、球形、円錐などの多角形ジオメトリを描画するArrowCap Draw an arrow like those used by the move tool.SphereCap Draw a Sphere. Pass this into handle functions. CircleCap Draw a camera-facing Circle. Pass this into handle functions. ConeCap Draw a Cone. Pass this into handle functions. CubeCap Draw a cube. Pass this into handle functions. CylinderCap Draw a Cylinder. Pass this into handle functions. DotCap Draw a camera-facing dot. Pass this into handle functions. Rectangle Capは矩形のSelectionFrame Draw a camera facing selection frameを描いた.
4.2 DGUI:Begingui Begin a 2 DGUI block inside the 3 Dhandle GUIの代わりにGUILayoutまたはEditorGUILayoutを使用する.EndGUI End a 2D GUI block and get back to the 3D handle GUI. Slider2D Slide a handle in a 2D plane.
5.Camera:カメラDrawCamera Draws a camera inside a rectangle.SetCameraSet the current camera so all Handles and Gizmos are draw with its settings. ClearCamera Clears the camera. SnapValue Rounds the value val to the closest multiple of snap (snap can only be posiive)
テストコード
using UnityEngine;
using System.Collections;
 
public class SceneTag : MonoBehaviour 
{
	public Vector3 vectorPoint = Vector3.zero;
	public Vector3 vectorPoint2 = Vector3.zero;
	
	public float shieldArea = 5;
	public Quaternion rot = Quaternion.identity;
}
using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
using System.Text;
 
[CustomEditor(typeof(SceneTag))]
public class SceneEditor : Editor 
{
	void OnSceneGUI()
	{
		Test_Handles();
		Test_Event();
	}
 
	private static void Test_Event()
	{
		Vector2 mousePosition = Event.current.mousePosition;
		StringBuilder sb = new StringBuilder();
		sb.AppendFormat("({0},{1})"
			, mousePosition.x
			, mousePosition.y);
		//Debug.Log(sb.ToString());
	}
 
	private void Test_Handles()
	{
		SceneTag scene = target as SceneTag;
		Test_Color(scene);
		//Test_Button(scene);
		Test_ArrowCap(scene);
		Test_CircleCap(scene);
		Test_ChangeValue(scene);
		Test_DrawAAPolyLine(scene);
		Test_DrawSolidArc(scene);
		Test_ScaleValueHandle(scene);
		Test_FreeRotateHandle(scene);
		Test_FreeMoveHandle(scene);
		Test_BeginGUI(scene);
 
		if (GUI.changed)
			EditorUtility.SetDirty(target);
	}
 
	private static void Test_FreeMoveHandle(SceneTag scene)
	{
		scene.vectorPoint = Handles.FreeMoveHandle(scene.vectorPoint,
								  Quaternion.identity,
								  2.0f,
								  Vector3.zero,
								  Handles.ArrowCap);
	}
 
	private static void Test_FreeRotateHandle(SceneTag scene)
	{
		scene.transform.rotation = Handles.FreeRotateHandle(scene.transform.rotation, scene.transform.position, 0.5f);
	}
 
	private static void Test_ScaleValueHandle(SceneTag scene)
	{
		scene.shieldArea
				  = Handles.ScaleValueHandle(scene.shieldArea,
								  scene.transform.position + scene.transform.forward * scene.shieldArea,
								  scene.transform.rotation,
								  1,
								  Handles.ConeCap,
								  1);
	}
 
	private static void Test_DrawSolidArc(SceneTag scene)
	{
		Handles.color = new Color(1, 1, 1, 0.2f);
		Handles.DrawSolidArc(scene.transform.position,
				scene.transform.up,
				-scene.transform.right,
				270,
				scene.shieldArea);
		Handles.color = Color.white;
		scene.shieldArea =
		Handles.ScaleValueHandle(scene.shieldArea,
						scene.transform.position + scene.transform.forward * scene.shieldArea,
						scene.transform.rotation,
						1,
						Handles.ConeCap,
						1);
	}
 
	private static void Test_DrawAAPolyLine(SceneTag scene)
	{
		Vector3[] positions = new Vector3[]
		{
			new Vector3(2,0,0),
			new Vector3(2,1,0),
			new Vector3(2,1,1),
			new Vector3(1,2,1),
			new Vector3(1,2,2),
		};
		for (int i = 0; i < positions.Length; ++i)
		{
			positions[i] += scene.transform.position;
		}
		Handles.DrawAAPolyLine(positions);
	}
 
	private static void Test_ChangeValue(SceneTag scene)
	{
		scene.rot =
					 Handles.Disc(scene.rot,
							 scene.transform.position,
							 new Vector3(1, 1, 0),
							 5,
							 false,
							 1);
		scene.vectorPoint = Handles.DoPositionHandle(scene.vectorPoint, Quaternion.identity);
		scene.vectorPoint2 = Handles.DoPositionHandle(scene.vectorPoint2, Quaternion.identity);
	}
 
	private static void Test_CircleCap(SceneTag scene)
	{
		float circleSize = 1;
		Handles.color = Color.red;
		Handles.CircleCap(0,
				scene.transform.position + new Vector3(5, 0, 0),
				scene.transform.rotation,
				circleSize);
		Handles.color = Color.green;
		Handles.CircleCap(0,
				scene.transform.position + new Vector3(0, 5, 0),
				scene.transform.rotation,
				circleSize);
		Handles.color = Color.blue;
		Handles.CircleCap(0,
				scene.transform.position + new Vector3(0, 0, 5),
				scene.transform.rotation,
				circleSize);
 
		Handles.color = Color.red;
		Vector3 newpos = scene.transform.position + new Vector3(5, 0, 0);
		circleSize = HandleUtility.GetHandleSize(newpos);
		Handles.CircleCap(0,
				newpos,
				scene.transform.rotation,
				circleSize);
		Handles.color = Color.green;
		newpos = scene.transform.position + new Vector3(0, 5, 0);
		circleSize = HandleUtility.GetHandleSize(newpos);
		Handles.CircleCap(0,
				newpos,
				scene.transform.rotation,
				circleSize);
		Handles.color = Color.blue;
		newpos = scene.transform.position + new Vector3(0, 0, 5);
		circleSize = HandleUtility.GetHandleSize(newpos);
		Handles.CircleCap(0,
				newpos,
				scene.transform.rotation,
				circleSize);
 
	}
 
	// 3D                  。;
	//                ,  ?;
	private static void Test_Button(SceneTag scene)
	{
		Handles.Button(scene.transform.position + new Vector3(0, 2, 0),
							  Quaternion.identity,
							  3,
							  3,
							  Handles.RectangleCap);
	}
 
	private static void Test_BeginGUI(SceneTag scene)
	{
		Handles.color = Color.blue;
		Handles.Label(scene.transform.position + Vector3.up * 2,
				scene.transform.position.ToString() + "
ShieldArea: "
+ scene.shieldArea.ToString()); Handles.color = Color.red; Handles.DrawWireArc(scene.transform.position, scene.transform.up, -scene.transform.right, 180, scene.shieldArea); scene.shieldArea = Handles.ScaleValueHandle(scene.shieldArea, scene.transform.position + scene.transform.forward * scene.shieldArea, scene.transform.rotation, 1, Handles.ConeCap, 1); // GUI Handles , ; // Handles.BeginGUI , ; GUILayout.BeginArea(new Rect(Screen.width - 100, Screen.height - 80, 90, 50)); //Handles.BeginGUI(new Rect(Screen.width - 100, Screen.height - 80, 90, 50)); try { float a = float.Parse(GUILayout.TextField(scene.shieldArea.ToString())); scene.shieldArea = a; } catch (System.Exception ex) { } if (GUILayout.Button("Reset Area")) scene.shieldArea = 5; //Handles.EndGUI(); GUILayout.EndArea(); } private static void Test_ArrowCap(SceneTag scene) { float arrowSize = 1; Handles.color = Color.red; Handles.ArrowCap(0, scene.transform.position + new Vector3(5, 0, 0), scene.transform.rotation, arrowSize); Handles.color = Color.green; Handles.ArrowCap(0, scene.transform.position + new Vector3(0, 5, 0), scene.transform.rotation, arrowSize); Handles.color = Color.blue; Handles.ArrowCap(0, scene.transform.position + new Vector3(0, 0, 5), scene.transform.rotation, arrowSize); } private void Test_Color(SceneTag scene) { Handles.color = Color.magenta; scene.vectorPoint = Handles.Slider(scene.vectorPoint, Vector3.zero - scene.transform.position); } }