11.05 Unity 객체, 이벤트, prefeb파일

C#/실습 2019. 11. 5. 17:50

Unity:

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 prefabFiery = Resources.Load<GameObject>("Boximon Fiery");       //메모리상에 올린다.
        GameObject prefabCyclopes = Resources.Load<GameObject>("Boximon Cyclopes");
        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(400);
        modelFiery.transform.SetParent(monster.transform,false);  //클론화된 모델 객체가 monster의 transform을 상속받는다.
        modelCyclopes.transform.SetParent(hero.transform, false);
        monster.hp = 100;
        monster.damage = 5;
        monster.model = prefabFiery;
 
        hero.hp = 50;
        hero.damage = 15;
        hero.model = prefabCyclopes;
        hero.Attack(monster);
        monster.Attack(hero);
    }
 
    // 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)
    {
        Debug.LogFormat("{0}이(가) {1}에게 {2}만큼의 피해를 주었습니다.",this.name, target.name, this.damage);
        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
 

 

: