2021. 10. 16. 13:23
Unity/C#
float canvasHei = GetComponentInParent<Canvas>().GetComponent<RectTransform>().rect.height; //현재 캔버스의 높이를 반환
float oneCentimeterPixelCount = (1 / screenHeightCentimeter) * canvasHei //1센치에 대한 캔버스 픽셀사이즈를 반환
//RectTransform계열
float wid = GetComponent<RectTransform>().sizeDelta.x; //rect.width와 동일
float wid = GetComponent<RectTransform>().rect.width; //sizeDelta.x와 동일
textUI.rectTransform.anchoredPosition.x //일반적으로 생각하는 rectTransform의 x좌표
textUI.rectTransform.localPosition.x //Anchors가 미적용된 x좌표, 가급적 사용자제
textUI.rectTransform.rect.x //작동안함, anchoredPosition써라
사이즈 조정
public GameObject board;
var rectTransform = board.GetComponent<RectTransform>();
var sizeDelta = rectTransform.sizeDelta;
sizeDelta.y = 400;
rectTransform.sizeDelta = sizeDelta;
Rect 사이즈를 표시
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawRectSize : MonoBehaviour
{
public Color gizmoColor=Color.white;
void OnDrawGizmos()
{
// Draw a semitransparent blue cube at the transforms position
var rectTransform = GetComponent<RectTransform>();
if (rectTransform == null)
{
return;
}
var lossyScale = rectTransform.lossyScale;
float wid = rectTransform.rect.width;
float hei = rectTransform.rect.height;
var pivot = rectTransform.pivot;
Vector3 downPos = transform.up * lossyScale.y * hei * (pivot.y - 1);
Vector3 upPos = transform.up * lossyScale.y * hei * pivot.y;
Vector3 leftPos = transform.right * lossyScale.x * wid * (pivot.x - 1);
Vector3 rightPos = transform.right * lossyScale.x * wid * pivot.x;
Gizmos.color = gizmoColor;
Gizmos.DrawLine(transform.position + downPos + leftPos, transform.position + upPos + leftPos);
Gizmos.DrawLine(transform.position + downPos + rightPos, transform.position + upPos + rightPos);
Gizmos.DrawLine(transform.position + downPos + leftPos, transform.position + downPos + rightPos);
Gizmos.DrawLine(transform.position + upPos + leftPos, transform.position + upPos + rightPos);
}
}
특정 시야각만큼 차지하는 RectSize를 가져옴
(Canvas, Camera) GetCanvasCamera()
{
var canvas = GetComponentInParent<Canvas>();
var camera = canvas.worldCamera;
if (camera == null)
{
camera = Camera.main;
}
return (canvas,camera);
}
Vector2 GetRectSize(float angle)
{
(Canvas canvas, Camera camera) = GetCanvasCamera();
if (camera == null)
{
return Vector2.zero;
}
var canvasSize = canvas.GetComponent<RectTransform>().rect.size;
var worldPos = camera.transform.position + camera.transform.rotation * (Quaternion.Euler(angle, 0, 0) * Vector3.forward * 2);
{
//debug
var worldPosb = camera.transform.position + camera.transform.rotation * (Quaternion.Euler(-angle, 0, 0) * Vector3.forward * 2);
Debug.DrawLine(camera.transform.position, worldPos, Color.yellow);
Debug.DrawLine(camera.transform.position, worldPosb, Color.yellow);
Debug.DrawLine(worldPos, worldPosb, Color.yellow);
}
var screenPos = camera.WorldToScreenPoint(worldPos);
var center = camera.ViewportToScreenPoint(Vector2.one * 0.5f);
var screenSize = camera.ViewportToScreenPoint(Vector2.one);
var newSize = Vector2.one * Mathf.Abs(center.y - screenPos.y) * 2;
newSize *= canvasSize.y / screenSize.y;
return newSize;
}
화면상 RectTransform의 위치
GetComponent<RectTransform>().position //절대적
GetComponent<RectTransform>().localPosition //상대적
'Unity > C#' 카테고리의 다른 글
유니티 기울기 관련 (0) | 2021.10.23 |
---|---|
C# 시리얼 (0) | 2021.10.11 |
C# Delegate와 IEnumerator (0) | 2021.10.11 |