아이템 획득시, 장착 연습
2023. 8. 11.

아이템을 장착하는 연습을 하기위해 새로운 씬을 생성하였다.

-TestEquipScene을 만들어주었다.

 

장착을하려면=> 1. 원래 있던 장비를 제거하고 2. 새로 들어온 아이템을 장착한

다.

따라서 먼저 장비를 제거해보도록 하겠다.

버튼 추가후 메인 수정

-SerializeField로 버튼을 넣어준다. 인스펙터에서 버튼을 직접 할당해주어야 한다.

-Sword는 WeaponTransform 자식으로 붙어있다.

 

-위와 같이 작성해도 삭제는 잘 되지만, 무기의 자식으로 접근해서 하는 것으로 코드를 수정해보았다.

-동일하게 작동한다.

버튼 두개를 추가해 두가지 방법으로 검을 착용하도록 했다.

main수정
Hero수정. Hero가 무기를 갖고있는지 여부이므로 여기에 작성

-방패를 착용하는 것을 추가하였다.

-HeroController에서 위치정보에 관한 부분을 코드 수정하고, 방패 프리팹을 메인에 추가하였다.

 

변수수정

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Test4
{
    public class HeroController : MonoBehaviour
    {
        [SerializeField] private Transform swordTrans;
        private Transform weaponTrans;
        [SerializeField] private Transform shieldTrans;
        public Transform WeaponTrans
        {
            get { return weaponTrans; }
        }
        // Start is called before the first frame update
        void Start()
        {
            
        }

        // Update is called once per frame
        void Update()
        {

        }
        public bool HasWeapon()
        {
            return this.weaponTrans.childCount > 0;
        }
        public Transform ShieldTransform()
        {
            this.weaponTrans = this.shieldTrans;
            return this.weaponTrans;
        }
        public Transform SwordTransform()
        {
            this.weaponTrans = this.swordTrans;
            return this.weaponTrans;
        }

        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);
            }
        }

        

       
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Test4
{
    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;//생성후 부모를 지정
       // private GameObject weaponGo;
        [SerializeField] private GameObject swordPrefab;//가져올 아이템의 프리팹1-검
        [SerializeField] private GameObject shieldPrefab;//가져올 아이템의 프리팹2-방패
        private Test4.HeroController heroController;
        // Start is called before the first frame update
        void Start()
        {
          
            this.heroController = FindAnyObjectByType<Test4.HeroController>();

            this.btnRemoveSword.onClick.AddListener(() =>
            {//버튼을 클릭하면 Sword제거
                Debug.Log("OnClickRemove");
                heroController.SwordTransform();
                heroController.UnEquipWeapon();
            });
            this.btnEquipSword0.onClick.AddListener(() =>
            {//버튼을 클릭하면 Sword생성, 생성시 부모를지정
                Debug.Log("OnClickEquip0");
                Debug.Log("생성시 부모를 지정");
                heroController.SwordTransform();
                bool hasWeapon = this.heroController.HasWeapon();
                if (!hasWeapon)
                {
                    Instantiate(this.swordPrefab, this.heroController.WeaponTrans);
                }
                else
                {
                    Debug.Log("이미 착용중입니다.");
                }
            });
            this.btnEquipSword1.onClick.AddListener(() =>
            {//버튼을 클릭하면 Sword생성, 생성후 부모를지정 => 생성 위치에 문제가 생긴다.
                Debug.Log("OnClickEquip1");
                heroController.SwordTransform();
                bool hasWeapon = this.heroController.HasWeapon();
                if (!hasWeapon)
                {
                    GameObject go = Instantiate(this.swordPrefab);
                    go.transform.SetParent(this.heroController.WeaponTrans);//부모를 지정
                    go.transform.localPosition = Vector3.zero;
                    go.transform.localRotation = Quaternion.identity;//회전을 초기화
                }
                else
                {
                    Debug.Log("이미 착용중입니다.");
                }
            });

            this.btnRemoveShield.onClick.AddListener(() =>
            {//버튼을 클릭하면 Shield제거
                Debug.Log("OnClickRemove");
                heroController.ShieldTransform();
                heroController.UnEquipWeapon();
            });
            this.btnEquipShield0.onClick.AddListener(() =>
            {//버튼을 클릭하면 Sword생성, 생성시 부모를지정
                Debug.Log("OnClickEquip0");
                Debug.Log("생성시 부모를 지정");
                heroController.ShieldTransform();
                bool hasWeapon = this.heroController.HasWeapon();
                if (!hasWeapon)
                {
                    Instantiate(this.shieldPrefab, this.heroController.WeaponTrans);
                }
                else
                {
                    Debug.Log("이미 착용중입니다.");
                }
            });
            this.btnEquipShield1.onClick.AddListener(() =>
            {//버튼을 클릭하면 Sword생성, 생성후 부모를지정 => 생성 위치에 문제가 생긴다.
                Debug.Log("OnClickEquip1");
                heroController.ShieldTransform();
                bool hasWeapon = this.heroController.HasWeapon();
                if (!hasWeapon)
                {
                    GameObject go = Instantiate(this.shieldPrefab);
                    go.transform.SetParent(this.heroController.WeaponTrans);//부모를 지정
                    go.transform.localPosition = Vector3.zero;
                   // go.transform.localRotation = Quaternion.identity;//회전을 초기화
                    go.transform.localRotation = Quaternion.Euler(new Vector3(-0.315f,-90,0));
                    //go.transform.localRotation = heroController.WeaponTrans.rotation;
                }
                else
                {
                    Debug.Log("이미 착용중입니다.");
                }
            });
        }

        // Update is called once per frame
        void Update()
        {

        }

      
    }
}

 

myoskin