KDT/유니티 심화
23/09/07 LearnUGUI (구조잡고 정적 스크롤뷰 관리 하는 스크립트 만들기)
잰쟁
2023. 9. 7. 12:37
728x90
**상속
-부모 클래스(기반클래스): Base
-자식 클래스(파생 클래스): Derived
-부모: Virtual / 자식: Override 를 붙여줘야함
[Start에 입력]
- 호출 순서가 제멋대로..
(Golden>Epic>Legendary>Silver>Wooden 순서)
자식에서 부모를 상속받으려면
부모에서 'protected'로 해줘야 상속받을 수 있음!!!!!!!
UIChestCell
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIChestCell : MonoBehaviour
{
public enum eChestType
{
Wooden, Silver, Golden, Epic, Legendary
}
//상속을 하려면 'protected'로 변경해야함!!!!!
[SerializeField]
protected Button btnPrice;
[SerializeField]
protected int price;
[SerializeField]
protected eChestType chestType;
void Start()
{
Debug.LogFormat("[UIChestCell] Start : {0}", this.chestType);
this.btnPrice.onClick.AddListener(() => {
Debug.LogFormat("{0}, {1}", this.chestType, this.price);
});
}
}
UIChestCellAd
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIChestCellAd : UIChestCell
{
[SerializeField]
private Button btnAd;
void Start()
{
Debug.LogFormat("[UIChestCellAd] Start : {0}",this.chestType);
//AD버튼 클릭하면 chestType과 광고보기 표시
this.btnAd.onClick.AddListener(() =>
{
Debug.LogFormat("{0}, 광고 보기",this.chestType);
});
//price버튼 클릭하면 chestType과 price 표시
this.btnPrice.onClick.AddListener(() =>
{
Debug.LogFormat("{0} ,{1}", this.chestType, this.price);
});
}
}
[Init 메서드 생성하여 입력]
-호출 순서 통제 가능
(Wooden>Silver>Golden>Epic>Legendary 순으로 출력됨)
UIChestScrollView
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIChestScrollView : MonoBehaviour
{
//UIChestCell 배열만들기
[SerializeField]
private UIChestCell[] cells;
void Start()
{
for(int i = 0; i < cells.Length; i++)
{
UIChestCell cell = cells[i];
//초기화
cell.Init();
}
}
}
UIChestCell
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIChestCell : MonoBehaviour
{
public enum eChestType
{
Wooden, Silver, Golden, Epic, Legendary
}
//상속을 하려면 'protected'로 변경해야함!!!!!
[SerializeField]
protected Button btnPrice;
[SerializeField]
protected int price;
[SerializeField]
protected eChestType chestType;
public virtual void Init()
{
Debug.LogFormat("[UIChestCell] Start : {0}", this.chestType);
this.btnPrice.onClick.AddListener(() => {
Debug.LogFormat("{0}, {1}", this.chestType, this.price);
});
}
}
UIChestCellAd
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIChestCellAd : UIChestCell
{
[SerializeField]
private Button btnAd;
//부모의 Init메서드 상속받아 가져오기
public override void Init()
{
//부모 Init
base.Init();
Debug.LogFormat("[UIChestCellAd] Start : {0}",this.chestType);
//AD버튼 클릭하면 chestType과 광고보기 표시
this.btnAd.onClick.AddListener(() =>
{
Debug.LogFormat("{0}, 광고 보기",this.chestType);
});
}
}