728x90
숫자 표시하기
UI작업
GraphContainer에 x좌표와 y좌표의 값을 나타낼 때 사용할 Text박스를 만든다. (TMP로 만듦 상관없음.)
만들어 놓은 labelTemplateX, labelTemplateY는 상단 체크박스를 통해 active false로 만들어 준다.
소스코드
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class UI_WindowGraph : MonoBehaviour
{
[SerializeField]
private Sprite circleSprite;
private RectTransform graphContainer;
private RectTransform labelTemplateX;
private RectTransform labelTemplateY;
private void Awake()
{
graphContainer = transform.Find("graphContainer").GetComponent<RectTransform>();
labelTemplateX = graphContainer.Find("labelTemplateX").GetComponent<RectTransform>();
labelTemplateY = graphContainer.Find("labelTemplateY").GetComponent<RectTransform>();
List<int> valueList = new List<int>() { 5, 10, 98, 56, 45, 30, 22, 17, 15, 13, 17, 25, 37, 40, 36, 33 };
ShowGraph(valueList);
}
private GameObject CreateCircle(Vector2 anchoredPosition)
{
GameObject gameObject = new GameObject("circle", typeof(Image));
gameObject.transform.SetParent(graphContainer, false);
gameObject.GetComponent<Image>().sprite = circleSprite;
RectTransform rectTransform = gameObject.GetComponent<RectTransform>();
rectTransform.anchoredPosition = anchoredPosition;
rectTransform.sizeDelta = new Vector2(11, 11);
rectTransform.anchorMin = new Vector2(0, 0);
rectTransform.anchorMax = new Vector2(0, 0);
return gameObject;
}
void ShowGraph(List<int> valueList)
{
float graphHeight = graphContainer.sizeDelta.y;
float yMaimum = 100f;
float xSize = 50f;
GameObject lastCircleGameObject = null;
for (int i = 0; i < valueList.Count; i++)
{
float xPostion = xSize + i * xSize;
float yPosition = (valueList[i] / yMaimum) * graphHeight;
GameObject circleGameObject = CreateCircle(new Vector2(xPostion, yPosition));
if(lastCircleGameObject != null)
{
CreateDotConnection(lastCircleGameObject.GetComponent<RectTransform>().anchoredPosition, circleGameObject.GetComponent<RectTransform>().anchoredPosition);
}
lastCircleGameObject = circleGameObject;
RectTransform labelX = Instantiate(labelTemplateX);
labelX.SetParent(graphContainer, false);
labelX.gameObject.SetActive(true);
labelX.anchoredPosition = new Vector2(xPostion, -7f);
labelX.GetComponent<TMP_Text>().text = i.ToString();
}
int separatorCount = 10;
for (int i = 0; i < separatorCount; i++)
{
RectTransform labelY = Instantiate(labelTemplateY);
labelY.SetParent(graphContainer, false);
labelY.gameObject.SetActive(true);
float normalizedValue = i * 1f/ separatorCount;
labelY.anchoredPosition = new Vector2(-7f, normalizedValue * graphHeight);
labelY.GetComponent<TMP_Text>().text = Mathf.RoundToInt( normalizedValue * yMaimum).ToString();
}
}
void CreateDotConnection(Vector2 dotPositionA, Vector2 dotPositionB)
{
GameObject gameObject = new GameObject("dotConnection", typeof (Image));
gameObject.transform.SetParent(graphContainer, false);
gameObject.GetComponent<Image>().color = new Color(1, 1, 1, 0.5f);
RectTransform rectTransform = gameObject.GetComponent<RectTransform>();
Vector2 dir = (dotPositionB - dotPositionA).normalized;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
float distance = Vector2.Distance(dotPositionA, dotPositionB);
rectTransform.anchorMin = new Vector2(0, 0);
rectTransform.anchorMax = new Vector2(0, 0);
rectTransform.sizeDelta = new Vector2(distance, 3f);
rectTransform.anchoredPosition = dotPositionA + (dir) * distance * 0.5f ;
rectTransform.localEulerAngles = new Vector3(0,0, angle);
}
}
결과
728x90
'Unity' 카테고리의 다른 글
[Unity] Addresable 어드레서블를 이용한 웹서버 원격 로드방법 (0) | 2024.03.26 |
---|---|
[Unity] Unity 파이프라인 이란? (0) | 2024.03.12 |
[Unity] LINE CHART 그래프 만들기 (1) (0) | 2023.11.20 |
[Unity] UI 마우스 Drag로 UI 이동 방법 (0) | 2023.10.27 |
[Unity] Unity 프로빌더 Probuilder (0) | 2023.10.11 |