장면 뷰에서 RayCastPadding 시각화
13346 단어 Unity
여기 기사가 편리한 기능을 소개했어요.
Sceneview가 얼마나 확대됐는지 기즈모를 써봤어요.
디스플레이와 일치하는지 확인
RaycastPaddingDrawer.cs
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
public static class RaycastPaddingDrawer
{
[DrawGizmo(GizmoType.Selected, typeof(Image))]
private static void DrawPadding(Image image, GizmoType gizmoType)
{
var pivot = image.rectTransform.pivot;
image.rectTransform.SetPivotWithKeepingPosition(new Vector2(0.5f, 0.5f));
image.rectTransform.ForceUpdateRectTransforms();
var center = (Vector2)image.rectTransform.position;
image.rectTransform.SetPivotWithKeepingPosition(new Vector2(0f, 0f));
image.rectTransform.ForceUpdateRectTransforms();
var leftBottom = (Vector2)image.rectTransform.position;
image.rectTransform.SetPivotWithKeepingPosition(new Vector2(1f, 1f));
image.rectTransform.ForceUpdateRectTransforms();
var rightTop = (Vector2)image.rectTransform.position;
//CanvasのRenderModeに対応
var width = Mathf.Abs(leftBottom.x - rightTop.x);
var ratio = image.rectTransform.rect.width / width;
image.rectTransform.SetPivotWithKeepingPosition(pivot);
image.rectTransform.ForceUpdateRectTransforms();
//マイナスにすると広がるので値を反転させる
var padding = -image.raycastPadding;
var left = center.x - image.rectTransform.rect.width / ratio / 2f - padding.x / ratio;
var right = center.x + image.rectTransform.rect.width / ratio / 2f + padding.z / ratio;
var top = center.y + image.rectTransform.rect.height / ratio / 2f + padding.w / ratio;
var bottom = center.y - image.rectTransform.rect.height / ratio / 2f - padding.y / ratio;
Gizmos.color = Color.green;
Gizmos.DrawLine(new Vector3(left, top), new Vector3(right, top));
Gizmos.DrawLine(new Vector3(right, top), new Vector3(right, bottom));
Gizmos.DrawLine(new Vector3(right, bottom), new Vector3(left, bottom));
Gizmos.DrawLine(new Vector3(left, bottom), new Vector3(left, top));
}
// MIT License
// auther : Kohki Nakaji@nkjzm
// Link : https://qiita.com/nkjzm/items/297fb6921d5caca3eca9
public static void SetPivotWithKeepingPosition(this RectTransform rectTransform, Vector2 targetPivot)
{
var diffPivot = targetPivot - rectTransform.pivot;
rectTransform.pivot = targetPivot;
var diffPos = new Vector2(rectTransform.sizeDelta.x * diffPivot.x, rectTransform.sizeDelta.y * diffPivot.y);
rectTransform.anchoredPosition += diffPos;
}
}
#endif
Reference
이 문제에 관하여(장면 뷰에서 RayCastPadding 시각화), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Basuhei_Acheron/items/54167e4d1afa011a4cc6텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)