csharp 自定义Unity句柄 - `HandlesExtensions.DrawSolidHollowDiscArc(...)` - 就像`Handles.DrawSolidDisc`但具有内半

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 自定义Unity句柄 - `HandlesExtensions.DrawSolidHollowDiscArc(...)` - 就像`Handles.DrawSolidDisc`但具有内半相关的知识,希望对你有一定的参考价值。

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Reflection;

using self = HandlesExtensions;

public class HandlesExtensions {
	
	static PropertyInfo handlesHandleWireMaterial_PropertyInfo;
	static MethodInfo handleUtilityApplyWireMaterial_MethodInfo;

	static HandlesExtensions() {
		BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
		self.handleUtilityApplyWireMaterial_MethodInfo = typeof(HandleUtility).GetMethod("ApplyWireMaterial", bindingFlags);

		self.handlesHandleWireMaterial_PropertyInfo = typeof(HandleUtility).GetProperty("handleWireMaterial", bindingFlags);
	}

	static void DrawSolidHollowDiscArc(
		Vector3 position,
		Vector3 normal,
		Vector3 alongPlane,
		float angle,
		float innerRadius,
		float outerRadius
	) {
		if (Event.current.type != EventType.Repaint) {
			return;
		}

		int numSamples = 60;
		
		Shader.SetGlobalColor("_HandleColor", Handles.color * new Color(1f, 1f, 1f, 0.5f));
		Shader.SetGlobalFloat("_HandleSize", 1f);
		if(self.handleUtilityApplyWireMaterial_MethodInfo != null) {
			self.handleUtilityApplyWireMaterial_MethodInfo.Invoke(null, null);
		}
		else if(handlesHandleWireMaterial_PropertyInfo != null) {
			((Material)handlesHandleWireMaterial_PropertyInfo.GetValue(null, null)).SetPass(0);
		}


		GL.PushMatrix();
		GL.MultMatrix(Handles.matrix);
		
		GL.Begin(GL.TRIANGLES);

		alongPlane.Normalize();
		Vector3 outerArcEdge = alongPlane * outerRadius;
		Vector3 innerArcEdge = alongPlane * innerRadius;
		for (int i = 0; i <= numSamples; i++) {
			Quaternion rotation = Quaternion.AngleAxis((((float)i)/numSamples)*angle, normal);
			Vector3 outerVertice = position + rotation * outerArcEdge;
			Vector3 innerVertice = position + rotation * innerArcEdge;
			
			Quaternion rotationNext = Quaternion.AngleAxis((Mathf.Clamp((float)i+1, 0f, numSamples)/numSamples)*angle, normal);
			Vector3 outerVerticeNext = position + rotationNext * outerArcEdge;
			Vector3 innerVerticeNext = position + rotationNext * innerArcEdge;

			GL.Color(Handles.color);
			// 1____2
			//  |  /
			//  | /
			// 3|/
			GL.Vertex(outerVertice);
			GL.Vertex(outerVerticeNext);
			GL.Vertex(innerVertice);

			//    /|2
			//   / |
			//  /  |
			// 1‾‾‾‾3
			GL.Vertex(innerVertice);
			GL.Vertex(outerVerticeNext);
			GL.Vertex(innerVerticeNext);
		}

		GL.End();
		GL.PopMatrix();


	}
}

以上是关于csharp 自定义Unity句柄 - `HandlesExtensions.DrawSolidHollowDiscArc(...)` - 就像`Handles.DrawSolidDisc`但具有内半的主要内容,如果未能解决你的问题,请参考以下文章

csharp 每个实例的ScriptableObject实例的自定义图标;来自https://www.reddit.com/r/Unity3D/comments/3yq4we/custom_icon_

自定义Mono,实现Unity Android平台代码更新

视图 1:按自定义表/字段过滤(或使用 Argument Hand.Code)

Unity中C#如何实现物体在场景中随机移动

使用 css 围绕 div 自定义覆盖 - jQuery 可调整大小的自定义句柄

如何在 Flex 中获取自定义组件的句柄?