c# 강의/유니티

20200507 - 캐릭터 생성 및 무기 장착

쪼혀 2020. 5. 8. 14:32

 

 

 

 


 

> Code

더보기

> App.cs

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


public class App : MonoBehaviour
{
    public Button btn;
    public GameObject heroPrefab;
    public GameObject weaponPrefab;

    private void Start()
    {
        this.btn.onClick.AddListener(() => {
            var heroModelGo = this.CreateModel();
            var weaponGo = this.CreateWeapon();
            var dummyRHand = heroModelGo.GetComponent<HeroModel>().dummyRHand;
            weaponGo.transform.SetParent(dummyRHand, false);
        });
    }

    private GameObject CreateModel()
    {
        return Instantiate(this.heroPrefab);
    }
    private GameObject CreateWeapon()
    {
        return Instantiate(this.weaponPrefab);
    }

}

 

>HeroModel.cs 

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

public class HeroModel : MonoBehaviour
{

    public Transform dummyRHand;

    // Start is called before the first frame update
    void Start()
    {
        
    }

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