C#/실습
11.08 Unity 동적 캐릭터 생성 후 사거리에 따라 공격기능 on/off
미고렝
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;
using Newtonsoft.Json;
public class App : MonoBehaviour
{
private Dictionary<int, CharacterData> CharacterDatas;
public Ingame ingame;
// Start is called before the first frame update
void Start()
{
//json 파일 로드
CharacterData[] arrCharacterDatas = JsonConvert.DeserializeObject<CharacterData[]>(json);
CharacterDatas = new Dictionary<int, CharacterData>();
foreach(var data in arrCharacterDatas)
{
}
// 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>();
}
public GameObject GetPrefab(string prefabName)
{
string path = string.Format("Prefabs/{0}", prefabName);
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);
if (heroAttackrange >= dis)
{
}
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);
}
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
|