'C#/실습'에 해당되는 글 45건

  1. 2019.11.26 11.26 Hero와 Monster의 App에 의한 수동적 싸움 (delegate)
  2. 2019.11.13 11.13 unity 유닛 동적생성, 유닛 이동 및 사정거리 내 진입시 공격하기
  3. 2019.11.12 11.12 Unity 케릭터 공격모션 + 상대방 피격모션 구현하기
  4. 2019.11.11 Unity 11.11 케릭터 동적 생성하고 무기 장착및 교체하기
  5. 2019.11.08 11.08 Unity 동적 캐릭터 생성 후 사거리에 따라 공격기능 on/off
  6. 2019.11.07 11.07 Unity monster객체에 역직렬화 데이터와 모델 데이터 넣기.
  7. 2019.11.05 11.05 Unity 객체, 이벤트, prefeb파일 2
  8. 2019.10.31 10.31 인덱서를 이용해 LinkedList 클래스 만들기

11.26 Hero와 Monster의 App에 의한 수동적 싸움 (delegate)

C#/실습 2019. 11. 26. 17:40

코드:

 

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
 
namespace _11._26_study2
{
    class App
    {
        private delegate void Del(Monster monster, Action<Monster> action);
        Action<Monster> monsterHit;
        Action<Hero, Monster> attackAction;
        public App()
        {
            var hero = new Hero(10);
            var monster = new Monster(100);
            monster.onDie = () => { Console.WriteLine("몬스터가 죽었습니다. hp:{0}"monster.hp); };
            //monsterHit = (monster) =>
            //{
            //    Console.WriteLine("몬스터의 체력이 감소되었습니다. 몬스터의 현재체력: {0}", monster.hp);
            //    Thread.Sleep(300);
            //    if (monster.hp > 0)
            //    {
            //        hero.Attack(monster, monsterHit);
            //    }
            //    else
            //    {
            //        monster.onDie(); //얘죽음
            //    }
            //};
            //hero.Attack(monster, monsterHit);
            //**********************************************
            //모든 과정을 App을 통해 전달:
 
            //어떻게 맞는지 정의
            monster.GetDamageAc = (damage) =>
            {
               monster.GetDamage(damage);
                Console.WriteLine("몬스터가 피격당했습니다. 몬스터 현재체력: {0}"monster.hp); //얘 맞음
            };
 
            //어떻게 때리는지 정의
            hero.iDamaged = (damage) =>
            {
                Console.WriteLine("영웅이 때렸습니다. 데미지:{0}", damage);
                monster.GetDamageAc(damage); //너 맞으래
            };
            //얘네 서로 어떻게싸우는지 정의
            attackAction = (hero, monster) =>
            {
                if(monster.hp>0)
                {
                    Thread.Sleep(300);
                    hero.iDamaged(hero.damage); //얘가 때린다
                    AttackUntilDie(hero, monster, attackAction);
                }
                else
                {
                    monster.onDie();
                }
            };
            AttackUntilDie(hero, monster, attackAction);
            
        }
        public void AttackUntilDie(Hero hero, Monster monster, Action<Hero, Monster> attackAction)
        {
            attackAction(hero, monster); //얘네 서로 싸운다
        }
    }
}
 
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
using System;
using System.Collections.Generic;
using System.Text;
 
namespace _11._26_study2
{
    class Hero
    {
        public int damage;
        public Action<int> iDamaged;
        public Hero(int damage)
        {
            this.damage = damage;
        }
        //public void Attack(Monster monster, Action<Monster> attackComplete)
        //{
        //    //몬스터의 체력을 감소시킨다
        //    monster.hp -= 10;
        //    attackComplete(monster);
            
        //}
        public void BlindAttack()
        {
            iDamaged(this.damage);
        }
    }
}
 
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;
using System.Collections.Generic;
using System.Text;
 
namespace _11._26_study2
{
    class Monster
    {
        public int hp;
        public Action onDie;
        public Action<int> GetDamageAc;
        public Monster(int hp)
        {
            this.hp = hp;
        }
        public void MyHpIsZero()
        {
            if(this.hp <=0)
            {
                onDie(); //나 죽었소
            }
            
        }
        public void GetDamage(int damage)
        {
            this.hp -= damage;
            GetDamageAc(hp); // 나 맞았소
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

출력:

 

 

:

11.13 unity 유닛 동적생성, 유닛 이동 및 사정거리 내 진입시 공격하기

C#/실습 2019. 11. 13. 17:28

예제:

Hero Go 버튼을 누르기 전

누른 직후

사정 거리 내 도착 후

코드:

 

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
 
public enum eCharacter
{
    none,
    ch_01_01,
    ch_02_01,
    StoneMonster
}
public class App : MonoBehaviour
{
    public eCharacter eCharacterNames;
    public Button btnCreateStage;
    public Button btnCreateHero;
    public Button btnCreateMonster;
    public Button btnHeroGO;
    private Dictionary<string, GameObject> dicPrefabs;
    private Stage stage;
    private Hero hero;
    private Monster monster;
    private bool isHeroGO = false;
    private bool isAttack = false;
    // Start is called before the first frame update
    void Start()
    {
        int count = 0;
        this.Init();
        this.btnCreateStage.onClick.AddListener(() =>
        {
            this.CreateStage();
        });
 
        this.btnCreateHero.onClick.AddListener(() =>
        {
            if(count ==0)
            {
                this.CreateHero();
                count++;
            }
            else
            {
                Debug.Log("영웅이 이미 생성되었습니다.");
            }
            
        });
        this.btnCreateMonster.onClick.AddListener(() =>
        {
            this.CreateMonster();
        });
        this.btnHeroGO.onClick.AddListener(() =>
        {
            isHeroGO = true;
            
        });
 
    }
    public GameObject FindChild(Transform parent, string childName)
    {
        GameObject childGO = null;
        foreach (Transform child in parent)
        {
            if (child.name == childName)
            {
                childGO = child.gameObject;
                return childGO;
            }
            else
            {
                childGO = FindChild(child.transform, childName);
                if (childGO != null)
                {
                    return childGO;
                }
                continue;
            }
        }
        return childGO;
    }
    public void Init()
    {
        dicPrefabs = new Dictionary<string, GameObject>();
        var prefab1 = Resources.Load<GameObject>("Prefabs/ch_01_01");
        var prefab2 = Resources.Load<GameObject>("Prefabs/ch_02_01");
        var prefabStage = Resources.Load<GameObject>("Prefabs/Stage");
        var prefabMonster = Resources.Load<GameObject>("Prefabs/StoneMonster");
 
 
        dicPrefabs.Add("ch_01_01", prefab1);
        dicPrefabs.Add("ch_02_01", prefab2);
        dicPrefabs.Add("Stage", prefabStage);
        dicPrefabs.Add("StoneMonster", prefabMonster);
        
    }
    public void CreateMonster()
    {
        if (eCharacterNames == eCharacter.none)
        {
            Debug.Log("케릭터 이름을 선택해주세요.");
            return;
        }
 
        GameObject monsterGO = new GameObject("Monster");
        Monster monster = monsterGO.AddComponent<Monster>();
        var prefab = dicPrefabs[eCharacterNames.ToString()];
        var model = Instantiate(prefab);
 
        monster.SetModel(model);
        monsterGO.transform.position = FindChild(stage.transform, "MonsterSpawnP").transform.position;
        monsterGO.transform.LookAt(FindChild(stage.transform, "HeroSpawnP").transform);
 
        this.monster = monster;
    }
    public void CreateStage()
    {
        GameObject stageGO = new GameObject("Stage");
        Stage stage = stageGO.AddComponent<Stage>();
        var prefabs = dicPrefabs["Stage"];
        var model = Instantiate(prefabs);
        model.transform.SetParent(stageGO.transform);
        model.transform.localPosition = Vector3.zero;
        stage.model = model;
        this.stage = stage;
 
    }
    public void CreateHero()
    {
        if(eCharacterNames == eCharacter.none)
        {
            Debug.Log("케릭터 이름을 선택해주세요.");
            return;
        }
 
        GameObject heroGO = new GameObject("Hero");
        Hero hero = heroGO.AddComponent<Hero>();
        var prefab = dicPrefabs[eCharacterNames.ToString()];
        var model = Instantiate(prefab);        
 
        hero.SetModel(model);
        heroGO.transform.position = FindChild(stage.transform, "HeroSpawnP").transform.position;
        heroGO.transform.LookAt(FindChild(stage.transform, "MonsterSpawnP").transform);
 
        hero.attack_range = 1f;
 
        this.hero = hero;
 
    }
 
    // Update is called once per frame
    void Update()
    {
        
        if(isHeroGO == true)
        {
            this.hero.model.GetComponent<Animation>().Play("run@loop");
            this.hero.transform.position = Vector3.Lerp(this.hero.transform.position, this.monster.transform.position, 0.05f);
            this.hero.transform.LookAt(this.monster.transform);
 
            if ((Vector3.Distance(this.hero.transform.position, this.monster.transform.position) <= this.hero.attack_range))
            {
                isHeroGO = false;
                isAttack = true;
            }
        }
        if(isAttack)
        {
            this.hero.model.GetComponent<Animation>().Play("attack_sword_01");
 
        }
 
    }
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Stage : MonoBehaviour
{
    public GameObject model;
    // Start is called before the first frame update
    void Start()
    {
        
    }
 
    // 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Hero : MonoBehaviour
{
    public GameObject model;
    public float attack_range;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void SetModel(GameObject model)
    {
        if (this.model != null)
        {
            Destroy(this.model);
        }
        this.model = model;
        this.model.transform.SetParent(this.gameObject.transform, false);
        this.model.transform.localPosition = Vector3.zero;
    }
 
 
 
    // 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 Monster : MonoBehaviour
{
    public GameObject model;
 
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void SetModel(GameObject model)
    {
        if (this.model != null)
        {
            Destroy(this.model);
        }
        this.model = model;
        this.model.transform.SetParent(this.gameObject.transform, false);
        this.model.transform.localPosition = Vector3.zero;
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

:

11.12 Unity 케릭터 공격모션 + 상대방 피격모션 구현하기

C#/실습 2019. 11. 12. 20:00

예제:

 

코드:

 

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class App : MonoBehaviour
{
    public Animation Anim1;
    public Animation Anim2;
    public Button btn;
    private float elapsedTime1;  //경과 시간
    private float elapsedTime2;
    private bool isAttackAnimationPlay1 = false;
    private bool isAttackAnimationPlay2 = false;
    private bool isAttackAnimationPlay3 = false;
    private bool isAttacked = false;
    private bool isHero2Alive = true;
    private Hero hero1;
    private Hero hero2;
    private Sword sword;
    // Start is called before the first frame update
    void Start()
    {
        //버튼을 누르면 공격 애니메이션을 실행한다
        this.btn.onClick.AddListener(() =>
        {
            Debug.Log("버튼을 눌렀습니다.");
            this.Anim1.Play("attack_sword_01");
            isAttackAnimationPlay1 = true;
 
        });
        //AnimationState state1 = this.Anim1["attack_sword_01"];
        //foreach (AnimationState state in Anim1)
        //{
        //    if (state.name == "attack_sword_01")
        //    {
        //        Debug.Log(state);
        //    }
        //}
        //this.Anim.Play("attack_sword_01");
 
        //model 불러오기
        var heroPrefab = Resources.Load<GameObject>("Prefabs/ch_01_01");
        var hero1Model = Instantiate(heroPrefab);
        var hero2Model = Instantiate(heroPrefab);
 
        GameObject heroGO1 = new GameObject("Hero1");
        hero1 = heroGO1.AddComponent<Hero>();
        hero1Model.transform.SetParent(heroGO1.transform, false);
        hero1Model.transform.localPosition = Vector3.zero;
        hero1Model.transform.localRotation = new Quaternion(0000);
        hero1.hp = 200;
        hero1.model = hero1Model;
        heroGO1.transform.position = Vector3.zero;
 
        GameObject heroGO2 = new GameObject("Hero2");
        hero2 = heroGO2.AddComponent<Hero>();
        hero2Model.transform.SetParent(heroGO2.transform, false);
        hero2Model.transform.localPosition = Vector3.zero;
        hero2Model.transform.localRotation = new Quaternion(0000);
        hero2.hp = 200;
        hero2.model = hero2Model;
        heroGO2.transform.position = new Vector3(100);
 
 
 
        GameObject swordGO = new GameObject("Sword");
        sword = swordGO.AddComponent<Sword>();
        sword.damage = 20;
 
        Anim1 = hero1.model.GetComponent<Animation>();
        Anim2 = hero2.model.GetComponent<Animation>();
    }
 
    // Update is called once per frame
    //매 프레임 마다 호출됨(각 프레임 경과 타임은 다르다)
    void Update()
    {
        if (isAttackAnimationPlay1 == true)
        {
            this.elapsedTime1 += Time.deltaTime;
            //0.2665초때 타격이라고 출력하기
            if (this.elapsedTime1 >= 0.2665f && isAttacked == false)
            {
                isAttacked = true;
                this.elapsedTime2 = 0;
                Debug.LogFormat("타격1! 시간: {0}"this.elapsedTime1);
                this.hero2.GetHit(sword.damage);
 
            }
            if (this.elapsedTime1 >= 0.733f)
            {
                this.isAttackAnimationPlay1 = false;
                this.Anim1.Play("attack_sword_02");
                this.elapsedTime1 = 0;
                this.isAttacked = false;
                this.isAttackAnimationPlay2 = true;
            }
        }
        if (isAttackAnimationPlay2 == true)
        {
            this.elapsedTime1 += Time.deltaTime;
            //0.5333초때 타격이라고 출력하기
            if (this.elapsedTime1 >= 0.5333f && isAttacked == false)
            {
                isAttacked = true;
                this.elapsedTime2 = 0;
                Debug.LogFormat("타격2! 시간: {0}"this.elapsedTime1);
                this.hero2.GetHit(sword.damage);
 
            }
            if (this.elapsedTime1 >= 0.8f)
            {
                this.isAttackAnimationPlay2 = false;
                this.Anim1.Play("attack_sword_03");
                this.elapsedTime1 = 0;
 
                this.isAttacked = false;
                this.isAttackAnimationPlay3 = true;
            }
        }
        if (isAttackAnimationPlay3 == true)
        {
            this.elapsedTime1 += Time.deltaTime;
            //0.5333초때 타격이라고 출력하기
            if (this.elapsedTime1 >= 0.5333f && isAttacked == false)
            {
                isAttacked = true;
                this.elapsedTime2 = 0;
                Debug.LogFormat("타격3! 시간: {0}"this.elapsedTime1);
                this.hero2.GetHit(sword.damage);
 
            }
            if (this.elapsedTime1 >= 0.8f)
            {
                this.isAttackAnimationPlay3 = false;
                this.Anim1.Play("idle@loop");
                this.elapsedTime1 = 0;
 
                this.isAttacked = false;
            }
        }
        this.elapsedTime2 += Time.deltaTime;
        if (isAttacked == true && isHero2Alive == true)
        {
            this.Anim2.Play("damage");
        }
        if(hero2.hp <=0 && isHero2Alive == true)
        {
            this.Anim2.Play("die");
            isHero2Alive = false;
        }
        if (this.elapsedTime2 >= 0.8f && isHero2Alive == true)
        {
            this.elapsedTime2 = 0;
            this.Anim2.Play("idle@loop");
        }
        if(hero2.hp >0)
        {
            isHero2Alive = true;
        }
    }
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Hero : MonoBehaviour
{
    public int hp;
    public GameObject model;
    // Start is called before the first frame update
    void Start()
    {
        
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
    public void GetHit(int damage)
    {
        if(this.hp - damage <= 0)
        {
            this.hp = 0;
        }
        else
        {
            this.hp -= damage;
        }
    }
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Sword : MonoBehaviour
{
    public int damage;
    // Start is called before the first frame update
    void Start()
    {
        
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

:

Unity 11.11 케릭터 동적 생성하고 무기 장착및 교체하기

C#/실습 2019. 11. 11. 17:13

예제:

hero가 처음에는 무기를 갖고 있다가 monster에게 넘겨준다(코드상으로만)

코드:

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class App2 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GameObject heroGO = new GameObject("Hero");
        heroGO.transform.position = new Vector3(-200);
        Hero hero = heroGO.AddComponent<Hero>();
        CharacterInfo info = new CharacterInfo() { id = 300, hp = 100 };
        hero.info = info;
 
        GameObject monsterGO = new GameObject("Monster");
        Monster monster = monsterGO.AddComponent<Monster>();
        monster.info = info;
 
        GameObject heroPrefab = Resources.Load<GameObject>("Prefabs/ch_02_01");
        GameObject heroModel = Instantiate(heroPrefab);
        heroModel.transform.SetParent(heroGO.transform,false);
        hero.model = heroModel;
        GameObject dummyhandGOHero = FindChild(heroGO.transform,"DummyRHand");
 
        GameObject monsterPrefab = Resources.Load<GameObject>("Prefabs/ch_03_01");
        GameObject monsterModel = Instantiate(monsterPrefab);
        monsterModel.transform.SetParent(monsterGO.transform, false);
        monster.model = monsterModel;
        GameObject dummyhandGOMonster = FindChild(monsterGO.transform, "DummyRHand");
 
        GameObject swordPrefab = Resources.Load<GameObject>("Prefabs/ch_sword_01");
        GameObject swordModel = Instantiate(swordPrefab);
        
        swordModel.transform.SetParent(dummyhandGOHero.transform, false);
        swordModel.transform.localPosition = Vector3.zero;
 
        swordModel.transform.SetParent(dummyhandGOMonster.transform, false);
 
 
        GameObject swordGO = new GameObject("Sword");
        Sword sword = swordGO.AddComponent<Sword>();
        sword.damage = 10;
        sword.attack_range = 1.5f;
        hero.sword = sword;
 
    }
    public GameObject FindChild(Transform parent, string childName)
    {
        GameObject childGO = null;
        foreach (Transform child in parent)
        {
            if (child.name == childName)
            {
                childGO = child.gameObject;
                return childGO;
            }
            else
            {
                childGO = FindChild(child.transform, childName);
                if(childGO != null)
                {
                    return childGO;
                }
                continue;
            }
        }
        return childGO;
    }   
    // 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class CharacterInfo
{
    public int id;
    public int hp;
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Character : MonoBehaviour
{
    public CharacterInfo info;
    public GameObject model;
    public Sword sword;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void SetModel(GameObject model)
    {
        this.model = model;
        var oldmodel = this.transform.Find("model");
        if(oldmodel != null)
        {
            Destroy(oldmodel.gameObject);
        }
        model.transform.SetParent(this.transform, false);
        model.transform.localPosition = Vector3.zero;
    }
    public void SetInfo(CharacterInfo info)
    {
        this.info = info;
    }
    // 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
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
 
1
2
3
4
5
6
7
8
9
10
using System;
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Sword : MonoBehaviour
{
    public int damage;
    public float attack_range;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

MS API 에는 특정 부모 한정 오브젝트의 이름을 찾기가 없어서 새로운 메소드를 만들어 그것을 구현했다.

:

11.08 Unity 동적 캐릭터 생성 후 사거리에 따라 공격기능 on/off

C#/실습 2019. 11. 8. 16:57

예제:

코드:

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class App : MonoBehaviour
{
    private Dictionary<int, CharacterData> CharacterDatas;
    public Ingame ingame;
    // Start is called before the first frame update
    void Start()
    {
        //json 파일 로드
 
        string json = Resources.Load<TextAsset>("Datas/character_data2").text;
        CharacterData[] arrCharacterDatas = JsonConvert.DeserializeObject<CharacterData[]>(json);
        CharacterDatas = new Dictionary<int, CharacterData>();
        foreach(var data in arrCharacterDatas)
        {
            this.CharacterDatas.Add(data.id, data);
        }
 
        // prefab 파일 로드
        GameObject prefabFiery = this.GetPrefab(this.CharacterDatas[200].prefab_name);
        GameObject prefabCyclopes = this.GetPrefab(this.CharacterDatas[100].prefab_name);
        //ingameparams 구조체에 담기
        Ingame.IngameParams param = new Ingame.IngameParams();
        param.CharacterDatas = this.CharacterDatas;
        param.heroPrefab = prefabFiery;
        param.monsterPrefab = prefabCyclopes;
        //ingame에 전달
        GameObject ingameGO = new GameObject("Ingame");
        this.ingame = ingameGO.AddComponent<Ingame>();
        this.ingame.Init(param);
 
    }
    public GameObject GetPrefab(string prefabName)
    {
        string path = string.Format("Prefabs/{0}", prefabName);
        var prefab = Resources.Load<GameObject>(path);
        return prefab;
    }
    // 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
53
54
55
56
57
58
59
60
61
62
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Ingame : MonoBehaviour
{
    public class IngameParams
    {
        public Dictionary<int, CharacterData> CharacterDatas;
        public GameObject monsterPrefab;
        public GameObject heroPrefab;
    }
    IngameParams param;
    Hero hero;
    Monster monster;
 
    // Start is called before the first frame update
    void Start()
    {
        
    }
 
    // Update is called once per frame
    void Update()
    {
        //사거리 재고 공격하기
        float dis = Vector3.Distance(monster.transform.position, hero.transform.position);
        float heroAttackrange = this.param.CharacterDatas[hero.Info.id].attack_range;
        if (heroAttackrange >= dis)
        {
            hero.Attack(monster.gameObject);
        }
        else
        {
            Debug.LogFormat("대상을 공격할 수 없습니다. dis: {0} range: {1}", dis, heroAttackrange);
        }
    }
    public void Init(IngameParams param)
    {
        this.param = param;
        this.monster = (Monster)this.GetCharacterGO<Monster>("Monster"this.param.monsterPrefab);
        CharacterInfo monsterInfo = new CharacterInfo(this.param.CharacterDatas[100].hp, this.param.CharacterDatas[100].damage, 100);
 
        this.hero = (Hero)this.GetCharacterGO<Hero>("Hero"this.param.heroPrefab);
        CharacterInfo heroInfo = new CharacterInfo(this.param.CharacterDatas[200].hp, this.param.CharacterDatas[200].damage, 200);
        hero.Init(heroInfo);
 
        
 
    }
    public Character GetCharacterGO<T>(string name, GameObject prefab) where T : Character
    {
        GameObject GO = new GameObject(name);
        GameObject modelGO = Instantiate(prefab);
        modelGO.transform.SetParent(GO.transform, false);
        modelGO.transform.localPosition = Vector3.zero;
        return GO.AddComponent<T>();
    }
 
}
 
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
{
    private CharacterInfo info;
    public CharacterInfo Info
    {
        get { return this.info; }
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void Init(CharacterInfo info)
    {
        this.info = info;
    }
    public void Attack(GameObject target)
    {
        Debug.LogFormat("{0}을 공격했습니다."target.name);
    }
    // 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class CharacterData 
{
    public int id;
    public string name;
    public int hp;
    public int damage;
    public float attack_range;
    public string prefab_name;
 
}
 
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 CharacterInfo
{
    public int id;
    public int hp;
    public int damage;
    public CharacterInfo(int hp, int damage, int id)
    {
        this.hp = hp;
        this.damage = damage;
        this.id = id;
    }
}
 
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
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
 
1
2
3
4
5
6
7
8
9
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
 
:

11.07 Unity monster객체에 역직렬화 데이터와 모델 데이터 넣기.

C#/실습 2019. 11. 7. 17:51

예제:

코드:

 

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
{
    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
53
54
55
56
57
58
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Ingame : MonoBehaviour
{
    Chicken chicken;
    Monster monster;
    Hero hero;
    // Start is called before the first frame update
    void Start()
    {
        
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
 
    internal void StartGame()
    {
        GameObject heroGO = new GameObject("Hero");
        GameObject monsterGO = new GameObject("Monster");
        GameObject chickenGO = new GameObject("Chicken");
 
        this.chicken = chickenGO.AddComponent<Chicken>();
        this.monster = monsterGO.AddComponent<Monster>();
        this.hero = heroGO.AddComponent<Hero>();
 
        //info 받아오기
        TextAsset asset = Resources.Load<TextAsset>("Data/character_data2"); //프로젝트에 존재하는 모든 파일을 어셋이라고 한다.
        CharacterData[] characterDatas = JsonConvert.DeserializeObject<CharacterData[]>(asset.text);
        Dictionary<int, CharacterData> dicCharacterDatas = new Dictionary<int, CharacterData>();
        foreach (var data in characterDatas)
        {
        }
        var characterdata = dicCharacterDatas[100];
 
        CharacterInfo characterInfo = new CharacterInfo(characterdata.id, characterdata.name, characterdata.hp, characterdata.damage);
 
        //model 가져오기
        string path = string.Format("Prefabs/{0}", characterdata.prefab_name);
        GameObject prefabCyclopes = Resources.Load<GameObject>(path);
        GameObject modelCyclopes = Instantiate(prefabCyclopes);
        modelCyclopes.transform.SetParent(heroGO.transform, false);
        modelCyclopes.transform.localPosition = Vector3.zero;
 
        Debug.LogFormat("이름: {0} 체력: {1}  공격력: {2}"hero.info.name, hero.info.hp, hero.info.damage);
 
    }
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Hero : MonoBehaviour
{
    public CharacterInfo info;
    private Vector3 Pos;
    GameObject model;
 
    public void Init(CharacterInfo info, GameObject model, Vector3 pos)
    {
        this.info = info;
        this.Pos = pos;
        this.model = model;
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }
 
    // 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class CharacterData 
{
    public int id;
    public string name;
    public string prefab_name;
    public int hp;
    public int damage;
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class CharacterInfo
{
    private int id;
    public string name;
    public int hp;
    public int damage;
    public int ID { get { return this.id; } }
    public CharacterInfo(int id, string name, int hp, int damage)
    {
        this.id = id;
        this.name = name;
        this.hp = hp;
        this.damage = damage;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

json 파일:
[
  {
    "id": 100,
    "name": "Cyclopes",
    "prefab_name": "Boximon Cyclopes",
    "hp": 100,
    "damage": 3
  },
  {
    "id": 200,
    "name": "Fiery",
    "prefab_name": "Boximon Fiery",
    "hp": 95,
    "damage": 4
  }
]

:

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
 

 

:

10.31 인덱서를 이용해 LinkedList 클래스 만들기

C#/실습 2019. 10. 31. 16:22

예제:

코드:

 

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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._31_step2
{
    class App
    {
        public App()
        {
            LinkedList list = new LinkedList(4);
            Item item1 = new Item(1"단검");
            list.Add(item1);
            list.Add(new Item(2"장검"));
            list.Add(new Item(3"활"));
            Console.WriteLine("리스트 요소 개수: {0}"list.Count);
            list.PrintByIndex();
            list.Remove(item1);
            Console.WriteLine("리스트 요소 개수: {0}"list.Count);
            list.Add(new Item(3"활"));
            Console.WriteLine("리스트 요소 개수: {0}"list.Count);
            list.PrintByIndex();
            list.PrintByNode();
 
        }
    }
}
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._31_step2
{
    public class Item
    {
        public int Id { get; private set; }
        public string Name { get; private set; }
        public Item(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
}
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._31_step2
{
    public class LinkedList
    {
        public Node[] nodes;
        public Node first;
        public int count = 0;
        public int Count
        {
            get
            {
                return count;
            }
        }
        public LinkedList(int capacity)
        {
            Console.WriteLine("LinkedList 생성자가 호출되었습니다.");
            nodes = new Node[capacity];
        }
        public void PrintByNode()
        {
            Console.WriteLine("노드 순으로 출력:");
 
            if(first != null)
            {
                Node node = first;
                while (node != null)
                {
                    Console.WriteLine("ID: {0} Name: {1}"node.item.Id, node.item.Name);
                    node = node.next;
                }
 
            }
        }
        public void PrintByIndex()
        {
            Console.WriteLine("인덱스 순으로 출력:");
 
            foreach (Node node in nodes)
            {
                if (node != null)
                {
                    Console.WriteLine("ID: {0} Name: {1}"node.item.Id, node.item.Name);
                }
            }
        }
        public int CheckNumofNull()
        {
            int nullCount = nodes.Length;
            for (int i = 0; i < nodes.Length; i++)
            {
                if (nodes[i] != null)
                    nullCount--;
            }
            return nullCount;
        }
        private Node FindNode(Item item)
        {
            for (int i = 0; i < nodes.Length; i++)
            {
                if (nodes[i].item == item)
                {
                    return nodes[i];
                }
            }
            return null;
        }
        public void Remove(Item item)
        {
            //1.아이템을 담고있는 해당 노드를 찾는다
            var node = FindNode(item);//nodes[i]
            if (node == null)
            {
                Console.WriteLine("해당 아이템을 찾을 수 없습니다.");
                return;
            }
            //2.1 해당 노드가 first 인 경우 자신의 참조를 끊고 first 값을 자신의 next 값으로 바꿔준다.
            //2.2 해당 노드를 next값으로 갖고있는 노드를 찾고 그걸 해당노드의 next값으로 바꿔준다.
            if (node == first)
            {
                first = node.next;
                node.next = null;
                Console.WriteLine("{0}을 삭제했습니다."node.item.Name);
                count--;
            }
            else
            {
                for (int i = 0; i < nodes.Length; i++)
                {
                    if (nodes[i].next == node)
                    {
                        nodes[i].next = node.next;
                        count--;
                        Console.WriteLine("{0}을 삭제했습니다."node.item.Name);
                        break;
                    }
                }
            }
 
            //3. 해당 노드를 참조하고있는 배열의 인덱스를 null로 바꾼다.
            for (int i = 0; i < nodes.Length; i++)
            {
                if (nodes[i] == node)
                {
                    nodes[i] = null;
                    break;
                }
            }
            //4. 해당 노드를 지운다. --> 해당 노드가 아무것도 참조받지 않고 참조 하지 않게한다.
            node.next = null;
 
        }
        public Node FindLastNode(Node node)
        {
            //배열이랑 상관이 없다.
            if (node.next == null)
            {
                return node;
            }
            return FindLastNode(node.next);
        }
        public void Add(Item item)
        {
            if (CheckNumofNull() == 0)
            {
                Console.WriteLine("배열에 공간이 없습니다.");
                return;
            }
            Node node = new Node(item);
            if (first == null// 첫번째 노드가 없을때
            {
                first = node;
                count++;
                for (int i = 0; i < nodes.Length; i++// 배열의 첫번째 빈공간에 추가
                {
                    if (nodes[i] == null)
                    {
                        nodes[i] = node;
                        Console.WriteLine("새로운 아이템이 인덱싱 되어 추가되었습니다.");
                        Console.WriteLine("ID: {0} Name: {1}"node.item.Id, node.item.Name);
                        break;
                    }
                }
            }
            else // 첫번째 노드가 있을때.
            {
                Node lastNode = FindLastNode(this.first);
                lastNode.next = node; // 마지막 노드 뒤에 추가
                count++;
                for (int i = 0; i < nodes.Length; i++// 배열의 첫번째 빈공간에 추가
                {
                    if (nodes[i] == null)
                    {
                        nodes[i] = node;
                        Console.WriteLine("새로운 아이템이 인덱싱 되어 추가되었습니다.");
                        Console.WriteLine("ID: {0} Name: {1}"node.item.Id, node.item.Name);
 
                        break;
                    }
                }
            }
        }
    }
}
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._31_step2
{
    public class Node
    {
        public Node next;
        public Item item;
        public Node(Item item)
        {
            this.item = item;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

: