用于在场景视图中编辑角度和半径的复合手柄类。
场景视图中的 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 | 要在显示角度控制手柄时使用的 CapFunction。 |
angleHandleSizeFunction | 用于指定角度控制手柄应该多大的 SizeFunction。 |
fillColor | 返回或指定圆弧形状的颜色。 |
radius | 返回或指定手柄圆弧的半径。 |
radiusHandleColor | 返回或指定半径控制手柄的颜色。 |
radiusHandleDrawFunction | 要在显示半径控制手柄时使用的 CapFunction。 |
radiusHandleSizeFunction | 用于指定角度控制手柄应该多大的 SizeFunction。 |
wireframeColor | 返回或指定沿圆弧外侧曲线的颜色。 |
ArcHandle | 创建 ArcHandle 类的新实例。 |
DrawHandle | 使用实例的当前配置在当前手柄摄像机中显示此实例的函数。 |
SetColorWithoutRadiusHandle | 将 angleHandleColor、wireframeColor 和 fillColor 设置为相同的值,其中 fillColor 将具有指定的 Alpha 值。radiusHandleColor 将被设置为 Color.clear,且半径手柄将被禁用。 |
SetColorWithRadiusHandle | 将 angleHandleColor、radiusHandleColor、wireframeColor 和 fillColor 设置为相同的值,其中 fillColor 将具有指定的 Alpha 值。 |
DefaultAngleHandleDrawFunction | 可绘制一条以 Handles.CylinderHandleCap 终止的线的 CapFunction。 |
DefaultAngleHandleSizeFunction | 返回一个固定屏幕空间尺寸的 SizeFunction。 |
DefaultRadiusHandleSizeFunction | 返回一个固定屏幕空间尺寸的 SizeFunction。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.