만드려는 캐릭터 prefab파일에 HeroModel컴포넌트를 추가한다
1. App
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
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;
//true - 부모요소와 상대적으로 위치를 정함
//false - 독립적으로 위치를 정함
weaponGo.transform.SetParent(dummyRHand, false);
});
}
private GameObject CreateModel()
{
return Instantiate(this.heroPrefab);
}
private GameObject CreateWeapon()
{
return Instantiate(this.weaponPrefab);
}
}
|
2. HeroModel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
using JetBrains.Annotations;
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()
{
}
}
|
cs |
-----------------------------------------------------------------------------------------------------------------
캐릭터 모델 객체 구성인 Bip01의 하위 요소인 DummyRHand 에
무기를 드래그하여 밑에 놓으면 무기를 가진 캐릭터처럼 행동하는 것은 알 수 있었지만
코드적으로 Bip01 이나 DummyRHand 에 접근하는 방법을 찾지못해서
Hero 클래스에서 무기를 만들고 실체화 하는 것 까지 진행했습니다.
1. App
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class App : MonoBehaviour
{
public Hero hero;
public GameObject[] arrHero;
public string heroName;
void Start()
{
GameObject newHero = this.CreateNewHero(this.heroName);
hero.Init(newHero);
}
GameObject CreateNewHero(string name)
{
GameObject foundObject = null;
foreach(GameObject data in this.arrHero)
{
if(data.name==name)
{
foundObject = data.gameObject;
}
}
return Instantiate(foundObject);
}
}
|
2. Hero
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEditor.SceneManagement;
using UnityEngine;
public class Hero : MonoBehaviour
{
public GameObject model;
public GameObject weapon;
public void Init(GameObject model)
{
this.model = model;
this.model.transform.SetParent(this.gameObject.transform);
this.EquipWeapon(this.weapon);
}
public void EquipWeapon(GameObject weapon)
{
var newWeapon = Instantiate(weapon);
newWeapon.transform.position = new Vector3(0.218f, 0.04f, -0.014f);
newWeapon.transform.SetParent(this.model.transform);
}
}
|
3.
'C# > 과제' 카테고리의 다른 글
2020.05.12. 과제 - Scene전환, Json파일 로드, 캐릭터 생성 및 애니메이션 실행 (0) | 2020.05.12 |
---|---|
2020.05.10. 과제 - 캐릭터 달리기, 순위 매기기 (0) | 2020.05.10 |
2020.05.02. 과제 - 2048 게임(수정) (0) | 2020.05.02 |
2020.04.30. 과제 - 시간 제한이 있는 상점 구현 연습하기(쿠키런 방랑박쥐 상점) (0) | 2020.04.30 |
2020.04.27. 과제 - 승급하기 3 (메이플 스토리 / 몬스터, 레벨업) (0) | 2020.04.28 |