用于在场景视图中编辑角度和半径的复合手柄类。
场景视图中的 ArcHandle。\ \ 该类允许您显示用于编辑圆弧角度和半径的控制手柄。圆弧源自 Vector3.forward,乘以 radius 并围绕 Vector3.up 旋转。由该类的 DrawHandle 方法渲染的手柄受 Handles 类(例如 Handles.matrix 和 Handles.color)中的全局状态影响。\ \ 以下组件通过角度和力属性定义对象。
using UnityEngine;
public class ProjectileExample : MonoBehaviour { public float elevationAngle { get { return m_ElevationAngle; } set { m_ElevationAngle = value; } } [SerializeField] float m_ElevationAngle = 45f;
public float impulse { get { return m_Impulse; } set { m_Impulse = value; } } [SerializeField] float m_Impulse = 20f;
public Vector3 facingDirection { get { Vector3 result = transform.forward; result.y = 0f; return result.sqrMagnitude == 0f ? Vector3.forward : result.normalized; } }
protected virtual void Start() { GameObject ball = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Vector3 direction = facingDirection; direction = Quaternion.AngleAxis(elevationAngle, Vector3.Cross(direction, Vector3.up)) * direction; ball.AddComponent<Rigidbody>().AddForce(direction * impulse, ForceMode.Impulse); } }
以下自定义编辑器示例允许您在场景视图中编辑此组件的仰角和力属性,其中力由手柄的半径表示。
using UnityEditor; using UnityEditor.IMGUI.Controls; using UnityEngine;
[CustomEditor(typeof(ProjectileExample))] public class ProjectileExampleEditor : Editor { ArcHandle m_ArcHandle = new ArcHandle();
protected virtual void OnEnable() { // arc handle has no radius handle by default m_ArcHandle.SetColorWithRadiusHandle(Color.white, 0.1f); }
// the OnSceneGUI callback uses the scene view camera for drawing handles by default protected virtual void OnSceneGUI() { ProjectileExample projectileExample = (ProjectileExample)target;
// copy the target object's data to the handle m_ArcHandle.angle = projectileExample.elevationAngle; m_ArcHandle.radius = projectileExample.impulse;
// set the handle matrix so that angle extends upward from target's facing direction along ground Vector3 handleDirection = projectileExample.facingDirection; Vector3 handleNormal = Vector3.Cross(handleDirection, Vector3.up); Matrix4x4 handleMatrix = Matrix4x4.TRS( projectileExample.transform.position, Quaternion.LookRotation(handleDirection, handleNormal), Vector3.one );
using (new Handles.DrawingScope(handleMatrix)) { // draw the handle EditorGUI.BeginChangeCheck(); m_ArcHandle.DrawHandle(); if (EditorGUI.EndChangeCheck()) { // record the target object before setting new values so changes can be undone/redone Undo.RecordObject(projectileExample, "Change Projectile Properties");
// copy the handle's updated data back to the target object projectileExample.elevationAngle = m_ArcHandle.angle; projectileExample.impulse = m_ArcHandle.radius; } } } }
angle | 返回或指定手柄圆弧的角度。 |
angleHandleColor | 返回或指定角度控制手柄的颜色。 |
angleHandleDrawFunction | An optional CapFunction to use when displaying the angle control handle. Defaults to a line terminated with Handles.CylinderHandleCap if no value is specified. |
angleHandleSizeFunction | An optional SizeFunction to specify how large the angle control handle should be. Defaults to a fixed screen-space size. |
fillColor | 返回或指定圆弧形状的颜色。 |
radius | 返回或指定手柄圆弧的半径。 |
radiusHandleColor | 返回或指定半径控制手柄的颜色。 |
radiusHandleDrawFunction | An optional CapFunction to use when displaying the radius control handle. Defaults to a Handles.DotHandleHandleCap along the arc if no value is specified. |
radiusHandleSizeFunction | An optional SizeFunction to specify how large the radius control handle should be. Defaults to a fixed screen-space size. |
wireframeColor | 返回或指定沿圆弧外侧曲线的颜色。 |
ArcHandle | 创建 ArcHandle 类的新实例。 |
DrawHandle | 使用实例的当前配置在当前手柄摄像机中显示此实例的函数。 |
SetColorWithoutRadiusHandle | 将 angleHandleColor、wireframeColor 和 fillColor 设置为相同的值,其中 fillColor 将具有指定的 Alpha 值。radiusHandleColor 将被设置为 Color.clear,且半径手柄将被禁用。 |
SetColorWithRadiusHandle | 将 angleHandleColor、radiusHandleColor、wireframeColor 和 fillColor 设置为相同的值,其中 fillColor 将具有指定的 Alpha 值。 |