Test_EquipItemMain
GameObject.transform.SetParent(null)을 하면 아이템이 드랍되는 효과
csharp
닫기using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Test_EquipItem
{
public class Test_EquipItemMain : MonoBehaviour
{
[SerializeField]
private Button btnRemoveSword;
[SerializeField]
private Button btnEquipSword0;
[SerializeField]
private Button btnEquipSword1;
[SerializeField]
private Button btnRemoveShield;
[SerializeField]
private Button btnEquipShield0;
[SerializeField]
private Button btnEquipShield1;
[SerializeField]
private GameObject swordPrefab;
[SerializeField]
private GameObject shieldPrefab;
[SerializeField]
private HeroController heroController;
// Start is called before the first frame update
void Start()
{
this.btnRemoveShield.onClick.AddListener(() => {
//무기를 제거
this.heroController.UnEquipShield();
});
this.btnEquipShield0.onClick.AddListener(() => {
//생성 시 부모를 지정
bool hasShield = this.heroController.HasShield();
//true가 아니라면
if(!hasShield)
{
GameObject go = Instantiate(this.shieldPrefab, this.heroController.Shield);
//회전을 초기화
go.transform.localRotation = Quaternion.Euler(new Vector3(0, -100, 0));
}
else //쉴드가 있다면
{
Debug.Log("쉴드가 있습니다.");
}
});
this.btnEquipShield1.onClick.AddListener(() => {
//생성 후 부모를 지정
bool hasShield = this.heroController.HasShield();
if(!hasShield)
{
//쉴드가 없다면
GameObject go = Instantiate(this.shieldPrefab);
//부모를 지정 null하면 아이템 떨어짐
//go.transform.SetParent(null);
go.transform.SetParent(this.heroController.Shield);
//위치를 초기화
go.transform.localPosition = Vector3.zero;
//회전을 초기화
go.transform.localRotation = Quaternion.Euler(new Vector3(0, -100, 0));
//스케일을 초기화 X
}
else
{
//쉴드가 있다면
Debug.Log("쉴드가 있습니다.");
}
});
this.btnRemoveSword.onClick.AddListener(() => {
//무기를 제거
this.heroController.UnEquipSword();
});
this.btnEquipSword0.onClick.AddListener(() => {
//새롭시 장착할 검의 인스턴스
bool hasWeapon = this.heroController.HasSword();
if(!hasWeapon)
{
GameObject go = Instantiate(this.swordPrefab, this.heroController.SwordTrans);
}
else
{
Debug.Log("이미 착용중입니다.");
}
});
this.btnEquipSword1.onClick.AddListener(() => {
bool hasWeapon = this.heroController.HasSword();
if (!hasWeapon)
{
//프리팹의 인스턴스 생성
GameObject go = Instantiate(this.swordPrefab);
//부모를 지정
go.transform.SetParent(this.heroController.SwordTrans);
//위치를 초기화
Debug.LogFormat("월드좌표: {0}", go.transform.position); // 월드 상의 좌표
Debug.LogFormat("로컬좌표: {0}", go.transform.localPosition); // 로컬 상의 좌표
go.transform.localPosition = Vector3.zero;
go.transform.localRotation = Quaternion.identity;
//go.transform.localPosition = new Vector3(0,0,0);
//회전을 초기화
//스케일을 초기화 (x)
}
else
{
Debug.Log("이미 착용중입니다");
}
});
}
// Update is called once per frame
void Update()
{
}
}
}
HeroController
csharp
닫기using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Test_EquipItem
{
public class HeroController : MonoBehaviour
{
[SerializeField]
private Transform swordTrans;
[SerializeField]
private Transform shieldTrans;
public Transform SwordTrans
{
get
{
return this.swordTrans;
}
}
public Transform Shield
{
get
{
return this.shieldTrans;
}
}
//private GameObject weaponGo; //착용중인 무기 게임오브젝트
public void UnEquipShield()
{
//자식이 있는가?
Debug.LogFormat("자식의 수: {0}", this.shieldTrans.childCount);
if(this.shieldTrans.childCount == 0)
{
//자식이 없다. (착용중인 방패가 없다.)
}
else
{
//자식이 있다. (착용중인 방패가 있다.)
Transform child = this.shieldTrans.GetChild(0);
//방패를 제거
Destroy(child.gameObject);
}
}
public bool HasShield()
{
return this.shieldTrans.childCount > 0;
}
public void UnEquipSword()
{
//자식이 있는가?
Debug.LogFormat("자식의 수 : {0}", this.swordTrans.childCount);
if (this.swordTrans.childCount == 0)
{
//자식이 없다 (착용중인 무기가 없다)
Debug.Log("착용중인 무기가 없습니다.");
}
else
{
//자식이 있다 (착용중인 무기가 있다)
Transform child = this.swordTrans.GetChild(0); // 첫번째 자식
//무기를 제거
Destroy(child.gameObject);
}
//if(this.weaponGo != null)
//{
// Destroy(this.weaponGo);
//}
//else
//{
// Debug.Log("착용중인 무기가 없습니다.");
//}
}
public bool HasSword()
{
return this.swordTrans.childCount > 0;
}
}
}

'유니티 기초' 카테고리의 다른 글
[합치기] 이동, 공격, 피격 (0) | 2023.08.11 |
---|---|
FadeIn, FadeOut (0) | 2023.08.11 |
몬스터 2초 후 삭제, 아이템 드랍, 아이템 씬에서 제거 후 아이템 로그 출력 (0) | 2023.08.10 |
몬스터를 죽이면 리스트에서 없애고 임의 위치에 포탈생성 (0) | 2023.08.10 |
몬스터 동적으로 데이터관리 생성 (0) | 2023.08.10 |