Unity에서 .NET 4.x 사용

Unity3D 2018. 12. 27. 16:01
반응형


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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Main : MonoBehaviour
{
    public Hero hero;
 
    // Start is called before the first frame update
    void Start()
    {
        
    }
 
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000))
            {
                if (hit.collider.tag == "Ground")
                {
                    this.hero.Move(hit.point);
                    //this.hero.MoveAsync(hit.point);
                }
            }
                
        }
    }
}
 
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
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
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using UnityEngine;
using static UnityEngine.Mathf;
 
public class Hero : MonoBehaviour
{
    public GameObject model;
    public int Health { get; set; } = 100;
    public string PlayerHealthUiText => $"Player Health: {Health}";
    private Transform trans;
    enum eDiff {
        None = -1, Easy, Medium, Hard
    }
 
    private void Awake()
    {
        this.trans = this.transform;
 
        Debug.Log($"PI: {RoundToInt(PI)}");
 
    }
 
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(nameof(eDiff.Easy));
 
        Debug.Log($"Player health: {Health}");    
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
 
    private int TaskDamage(int amount) => Health -= amount;
    private Coroutine routine;
 
    public void Move(Vector3 tpos)
    {
        if (routine != null) StopCoroutine(this.routine);
        this.routine = StartCoroutine(this.MoveImpl(tpos));
    }
 
    private IEnumerator MoveImpl(Vector3 tpos)
    {
        this.trans.LookAt(tpos);
        this.model.GetComponent<Animation>().Play("run@loop");
        while (true)
        {
            var speed = 1.0f;
            var dis = Vector3.Distance(tpos, this.transform.position);
            var dir = (tpos - this.transform.position).normalized;
 
            Debug.Log($"dis: {dis}");
 
            if (dis <= 0.2f)
            {
                break;
            }
            this.transform.position += speed * dir * Time.deltaTime;
            yield return null;
        }
        this.model.GetComponent<Animation>().Play("idle@loop");
        this.Wait();
    }
 
    public async void MoveAsync(Vector3 tpos)
    {
        //await this.MoveAsyncImpl(tpos);
        //await Task.Run(() => { });
        await Task.Run(() => this.MoveAsyncImpl(tpos));
 
        Debug.Log($"move complete!!");
    }
 
 
    /*
     * UnityException:  get_transform can only be called from the main thread.
                        Constructors and field initializers will be executed from the loading thread when loading a scene.
                        Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
     */
    private async Task MoveAsyncImpl(Vector3 tpos)
    {
        while (true)
        {
            var speed = 1.0f;
            var dis = Vector3.Distance(tpos, this.trans.position);
            var dir = (tpos - this.trans.position).normalized;
 
            Debug.Log($"dis: {dis}");
 
            if (dis <= 0.2f)
            {
                break;
            }
            this.trans.position += speed * dir * Time.deltaTime;
 
            await Task.Yield();
        }
    }
 
    
 
 
 
    private async void Wait()
    {
        Debug.Log("1");
 
        await this.WaitOneSecondAsync();
 
        Debug.Log("2");
    }
 
    private async Task WaitOneSecondAsync()
    {
        await Task.Delay(TimeSpan.FromSeconds(1));
        Debug.Log("Finished waiting");
    }
}
 
cs










Unity: Leveling up with Async / Await / Tasks


https://docs.unity3d.com/kr/2018.1/Manual/ScriptingRuntimeUpgrade.html


https://docs.unity3d.com/kr/2018.2/Manual/dotnetProfileSupport.html


https://docs.microsoft.com/ko-kr/visualstudio/cross-platform/unity-scripting-upgrade?view=vs-2017

반응형

'Unity3D' 카테고리의 다른 글

nodejs restful api server with unity  (0) 2019.01.04
nodejs restful api unity  (0) 2018.12.31
socket.io with unity  (1) 2018.12.24
GPGS 클라우드 저장/불러오기  (0) 2018.12.03
GPGS 리더보드  (0) 2018.11.29
: