::武器を変える。

youtu.be/SlFWpbw6N3Q

>>改善すべき:武器変更を他のスクリプトに入れて使う?

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

public class TestApp3 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        // :: Initialise Buttons
        Button btnNoHand = GameObject.Find("Button_NoHand").GetComponent<Button>();   
        Button btnAxe = GameObject.Find("Button_Axe").GetComponent<Button>();   
        Button btnSpear = GameObject.Find("Button_Spear").GetComponent<Button>();

        // :: Create Character and Instantiate
        GameObject unit = Object.Instantiate(Resources.Load<GameObject>("ch_03_01"));

        // :: Initialise dummyRHand
        Transform dummyRHand = null;

        // :: Find DummyRHand and Remember it
        Transform[] unitChildren = unit.transform.GetComponentsInChildren<Transform>();
        foreach(var child in unitChildren)
        {
            if(child.name == "DummyRHand")
            {
                dummyRHand = child;
                break;
            }
        }

        // :: Initialise currentWeaponName
        string currentWeaponName = null;

        // :: Add Event Listener : Button No Hand
        btnNoHand.onClick.AddListener(() =>
        {
            // :: When you have weapon
            if(currentWeaponName != null)
            {
                // :: Find current weapon by name
                Transform currentWeapon = dummyRHand.transform.Find(currentWeaponName);

                // :: Release parent
                currentWeapon.transform.SetParent(null);

                // :: Destroy current weapon
                Object.Destroy(currentWeapon.gameObject);

                // :: Set Current Weapon Name : null
                currentWeaponName = null;
            }
        });

        // :: Add Event Listener : Button Axe
        btnAxe.onClick.AddListener(() =>
        {
            // :: When you don't have weapon
            if (currentWeaponName == null)
            {
                // :: Resources Load and Set Parent
                var currentWeapon = Object.Instantiate<GameObject>(Resources.Load<GameObject>("Axe_3"));
                currentWeapon.transform.SetParent(dummyRHand, false);

                // :: Set Current Weapon Name
                currentWeaponName = currentWeapon.name;
            }
        });

        // :: Add Event Listener : Button Spear
        btnSpear.onClick.AddListener(() =>
        {
            // :: When you don't have weapon
            if (currentWeaponName == null)
            {
                // :: Resources Load and Set Parent
                var currentWeapon = Object.Instantiate<GameObject>(Resources.Load<GameObject>("Spear_7"));
                currentWeapon.transform.SetParent(dummyRHand);
                currentWeapon.transform.localPosition = Vector3.zero;
                currentWeapon.transform.localRotation = Quaternion.Euler(Vector3.zero);

                // :: Set Current Weapon Name
                currentWeaponName = currentWeapon.name;
            }
        });

    }
}
블로그 이미지

RIsN

,