::武器を変える。
>>改善すべき:武器変更を他のスクリプトに入れて使う?
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;
}
});
}
}
'Unity' 카테고리의 다른 글
Portfolio:事前検証 #1 (0) | 2020.11.17 |
---|---|
アカデミー#4:キー入力によって動く (0) | 2020.11.03 |
アカデミー#2:目標たちを辿って宝箱を開ける。 (0) | 2020.10.29 |
アカデミー#1:目標まで移動してヒット (0) | 2020.10.28 |
1 : UNITY=ANDROIDの記憶するべきもの (0) | 2020.10.27 |