'Unity/수업내용'에 해당되는 글 3건

  1. 2019.05.28 Scroll View 동적생성
  2. 2019.05.24 UGUI를 이용해 Scroll View UI 만들기
  3. 2019.04.25 Raycast, Collider, Rigidbody, Joystick 이용 캐릭터 이동하기

Scroll View 동적생성

Unity/수업내용 2019. 5. 28. 09:48


UGUI를 이용, json 파일을 통해서 scroll view의 항목들을 동적으로 생성해보았다.



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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.U2D;
 
public class TestUGUI : MonoBehaviour
{
    public SpriteAtlas atlas;
    public RectTransform contents;
    public MissionListItem listItemPrefab;
    private DataManager dm;
 
    void Start()
    {
 
        this.dm = DataManager.GetInstance();
        dm.LoadData<mission_data>("Data/mission_data");
        dm.LoadData<reward_data>("Data/reward_data");
 
        var mission0 = dm.GetData<mission_data>(0);
        Debug.LogFormat(mission0.sprite_name);
 
        dm.GetCount();
        this.CreateListItem();
    }
 
    private void CreateListItem()
    {
        for (int i = 0; i<5; i++)
        {
            var data = this.dm.GetData<mission_data>(i);
            var listItemGo = Instantiate(this.listItemPrefab);
 
            var script = listItemGo.GetComponent<MissionListItem>();
            script.Init(data.id, this.dm.GetData<reward_data>(data.reward_id).sprite_name, data.sprite_name, data.reward_val, data.mission_name, data.goal_val,
                    this.dm.GetData<reward_data>(data.reward_id).hex_color);
        }
    }
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.U2D;
 
public class MissionListItem : MonoBehaviour
{
    public int id;
    public Image rewardIcon, Thumb;
    public Image[] Stars;
    public Text rewardText, MissionName;
    public GameObject bg;
    public Button claim;
 
    public void Init(int id, string rewardIcon, string Thumb, int rewardText, string MissionName, int goalValue, string hex_color)
    {
        this.Claim();
        this.gameObject.transform.SetParent(GameObject.Find("Contents").transform);
        var atlas = GameObject.FindObjectOfType<TestUGUI>().atlas;
        this.bg.SetActive(false);
        this.id = id;
        this.rewardIcon.sprite = atlas.GetSprite(rewardIcon);
        this.Thumb.sprite = atlas.GetSprite(Thumb);
        this.rewardText.text = rewardText.ToString();
        Color color;
        ColorUtility.TryParseHtmlString(hex_color, out color);
        this.rewardText.color = color;
        this.MissionName.text = string.Format(MissionName,goalValue.ToString());
    }
 
    private void Claim()
    {
        this.claim.onClick.AddListener(() =>
        {
            var dm = DataManager.GetInstance();
            Debug.LogFormat("id : {0}, reward : {1}, reward_Value : {2}"this.id, this.rewardIcon.name, this.rewardText.text);
        });
    }
}
 
cs




1
2
3
4
5
6
7
8
9
10
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class RawData
{
    public int id;
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class mission_data : RawData
{
    public string sprite_name;
    public string mission_name;
    public int goal_val;
    public int reward_id;
    public int reward_val;
}
 
cs

1
2
3
4
5
6
7
8
9
10
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class reward_data : RawData
{
    public string sprite_name;
    public string hex_color;
}
 
cs


:

UGUI를 이용해 Scroll View UI 만들기

Unity/수업내용 2019. 5. 24. 19:08



오늘은 NGUI, UGUI의 기본적인 것을 배우고 UGUI를 이용하여 Scroll View 기능이 있는 UI를 만들어 보았다.

만든건 Mission(Quest) UI인데, Claim은 버튼으로 총 3가지가 가능하게 만들었다. (완료가능, 완료, 완료불가능)

나름 재밌었다. UI 공부 좀 많이 해야할 것 같다.


Component : Content Size fitter, Vertical Layout Group, Mask, Scroll Rect


하면서 배운 것 : Sprite Atlas, Atlas, UnityEngine.U2D(namespace)

:

Raycast, Collider, Rigidbody, Joystick 이용 캐릭터 이동하기

Unity/수업내용 2019. 4. 25. 18:11

Joystick 이용하여 이동하기



Keyboard(W,A,S,D) 이용하여 이동하기



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.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class TestRaycastAndCollider : MonoBehaviour
{
    public Text txtMousePoint;
    public Text txtHitPoint;
    public GameObject heroGo;
    private Animator heroAnim;
    private Coroutine heroRoutine;
    private Coroutine joystickRoutine;
 
    public TestMyJoystick joystick;
 
    private void Awake()
    {
        Debug.Log("TestRaycastAndCollider Awake!");
    }
 
    // Start is called before the first frame update
    void Start()
    {
        this.heroAnim = heroGo.GetComponent<Animator>();
        Debug.Log("TestRaycastAndCollider Start!");
 
        this.joystick.OnPointerDownHandler = () =>
        {
            Debug.Log("다운핸들러");
            if (this.joystickRoutine != null)
            {
                StopCoroutine(this.joystickRoutine);
            }
            StartCoroutine(this.UpdateJoystickImpl());
        };
        this.joystick.OnPointerUpHandler = () =>
        {
            Debug.Log("업핸들러");
            StopAllCoroutines();
            this.heroAnim.Play("idle");
        };
    }
 
    private void UpdateJoystick()
    {
        Vector3 direction = Vector3.forward * this.joystick.Vertical + Vector3.right * joystick.Horizontal;
 
        direction = new Vector3(direction.x, 0, direction.z).normalized;
        if (direction != Vector3.zero)
        {
            this.heroAnim.Play("run");
            this.heroGo.transform.position += direction * 1.0f * Time.deltaTime;
 
            var rad = Mathf.Atan2(direction.x, direction.z);
            var dig = Mathf.Rad2Deg * rad;
            this.heroGo.transform.rotation = Quaternion.LookRotation(direction);
            //this.heroGo.transform.rotation = Quaternion.Euler(new Vector3(0, dig, 0));
 
        }
        else
        {
            this.heroAnim.Play("idle");
        }
    }
 
    private IEnumerator UpdateJoystickImpl()
    {
        while (true)
        {
            Vector3 direction = (Vector3.forward * this.joystick.Vertical + Vector3.right * joystick.Horizontal).normalized;
            if (direction != Vector3.zero)
            {
                this.heroGo.transform.rotation = Quaternion.LookRotation(direction);
                this.heroAnim.Play("run");
                this.heroGo.transform.position += direction * 2.0f * Time.deltaTime;
                yield return null;
            }
            else
            {
                this.heroAnim.Play("idle");
            }
        }
    }
 
    // Update is called once per frame
    private void UpdateKeyInput()
    {
        var keyY = Input.GetAxis("Vertical");
        var keyX = Input.GetAxis("Horizontal");
        Debug.LogFormat("keyY : {0}, keyX : {1}", keyY, keyX);
 
        var moveKey = new Vector3(keyX, 0, keyY).normalized;
        if (moveKey != Vector3.zero)
        {
            this.heroAnim.Play("run");
            this.heroGo.transform.position += moveKey * 1.0f * Time.deltaTime;
            var rad = Mathf.Atan2(keyX, keyY);
            var dig = Mathf.Rad2Deg * rad;
            this.heroGo.transform.rotation = Quaternion.Euler(new Vector3(0, dig, 0));
 
        }
        else
        {
            this.heroAnim.Play("idle");
        }
    }
 
    private void UpdateMouseInput()
    {
        if(Input.GetMouseButton(0))
        {
            //찍은 곳 스크린 좌표 표시
            this.txtMousePoint.text = Input.mousePosition.ToString();
 
            //찍은 곳 Ray생성
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
            //화면 Ray표시
            Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 5f);
 
            RaycastHit hitInfo;
            if(Physics.Raycast(ray.origin, ray.direction, out hitInfo, 1000))
            {
                Debug.LogFormat("hitInfo.point : {0}", hitInfo.point.ToString());
                this.txtHitPoint.text = hitInfo.point.ToString();
                this.Move(hitInfo.point);
            }
            else
            {
                this.txtHitPoint.text = "None";
            }
        }
    }
 
    public void Move(Vector3 targetCoordinate)
    {
        if(this.heroRoutine == null)
        {
            this.heroAnim.Play("run");
        }
        if(this.heroRoutine != null)
        {
            StopAllCoroutines();
        }
        this.heroRoutine = StartCoroutine(this.MoveHero(targetCoordinate, () =>
        {
            this.heroAnim.Play("idle");
        }));
    }
 
    private IEnumerator MoveHero(Vector3 targetCoordinate, System.Action OnCompleteMove)
    {
        this.heroGo.transform.LookAt(targetCoordinate);
        var dir = (targetCoordinate - this.heroGo.transform.position).normalized;
        var speed = dir * 1.0f * Time.deltaTime;
        while(true)
        {
            var distance = Vector3.Distance(targetCoordinate, this.heroGo.transform.position);
            this.heroGo.transform.position += speed;
            if(distance <= 0.01f)
            {
                this.heroGo.transform.position = targetCoordinate;
                break;
            }
            yield return null;
        }
        OnCompleteMove();
    }
}
 
cs


'Unity > 수업내용' 카테고리의 다른 글

Scroll View 동적생성  (0) 2019.05.28
UGUI를 이용해 Scroll View UI 만들기  (0) 2019.05.24
: