아카이브
아이템 착용하기 본문
프리팹 생성 시 바로 부모를 설정하는 경우와
프리팹 인스턴스를 생성한 뒤에 부모를 설정하는 경우의 차이
Test_EquipItemMain.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test_EquipItemMain : MonoBehaviour
{
[SerializeField]
private Button btnRemoveSword;
[SerializeField]
private Button btnRemoveSword0;
[SerializeField]
private Button btnRemoveSword1;
[SerializeField]
private GameObject battleAxePrehab;
[SerializeField]
private Test_EquipItem.HeroController heroController;
// Start is called before the first frame update
void Start()
{
//버튼 이벤트 등록
this.btnRemoveSword.onClick.AddListener(() => {
Debug.Log("영웅의 칼이 있다면 씬에서 제거");
//무기제거
this.heroController.UnEquipWeapon();
});
this.btnRemoveSword0.onClick.AddListener(() => {
//새롭게 장착할 검(프리팹)의 인스턴스
Debug.Log("생성 시 부모를 지정");
bool hasWeapon = this.heroController.HasWeapon();
if (!hasWeapon)
{
Instantiate(this.battleAxePrehab, this.heroController.WeaponTrans);
}
else
{
Debug.Log("이미 착용중입니다.");
}
});
this.btnRemoveSword1.onClick.AddListener(() => {
Debug.Log("생성 후 부모를 지정");
bool hasWeapon = this.heroController.HasWeapon();
//착용중이 아니라면
if (!hasWeapon)
{
//프리팹 인스턴스 생성
GameObject go = Instantiate(this.battleAxePrehab);
//부모를 지정
go.transform.SetParent(this.heroController.WeaponTrans);
}
else
{
Debug.Log("이미 착용중입니다.");
}
});
}
HeroController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Test_EquipItem
{
public class HeroController : MonoBehaviour
{
[SerializeField]
private Transform weaponTrans;
public Transform WeaponTrans
{
get
{
return this.weaponTrans;
}
}
public bool HasWeapon()
{
return this.weaponTrans.childCount > 0;
}
public void UnEquipWeapon()
{
//자식이 있는가?
Debug.LogFormat("자식의 수: {0}", this.weaponTrans.childCount);
if(this.weaponTrans.childCount == 0)
{
//자식이 없다
Debug.Log("착용중인 무기가 없습니다");
}
else
{
//자식이 있다(착용중인 무기가 있다)
Transform child = this.weaponTrans.GetChild(0); //첫번째 자식
//무기를 제거
Destroy(child.gameObject);
}
}
}
}
프리팹 인스턴스를 생성한 후에 부모를 설정하는 경우 위치와 회전을 초기화 시키기
this.btnRemoveSword1.onClick.AddListener(() => {
Debug.Log("생성 후 부모를 지정");
bool hasWeapon = this.heroController.HasWeapon();
//착용중이 아니라면
if (!hasWeapon)
{
//프리팹 인스턴스 생성
GameObject go = Instantiate(this.battleAxePrehab);
//부모를 지정
go.transform.SetParent(this.heroController.WeaponTrans);
//위치를 초기화
Debug.LogFormat("월드좌표: {0}", go.transform.position); //월드좌표
Debug.LogFormat("로컬좌표: {0}", go.transform.localPosition); //지역좌표
go.transform.localPosition = Vector3.zero;
//회전을 초기화
go.transform.localRotation = Quaternion.identity;
}
else
{
Debug.Log("이미 착용중입니다.");
}
});
방패 추가함
Test_EquipItemMain.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test_EquipItemMain : MonoBehaviour
{
[SerializeField]
private Button btnRemoveSword;
[SerializeField]
private Button btnRemoveSword0;
[SerializeField]
private Button btnRemoveSword1;
[SerializeField]
private Button btnRemoveShield;
[SerializeField]
private Button btnRemoveShield0;
[SerializeField]
private Button btnRemoveShield1;
[SerializeField]
private GameObject shieldPrehab;
[SerializeField]
private GameObject battleAxePrehab;
[SerializeField]
private Test_EquipItem.HeroController heroController;
// Start is called before the first frame update
void Start()
{
//버튼 이벤트 등록
this.btnRemoveSword.onClick.AddListener(() => {
Debug.Log("영웅의 칼이 있다면 씬에서 제거");
//무기제거
this.heroController.UnEquipWeapon();
});
this.btnRemoveSword0.onClick.AddListener(() => {
//새롭게 장착할 검(프리팹)의 인스턴스
Debug.Log("생성 시 부모를 지정");
bool hasWeapon = this.heroController.HasWeapon();
if (!hasWeapon)
{
Instantiate(this.battleAxePrehab, this.heroController.WeaponTrans);
}
else
{
Debug.Log("이미 착용중입니다.");
}
});
this.btnRemoveSword1.onClick.AddListener(() => {
Debug.Log("생성 후 부모를 지정");
bool hasWeapon = this.heroController.HasWeapon();
//착용중이 아니라면
if (!hasWeapon)
{
//프리팹 인스턴스 생성
GameObject go = Instantiate(this.battleAxePrehab);
//부모를 지정
go.transform.SetParent(this.heroController.WeaponTrans);
//위치를 초기화
Debug.LogFormat("월드좌표: {0}", go.transform.position); //월드좌표
Debug.LogFormat("로컬좌표: {0}", go.transform.localPosition); //지역좌표
go.transform.localPosition = Vector3.zero;
//회전을 초기화
go.transform.localRotation = Quaternion.identity;
}
else
{
Debug.Log("이미 착용중입니다.");
}
});
this.btnRemoveShield.onClick.AddListener(() => {
Debug.Log("영웅의 방패가 있다면 씬에서 제거");
//무기제거
this.heroController.UnEquipShield();
});
this.btnRemoveShield0.onClick.AddListener(() => {
Debug.Log("방패 생성 시 부모를 지정");
bool hasShield = this.heroController.HasShield();
if (!hasShield) //착용중이 아니라면
{
Instantiate(this.shieldPrehab, this.heroController.ShieldTrans);
}
else
{
Debug.Log("이미 착용중입니다");
}
});
this.btnRemoveShield1.onClick.AddListener(() => {
Debug.Log("방패 생성 후 부모를 지정");
bool hasShield = this.heroController.HasShield();
if (!hasShield) //착용중이 아니라면
{
//프리팹 인스턴스 생성
GameObject go = Instantiate(this.shieldPrehab);
//부모를 지정
go.transform.SetParent(this.heroController.ShieldTrans);
//위치를 초기화
go.transform.localPosition = Vector3.zero;
//회전을 초기화 //-0.153f, -102.36f, -171.32f
go.transform.localRotation = Quaternion.Euler(new Vector3(-0.153f, -102.36f, -171.32f));
}
});
}
HeroController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Test_EquipItem
{
public class HeroController : MonoBehaviour
{
[SerializeField]
private Transform weaponTrans;
public Transform WeaponTrans
{
get
{
return this.weaponTrans;
}
}
[SerializeField]
private Transform shieldTrans;
public Transform ShieldTrans
{
get
{
return this.shieldTrans;
}
}
public bool HasWeapon()
{
return this.weaponTrans.childCount > 0;
}
public bool HasShield()
{
return this.shieldTrans.childCount > 0;
}
public void UnEquipWeapon()
{
//자식이 있는가?
Debug.LogFormat("자식의 수: {0}", this.weaponTrans.childCount);
if (this.weaponTrans.childCount == 0)
{
//자식이 없다
Debug.Log("착용중인 무기가 없습니다");
}
else
{
//자식이 있다(착용중인 무기가 있다)
Transform child = this.weaponTrans.GetChild(0); //첫번째 자식
//무기를 제거
Destroy(child.gameObject);
}
}
public void UnEquipShield()
{
//자식이 있는가?
Debug.LogFormat("자식의 수: {0}", this.shieldTrans.childCount);
if (this.shieldTrans.childCount == 0)
{
//자식이 없다
Debug.Log("착용중인 방패가 없습니다");
}
else
{
//자식이 있다(착용중인 방패가 있다)
Transform child = this.shieldTrans.GetChild(0); //첫번째 자식
//무기를 제거
Destroy(child.gameObject);
}
}
}
}
'유니티 기초' 카테고리의 다른 글
보스 만들기/시야 안에 타겟이 있는지 검사 (0) | 2023.08.14 |
---|---|
페이드인, 페이드 아웃 연습 (0) | 2023.08.14 |
클릭한 위치에 몬스터 생성/삭제 (0) | 2023.08.10 |
주말과제(Dog Knight) (1) | 2023.08.06 |
유니티짱 직선으로 움직이기 (0) | 2023.08.04 |