2023. 8. 28. 04:34
Unity
KillPeed 라고도 한다
어려운건 아닌데 ㅈㄴ 귀찮다
계층구조는 이렇고
컴포넌트는 하단처럼 심어주자
사용된 코드는 이 두개
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class KillPeed : MonoBehaviour
{
public KillPeedSlot killPeedSlotPrefab;
List<KillPeedSlot> killPeedSlots= new List<KillPeedSlot>();
public float displayTime = 5; //5초후삭제
public Color enemyColor=Color.red;
public Color allyColor = Color.blue;
// Start is called before the first frame update
void Start()
{
killPeedSlotPrefab.gameObject.SetActive(false);
}
public void Add(string source, string target,Sprite icon, Color sourceColor, Color targetColor, bool critical = false, bool highlight=false)
{
killPeedSlots = killPeedSlots.FindAll(x => x != null);//시간경과로 지워진건 제외
if (killPeedSlots.Count>=3)//3칸 넘으면 오래된거 삭제
{
Destroy(killPeedSlots[0].gameObject);
}
var instant = Instantiate(killPeedSlotPrefab, killPeedSlotPrefab.transform.parent);
instant.source.color = sourceColor;
instant.target.color = targetColor;
instant.source.text = source;
instant.target.text = target;
instant.icon.sprite = icon;
instant.critical.SetActive(critical);
instant.gameObject.SetActive(true);
if (highlight)
{
instant.Highlight();
}
Destroy(instant.gameObject, displayTime);
killPeedSlots.Add(instant);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class KillPeedSlot : MonoBehaviour
{
public Text source;
public GameObject critical;
public Image icon;
public Text target;
void Update()
{
GetComponent<ContentSizeFitter>().enabled = false;
GetComponent<ContentSizeFitter>().enabled = true;
}
public void Highlight()
{
var color = GetComponent<Image>().color;
color.r = 1f- color.r;
color.g = 1f - color.g;
color.b = 1f - color.b;
GetComponent<Image>().color = color;
icon.color = Color.black;
}
}
끝
'Unity' 카테고리의 다른 글
유니티 APK 용량 (0) | 2023.09.08 |
---|---|
유니티 플랫폼 훅 (0) | 2023.08.23 |
유니티 웨이포인트식 자율주행 (0) | 2023.08.19 |