11.05 Unity 객체, 이벤트, prefeb파일
C#/실습 2019. 11. 5. 17:50Unity:
App2는 Ingame을 관리 할 수있고 Ingame 안에서 Monster객체와 Hero 객체가 생성되면서 서로에게 데미지를 주고있다.
Monster객체는 계층 구조로 model을 갖고있으며 멤버변수로도 model을 갖고있다. (<--왜?)
코드:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class App : MonoBehaviour
{
private Ingame ingame;
// Start is called before the first frame update
void Start()
{
GameObject IngameGO = new GameObject("Ingame");
this.ingame = IngameGO.AddComponent<Ingame>();
ingame.StartGame();
}
// Update is called once per frame
void Update()
{
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
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
43
44
45
46
47
48
49
50
51
52
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ingame : MonoBehaviour
{
public Monster monster;
public Hero hero;
// Start is called before the first frame update
//void Awake()
//{
//}
void Start()
{
}
public void StartGame()
{
GameObject monsterGO = new GameObject("monsterGO");
GameObject heroGO = new GameObject("heroGO");
this.monster = monsterGO.AddComponent<Monster>();
this.hero = heroGO.AddComponent<Hero>();
var modelFiery = Instantiate(prefabFiery); //메모리에 올려져있는 데이터를 클론에 복제한다.
var modelCyclopes = Instantiate(prefabCyclopes);
monster.transform.position = new Vector3(0,0,0);
hero.transform.position = new Vector3(4, 0, 0);
modelFiery.transform.SetParent(monster.transform,false); //클론화된 모델 객체가 monster의 transform을 상속받는다.
modelCyclopes.transform.SetParent(hero.transform, false);
monster.model = prefabFiery;
hero.model = prefabCyclopes;
}
// Update is called once per frame
void Update()
{
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
public int hp;
public int damage;
public GameObject model;
// Start is called before the first frame update
void Start()
{
}
public void Attack(Character target)
{
target.Hit(this.damage);
}
public void Hit(int damage)
{
this.hp -= damage;
Debug.LogFormat("{0}이(가) 맞았습니다. 현재 체력:({1})", this.name, this.hp);
}
// Update is called once per frame
void Update()
{
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monster : Character
{
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero : Character
{
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'C# > 실습' 카테고리의 다른 글
11.08 Unity 동적 캐릭터 생성 후 사거리에 따라 공격기능 on/off (0) | 2019.11.08 |
---|---|
11.07 Unity monster객체에 역직렬화 데이터와 모델 데이터 넣기. (0) | 2019.11.07 |
10.31 인덱서를 이용해 LinkedList 클래스 만들기 (0) | 2019.10.31 |
10.31 제너릭을 이용해 사용자 지정 타입 활용하기 (where문 비활용) (0) | 2019.10.31 |
10.31 제너릭을 이용해 사용자 지정 타입 활용하기 (where문 활용) (0) | 2019.10.31 |