2021. 6. 15. 13:12
Unity/C#
구를그림
void OnDrawGizmos()
{
Gizmos.DrawWireSphere(transform.position, radius);
}
가려지지 않는 구를 그림
void OnDrawGizmos()
{
#if UNITY_EDITOR
UnityEditor.Handles.DrawWireDisc(transform.position, transform.up, radius);
UnityEditor.Handles.DrawWireDisc(transform.position, transform.right, radius);
UnityEditor.Handles.DrawWireDisc(transform.position, transform.forward, radius);
#endif
}
원을 그림
void OnDrawGizmos()
{
UnityEditor.Handles.DrawWireDisc(transforms[i].position, fowordAngle * Vector3.up, radius);
}
텍스트 표시
#if UNITY_EDITOR
void OnDrawGizmos()
{
UnityEditor.Handles.Label(transform.position, "text");
}
#endif
폰트설정한 텍스트 표시
var guiStyle = new GUIStyle();
guiStyle.alignment = TextAnchor.MiddleCenter; //왜인지 UpperLeft로만 된다
guiStyle.fontSize = 48;
guiStyle.normal.textColor = color;
UnityEditor.Handles.Label(transform.position, "text", guiStyle);
선그리기 커스텀
void DrawLine(params Transform[] transforms)
{
transforms = System.Array.FindAll(transforms, x => x != null);
if ((transforms == null) || (transforms.Length < 2))
{
return;
}
for (int i = 0; i < transforms.Length-1; i++)
{
Gizmos.DrawLine(transforms[i].position, transforms[i+1].position);
}
}
회전가능한 Cube
static void DrawWireCubeOverlay(Vector3 center, Vector3 size, Quaternion rotation)
{
#if UNITY_EDITOR
{
var size1 = size;
var size2 = size1;
for (int x = -1; x <= 1; x += 2)
{
for (int y = -1; y <= 1; y += 2)
{
size1.x = size.x * x;
size1.y = size.y * y;
size2 = size1;
size1.z = size.z;
size2.z = -size1.z;
DrawLine(size1, size2);
}
}
for (int y = -1; y <= 1; y += 2)
{
for (int z = -1; z <= 1; z += 2)
{
size1.y = size.y * y;
size1.z = size.z * z;
size2 = size1;
size1.x = size.x;
size2.x = -size1.x;
DrawLine(size1, size2);
}
}
for (int x = -1; x <= 1; x += 2)
{
for (int z = -1; z <= 1; z += 2)
{
size1.x = size.x * x;
size1.z = size.z * z;
size2 = size1;
size1.y = size.y;
size2.y = -size1.y;
DrawLine(size1, size2);
}
}
}
void DrawLine(Vector3 size1, Vector3 size2)
{
UnityEditor.Handles.DrawLine(center + rotation * (size1 / 2), center + rotation*(size2 / 2));
}
#endif
}
캡슐을 그림
static void DrawCapsule(float radius, params Transform[] transforms)
{
transforms = System.Array.FindAll(transforms, x => x != null);
if ((transforms == null) || (transforms.Length < 2))
{
return;
}
for (int i = 1; i < transforms.Length; i++)
{
DrawCapsule(transforms[i - 1].position, transforms[i].position, radius);
}
}
static void DrawCapsule(Vector3 start, Vector3 end, float radius)
{
var fowordAngle = Quaternion.LookRotation(start - end, Vector3.up);
start += (end - start).normalized * radius;
end -= (end - start).normalized * radius;
#if UNITY_EDITOR
UnityEditor.Handles.DrawWireDisc(start, fowordAngle * Vector3.forward, radius);
UnityEditor.Handles.DrawWireArc(start, fowordAngle * Vector3.up, fowordAngle * -Vector3.right, 180, radius);
UnityEditor.Handles.DrawWireArc(start, fowordAngle * Vector3.right, fowordAngle * Vector3.up, 180, radius);
UnityEditor.Handles.DrawWireDisc(end, fowordAngle * Vector3.forward, radius);
UnityEditor.Handles.DrawWireArc(end, fowordAngle * Vector3.up, fowordAngle * Vector3.right, 180, radius);
UnityEditor.Handles.DrawWireArc(end, fowordAngle * Vector3.right, fowordAngle * -Vector3.up, 180, radius);
#endif
Vector3 upVector = fowordAngle * Vector3.up * radius;
Vector3 rightVector = fowordAngle * Vector3.right * radius;
#if UNITY_EDITOR
UnityEditor.Handles.DrawLine(start + upVector, end + upVector);
UnityEditor.Handles.DrawLine(start + rightVector, end + rightVector);
UnityEditor.Handles.DrawLine(start - upVector, end - upVector);
UnityEditor.Handles.DrawLine(start - rightVector, end - rightVector);
#endif
}
실린더를 그림
void DrawCylinder(float radius, params Transform[] transforms)
{
transforms = System.Array.FindAll(transforms, x => x != null);
if ((transforms == null) || (transforms.Length < 2))
{
return;
}
for (int i = 1; i < transforms.Length; i++)
{
DrawCylinder(transforms[i - 1].position, transforms[i].position, radius);
}
}
void DrawCylinder(Vector3 start, Vector3 end, float radius)
{
var fowordAngle = Quaternion.LookRotation(start - end, Vector3.up);
#if UNITY_EDITOR
UnityEditor.Handles.DrawWireDisc(start, fowordAngle * Vector3.forward, radius);
UnityEditor.Handles.DrawWireDisc(end, fowordAngle * Vector3.forward, radius);
#endif
Vector3 upVector = fowordAngle * Vector3.up * radius;
Vector3 rightVector = fowordAngle * Vector3.right * radius;
#if UNITY_EDITOR
UnityEditor.Handles.DrawLine(start + upVector, end + upVector);
UnityEditor.Handles.DrawLine(start + rightVector, end + rightVector);
UnityEditor.Handles.DrawLine(start - upVector, end - upVector);
UnityEditor.Handles.DrawLine(start - rightVector, end - rightVector);
#endif
}
콜라이더를 그림
void DrawColliderGizmo(Transform transform)
{
if (transform != null)
{
DrawColliderGizmo(transform.gameObject);
}
}
void DrawColliderGizmo(GameObject gameObject)
{
if (gameObject != null)
{
var sphereCollider = gameObject.GetComponent<SphereCollider>();
if (sphereCollider != null)
{
Gizmos.DrawWireSphere(transform.position, sphereCollider.radius);
}
}
}
컬러적용
Gizmos.color = Color.red;
{
Gizmos.DrawLine(cameraPosition.position, thirdPersonPoint.position);
}
Gizmos.color = Color.white;
선택된 상태에서만 기즈모를 그림
void OnDrawGizmosSelected()
{
//기즈모를 그림
}
혹은
#if UNITY_EDITOR
if (Selection.transforms.Contains(transform))
{
//기즈모를 그림
}
#endif
화살표 표시 (arrow)
#if UNITY_EDITOR
Handles.color = Handles.xAxisColor;
UnityEditor.Handles.ArrowHandleCap(0, transform.position, transform.rotation * Quaternion.Euler(0, 90, 0), size: 0.25f, EventType.Repaint);
Handles.color = Handles.yAxisColor;
UnityEditor.Handles.ArrowHandleCap(0, transform.position, transform.rotation * Quaternion.Euler(-90, 0, 0), size: 0.25f, EventType.Repaint);
Handles.color = Handles.zAxisColor;
UnityEditor.Handles.ArrowHandleCap(0, transform.position, transform.rotation, size: 0.25f, EventType.Repaint);
Handles.color = Color.white;
#endif
EditorWindow에서 그릴때
Debug 계열만 됨
void OnGUI()
{
if (armTransform != null)
{
Debug.DrawLine(armTransform.position, armTransform.position+Vector3.forward,Color.red);
}
}
void Update()
{
Repaint();
}
'Unity > C#' 카테고리의 다른 글
유니티 커스텀 인터페이스 (0) | 2021.06.24 |
---|---|
유니티 안드로이드 빌드관련 스크립트 (0) | 2021.06.10 |
유니티 오디오 관련 코드 (0) | 2021.06.01 |