2018.04.03 수업내용 (3D 케릭터 이동과 애니메이션)

카테고리 없음 2018. 5. 15. 21:35
반응형

수업 자료실의 Grunt로 해보실분을 위한...케릭터 이동과 애니메이션 관련 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
    public Animator gruntAnimator;
    private float attackRange = 2.5f;
    public Animator target;
    private Dictionary<stringfloat> dicAnimLength;
    private int targetHp = 5;
 
    // Use this for initialization
    IEnumerator Start () {
        this.dicAnimLength = new Dictionary<stringfloat>();
        this.gruntAnimator.Play("Attack01");
        var clips = this.gruntAnimator.runtimeAnimatorController.animationClips;
        foreach (var clip in clips)
        {
            Debug.Log(clip.name);
            dicAnimLength.Add(clip.name, clip.length);
        }
            
 
        this.gruntAnimator.Play("Walk");
        this.gruntAnimator.transform.LookAt(this.target.transform);
        this.target.transform.LookAt(this.gruntAnimator.transform);
        this.target.Play("Idle");
 
        while (true)
        {
            var step = 1 * Time.deltaTime;
            this.gruntAnimator.transform.position = Vector3.MoveTowards(this.gruntAnimator.transform.position, target.transform.position, step);
            yield return null;
 
            var dis = Vector3.Distance(this.target.transform.position, this.gruntAnimator.transform.position);
            Debug.Log(dis);
            if (dis <= attackRange) {
                break;
            }
        }
 
        
        while (true)
        {
            yield return null;
 
            // 타격  frame : 21
            // total frame : 40
            // total time (sec) : 1.333
            // 타격 time (sec) : 40: 21 = 1.333: x
            // x = 0.699
 
            this.gruntAnimator.Play("Attack01");
 
            yield return new WaitForSeconds(0.699f);
 
            if (targetHp <= 0)
            {
                this.target.Play("Die");
                this.gruntAnimator.Play("Victory");
                break;
            }
            else
            {
                this.targetHp -= 1;
                this.target.Play("GetHit");
                yield return new WaitForSeconds(this.dicAnimLength["GetHit"]);
                this.target.Play("Idle");
            }
 
            //yield return new WaitForSeconds(this.dicAnimLength["Attack01"] - 0.699f);
 
            this.gruntAnimator.Play("Idle");
            
        }
 
 
 
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}
 




cs


NEXT..

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
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using UnityEngine;
using Newtonsoft.Json;
 
public class TestReadFile : MonoBehaviour {
 
    private Dictionary<int, HeroData> dicHeroData;
    private Character hero;
 
    // Use this for initialization
    void Start () {
 
        this.dicHeroData = new Dictionary<int, HeroData>();
 
        var fullPath = System.IO.Path.GetFullPath(".");
        Debug.LogFormat("fullPath: {0}", fullPath);
 
        var dataPath = Application.dataPath;
        Debug.LogFormat("dataPath: {0}", dataPath);
 
        var persistentDataPath = Application.persistentDataPath;
        Debug.LogFormat("persistentDataPath: {0}", persistentDataPath);
 
        var streamingAssetPath = Application.streamingAssetsPath;
        Debug.LogFormat("streamingAssetPath: {0}", streamingAssetPath);
 
        var temporaryCachePath = Application.temporaryCachePath;
        Debug.LogFormat("temporaryCachePath: {0}", temporaryCachePath);
 
        //기본 데이터 로드 
        var asset = Resources.Load("hero_data"as TextAsset;
        var heroDataList = JsonConvert.DeserializeObject<List<HeroData>>(asset.text);
        foreach (var data in heroDataList)
            this.dicHeroData.Add(data.id, data);
 
 
        //신규 유저 
        //파일이 있는지 없는지 검사 
        var isNewbie = File.Exists(string.Format("{0}/Data/hero_info.json", Application.persistentDataPath));
 
        if (isNewbie)
        {
            //기존유저 
            StreamReader reader = new StreamReader(Application.persistentDataPath + "/Data/hero_info.json");
            var str = reader.ReadToEnd();
            Debug.Log(str);
            reader.Close();
 
            var heroInfo = JsonConvert.DeserializeObject<HeroInfo>(str);
 
            //모델 생성 
            var obj = Resources.Load(this.dicHeroData[100].assetName);
            var prefab = Instantiate(obj) as GameObject;
 
            //캐릭터 생성 
            var monoCharacter = prefab.AddComponent<MonoCharacter>();   //껍대기 
 
            //Character클래스 생성 
            this.hero = new Character(heroInfo, monoCharacter);
 
            //hero가 실체와 되었음 
 
            Debug.Log("영웅의 이름: " + this.hero.info.name);
 
        }
        else
        {
            //케릭터 데이터 생성 
            var heroData = this.dicHeroData[100];
 
            //케릭터 모델 생성 
            var obj = Resources.Load(heroData.assetName);
            var prefab = Instantiate(obj) as GameObject;
 
            //캐릭터 생성 
            var monoCharacter = prefab.AddComponent<MonoCharacter>();   //껍대기 
 
            //Character클래스 생성 
            this.hero = new Character(heroData, monoCharacter);
 
            //hero가 실체와 되었음 
 
            Debug.Log("영웅의 이름: " + this.hero.info.name);
 
            //닉네임 변경권을 구매 했음 
 
            this.hero.info.name = "홍길또오오오옹";
 
            //게임을 종료했다고 침 
        }
 
 
 
 
 
 
 
 
        /*
        var heroDataList = JsonConvert.DeserializeObject<List<HeroData>>(str);
        heroDataList[0].name = "홍길똥";
        var saveData = JsonConvert.SerializeObject(heroDataList);
        StreamWriter writer = new StreamWriter(Application.dataPath + "/Data/saved_hero_data.json", true);
        writer.WriteLine(saveData);
        writer.Close();
        */
 
    }
 
    // Update is called once per frame
    void Update () {
        
    }
    private void OnApplicationPause(bool pause)
    {
        
    }
 
    private void OnApplicationQuit()
    {
        Debug.Log("<color=red>OnApplicationQuit</color>");
 
 
        //데이터 저장
 
        var path = string.Format("{0}/Data/hero_info.json", Application.persistentDataPath);
        var dirPath = string.Format("{0}/Data", Application.persistentDataPath);
        
        var existsDirectory = Directory.Exists(dirPath);
        if (!existsDirectory)
        {
            //디렉토리 생성 
            Directory.CreateDirectory(dirPath);
        }
 
        var serializeData = JsonConvert.SerializeObject(this.hero.info);
 
        StreamWriter writer = new StreamWriter(path, true);
        writer.WriteLine(serializeData);
        writer.Close();
 
        Debug.Log("저장되었습니다. " + path);
    }
}
 
cs


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 Character {
 
    public HeroInfo info;      //데이터 
    private MonoCharacter model;    //모델 
 
    public Character(HeroData data, MonoCharacter model)
    {
        this.info = new HeroInfo();
        this.info.name = data.name;
        this.info.hp = data.hp;
        this.info.damage = data.damage;
        this.model = model;
 
        this.model.SetModelName(this.info.name);
    }
 
    public Character(HeroInfo info, MonoCharacter model)
    {
        this.info = info;
        this.model = model;
 
        this.model.SetModelName(this.info.name);
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
using System;
 
[Serializable]
public class HeroData  {
    public int id;
    public string name;
    public int hp;
    public int damage;
    public string assetName;
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
 
[Serializable]
public class HeroInfo
{
    public string name;
    public int hp;
    public int damage;
}
 
cs
.





반응형
: