[Unity] UI

박호준·2022년 2월 13일
0

Unity

목록 보기
10/17
  • Canvas

  • Sort Order : 캔버스 우선순위

Canvas Scaler

  • UI Scale Mode : Scale With Screen Size
  • Raycast Target : 클릭유무
  • Text : Horizental, Vertical => OVerFlow
  • Image : SourceImage:Barground => ImageType:Filed 자주씀
  • Button : (Text + Image) / Transition : 효과부여 / OnClick : 수행할 함수

쿨타임 만들기

using UnityEngine.UI;

public class UI : MonoBehaviour
{
    [SerializeField] private Text txt_name;
    [SerializeField] private Image img_name;

    private bool isCoolTime = false;
    private float currentTime = 5f;
    private float delayTime = 5f;

    private void Update()
    {
        if (isCoolTime)
        {
            currentTime -= Time.deltaTime;
            img_name.fillAmount = currentTime / delayTime;

            if (currentTime <= 0)
            {
                isCoolTime = false;
                currentTime = delayTime;
                img_name.fillAmount = currentTime;
            }
        }
        //img_name.sprite = sprite; img 바꾸기
        //img_name.color = Color.red; 색 바꾸기
        //img_name.color.a = 0f; 투명하게 하기
    }
    public void Change()
    {
        txt_name.text = "변경됨";
        isCoolTime = true;
    }
}

slider (체력, 경험치 등)

  • Brckground : 안채워진곳
  • Fill Area : 채워진곳 / 여백(left, right) 설정해줘야함
  • Handle Slide Area : 손잡이
  • Value : 값

버튼 클릭시 체력 달게 만들기

using UnityEngine.UI;
public class SliderButton : MonoBehaviour
{
    [SerializeField] private Slider slider;

    private bool isClick;
    private float dotTime = 1f;
    private float currentDotTime = 0f;

    private void Start()
    {
        currentDotTime = dotTime;
    }

    private void Update()
    {
        if (isClick)
        {
            currentDotTime -= Time.deltaTime;
            if (currentDotTime <= 0)
            {
                slider.value -= Time.deltaTime;
                if (currentDotTime <= -1f)
                {
                    currentDotTime = dotTime;
                }
            }
        }
    }
    public void Button()
    {
        isClick = true;
    }
}

Input field

  • character limit : 글자 수
  • Content Type : 글자 타입
  • Caret : 커서
  • On Value Changed : 값 입력마다 호출되는 함수
  • On End Edit : 값 입력이 완료 되었을 때 호출되는 함수

입출금 시스템

using UnityEngine.UI;
public class InputFields : MonoBehaviour
{
    [SerializeField] private Text text_money;
    [SerializeField] private InputField inputTxt_money;

    private int currentMoney;

    public void Input()
    {
        currentMoney += int.Parse(inputTxt_money.text);

        text_money.text = currentMoney.ToString();
    }

    public void Output()
    {
        currentMoney -= int.Parse(inputTxt_money.text);
        text_money.text = currentMoney.ToString();
    }
    
}

scroll bar

  1. Panel ->2. Scroll Rect -> 3. Contents -> 4.항목;
    -> 2. Scroll bar
  • grid Layout : 자식애들을 정렬해줌
  • Mask : 넘은 애들 잘라줌
  • scroll Rect : 스크롤
  • content size filtter : 원래대로 안 돌아게 할 수 있음
  • scrool Rect에서 Scroll bar을 넣을 수 있음
profile
hopark

0개의 댓글