

public class Tile : MonoBehaviour
{
public bool hasUnits;
}




using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class UnitSlot : MonoBehaviour
{
public Sprite unitSprite; // 유닛 이미지
public GameObject unitObject; // 유닛 객체
public int cost;
public Image icon; // 유닛 이미지
public TextMeshProUGUI costText;
// GameManager 스크립트
private GameManager gameManager;
private void Start()
{
gameManager =
GameObject.Find("GameManager").GetComponent<GameManager>();
GetComponent<Button>().onClick.AddListener(BuyUnit);
}
private void BuyUnit()
{
gameManager.BuyUnit(unitObject, unitSprite);
}
public void OnValidate()
{
if(unitSprite) // Sprite가 있다면
{
icon.enabled = true;
icon.sprite = unitSprite;
costText.text = cost.ToString();
}
else
{
icon.enabled = false;
}
}
}
public Sprite unitSprite; // 유닛 이미지
public GameObject unitObject; // 유닛 객체
public int cost;
public Image icon; // 유닛 이미지
public TextMeshProUGUI costText;
// GameManager 스크립트
private GameManager gameManager;
Start함수에서 GameObject.Find를 사용해 GameManager라는 이름을 가진 게임오브젝트에 부착된 스크립트 가져오기
✅ 유닛 구입 버튼기능
private void Start()
{
gameManager =
GameObject.Find("GameManager").GetComponent<GameManager>();
GetComponent<Button>().onClick.AddListener(BuyUnit);
}
private void BuyUnit()
{
gameManager.BuyUnit(unitObject, unitSprite);
}
public void OnValidate()
{
if(unitSprite) // Sprite가 있다면
{
icon.enabled = true;
icon.sprite = unitSprite;
costText.text = cost.ToString();
}
else
{
icon.enabled = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public GameObject currentUnit;
public Sprite currentUnitSprite;
public Transform tiles;
public LayerMask tileMask;
public void BuyUnit(GameObject unit, Sprite sprite)
{
currentUnit = unit;
currentUnitSprite = sprite;
}
private void Update()
{
RaycastHit2D hit =
Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition),
Vector2.zero, Mathf.Infinity, tileMask);
// 반복문으로 타일 가져오고 비활성화
foreach(Transform tile in tiles)
{
tile.GetComponent<SpriteRenderer>().enabled = false;
}
// 콜라이더와 유닛이 충돌한다면
if(hit.collider && currentUnit)
{
// Sprite가져오고 컴포넌트 활성화
hit.collider.GetComponent<SpriteRenderer>().sprite = currentUnitSprite;
hit.collider.GetComponent<SpriteRenderer>().enabled = true;
if (Input.GetMouseButtonDown(0) && !hit.collider.GetComponent<Tile>().hasUnits)
{
Instantiate(currentUnit,
hit.collider.transform.position, Quaternion.identity);
hit.collider.GetComponent<Tile>().hasUnits = true;
currentUnit = null;
currentUnitSprite = null;
}
}
}
}

public GameObject currentUnit;
public Sprite currentUnitSprite;
public Transform tiles;
public LayerMask tileMask;
public void BuyUnit(GameObject unit, Sprite sprite)
{
currentUnit = unit;
currentUnitSprite = sprite;
}
GameManager에 선언한 함수가 public이기 때문에 다른 클래스에서 사용 가능
- [UnitSlot] 클래스의 스크립트
Physics2D.Raycast(시작좌표, Ray방향 & 크기, LayerMask);
✅ 여기서 Ray 란?
-> 특정좌표에서 방향과 길이를 가진 광선
private void Update()
{
RaycastHit2D hit =
Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition),
Vector2.zero, Mathf.Infinity, tileMask);
// 반복문으로 타일 가져오고 비활성화
foreach(Transform tile in tiles)
{
tile.GetComponent<SpriteRenderer>().enabled = false;
}
hit의 콜라이더와 현재유닛이 충돌한다면 -> 스프라이트 활성화 & 프로퍼티 활성화
마우스를 클릭하고 타일의 hasUnits함수가 False라면 -> 현재유닛 생성**
현재 유닛을 생성한 뒤 hasUnits를 True로 하여 그 타일에는 유닛이 배치되어있음을 나타냄
현재유닛 & 현재유닛 스프라이트 초기화
// 콜라이더와 유닛이 충돌한다면
if(hit.collider && currentUnit)
{
// Sprite가져오고 컴포넌트 활성화
hit.collider.GetComponent<SpriteRenderer>().sprite = currentUnitSprite;
hit.collider.GetComponent<SpriteRenderer>().enabled = true;
if (Input.GetMouseButtonDown(0) && !hit.collider.GetComponent<Tile>().hasUnits)
{
Instantiate(currentUnit,
hit.collider.transform.position, Quaternion.identity);
hit.collider.GetComponent<Tile>().hasUnits = true;
currentUnit = null;
currentUnitSprite = null;
}
}
게임 실행결과
- 유닛을 클릭하면 유닛의 스프라이트가 활성화되고 타일에 배치하게 되면 실제 오브젝트와 이미지가 배치
2024.11.13.수요일