UI 구조
Scene Main >> UI Component >> Button / Text / Image 등등
ON OFF 스위치 버튼 만들기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UISwichButton : MonoBehaviour
{
[SerializeField] private GameObject onGo;
[SerializeField] private GameObject offGo;
bool isOn = false; // 비활성화
private void Start()
{
onGo = transform.GetChild(0).gameObject;
offGo = transform.GetChild(1).gameObject;
}
public void SwitchButtonImage() //
{
isOn = !isOn;
onGo.SetActive(!isOn); // false 비활성화
offGo.SetActive(isOn); // true 활성화
}
}
게임 오브젝트 내 게임 오브젝트
즉, 하위 게임오브젝트를 찾는 방법
transform.FindChild(string str)
transform.GetChild(int index)
1. 이름으로 자식 GameObject를 찾는 방법
(자기 자신)transform.FindChild("이름");
2. 번호 순으로 자식 GameObject를 찾는 방법
(자기 자신)transform.GetChild(번호);
여기서 자기 자신이란, 해당 스크립트가 컴포넌트가 된 상태를 말한다.
만약, 외부의 게임오브젝트를 찾는 것은 앞에 어떤 GameObject인지를 명시해줘야 한다.
3. 이름으로 다른 게임오브젝트의 자식 GameObject를 찾는 방법
(외부게임오브젝트)otherGameobject.transform.FindChild("이름");
4. 번호 순으로 다른 게임오브젝트의 자식 GameObject를 찾는 방법
(외부게임오브젝트)otherGameobject.transform.GetChild(번호);
탭 메뉴 클릭하기
활성화 / 비활성화 매서드 만들기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UITap : MonoBehaviour
{
[SerializeField] private GameObject activeGo;
[SerializeField] private GameObject deactiveGo;
public Button btn;
public void Deactivate() //비활성화
{
this.activeGo.SetActive(false);
this.deactiveGo.SetActive(true);
}
public void Actiavate() //활성화
{
this.activeGo.SetActive(true);
this.deactiveGo.SetActive(false);
}
}
전체 탭 메뉴에서 탭을 클릭할때 적용
탭을 클릭하면 활성화
-> 다른 탭은 비활성화
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UITapMenu : MonoBehaviour
{
[SerializeField] private UITap[] uitabs;
void Start()
{
for(int i = 0; i < this.uitabs.Length; i++)
{
UITap tap = uitabs[i];
//변수정의
int idx = i;
tap.btn.onClick.AddListener(() =>
{
Debug.Log(idx); //0,1
this.DeactiveAll(); //전체 비활성화
tap.Actiavate();// 클릭하는 탭 활성화
}); //람다식
}
this.DeactiveAll(); //다른 탭은 비활성화함.
}
private void DeactiveAll()
{
for (int i = 0; i < this.uitabs.Length; i++)
{
UITap tab = uitabs[i];
tab.Deactivate();
}
}
}
Button.onClick.~~
AddListener | UnityEvent에 비지속적 리스너를 추가합니다. |
Invoke | 등록된 모든 콜백(런타임 및 영구)을 호출합니다. |
RemoveListener | UnityEvent에서 비지속적 리스너를 제거합니다. |
GetPersistantEventCount | 등록된 영구 리스너 수를 가져옵니다. |
GetPerpersistMethodName | 인덱스 index에서 리스너의 대상 메소드 이름을 가져옵니다. |
GetPerciousTarget | 인덱스 index에서 리스너의 대상 구성 요소를 가져옵니다. |
RemoveAllListeners | 이벤트에서 모든 비지속적(즉, 스크립트에서 생성된) 리스너를 제거합니다. |
SetPertantListenerState | 영구 리스너의 실행 상태를 수정합니다. |
'게임개발 > 게임 UI.UX 프로그래밍' 카테고리의 다른 글
Input 필드 UI만들기 (0) | 2024.02.07 |
---|---|
로딩바 만들기 (0) | 2024.02.07 |
동기 비동기 (0) | 2024.02.05 |
직렬화 & 역직렬화 (1) | 2024.02.05 |
유니티 싱글톤 패턴 (0) | 2024.02.05 |