'캐릭터 선택 UI 구현'에 해당되는 글 1건

  1. 2019.12.13 12.13 캐릭터 선택하기(랜덤포함)

12.13 캐릭터 선택하기(랜덤포함)

C#/실습 2019. 12. 13. 17:07

코드:

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Ingame : MonoBehaviour
{
    public UISelectedButton[] btns;
    public UISelectedButton randomBtn;
    public string[] btnNames;
    public Image image;
    private Coroutine changeC;
    private GameObject selectedObject;
    public Button selectBtn;
    public Text selectedText;
    public void Awake()
    {
 
        Debug.Log("Ingame: Awake");
 
    }
    private void Start()
    {
        selectBtn.onClick.AddListener(() =>
        {
            StopAllCoroutines();
            this.selectedText.text = this.selectedObject.transform.GetChild(0).GetComponent<Text>().text;
        });
        int i = 0;
        foreach(var btn in btns)
        {
            btn.Onselected = () =>
            {
                foreach(var btn2 in btns)
                {
                    btn2.gameObject.transform.GetChild(1).gameObject.SetActive(false);
                }
                StopAllCoroutines();
                btn.gameObject.transform.GetChild(1).gameObject.SetActive(true);
                this.selectedObject = btn.gameObject;
                this.image.sprite = btn.gameObject.GetComponent<Image>().sprite;
            };
            Debug.Log(btnNames[i]);
            btn.Init(btnNames[i]);
            i++;
        }
        randomBtn.Onselected = () =>
        {
            changeC = StartCoroutine(changeImage());
            foreach (var btn2 in btns)
            {
                btn2.gameObject.transform.GetChild(1).gameObject.SetActive(false);
                this.selectedObject = randomBtn.gameObject;
                randomBtn.gameObject.transform.GetChild(1).gameObject.SetActive(true);
 
            }
        };
        randomBtn.Init("random");
 
    }
    IEnumerator changeImage()
    {
        int i=0;
        while(true)
        {
            if(i >= 4)
            {
                i = 0;
            }
            this.image.sprite = btns[i].gameObject.GetComponent<Image>().sprite;
            this.selectedObject = btns[i].gameObject;
            yield return new WaitForSeconds(0.2f);
            i++;
        }
    }
    public void Init()
    {
        Debug.Log("게임 초기화");
 
    }
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class UISelectedButton : MonoBehaviour
{
    public Button btn;
    public System.Action Onselected;
    public Text text;
    void Start()
    {        
        this.btn.onClick.AddListener(() =>
        {
            this.Onselected();
        });
    }
    public void Init(string text)
    {
        this.gameObject.transform.GetChild(1).gameObject.SetActive(false);
        this.text.text = text;
    }
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public class App : MonoBehaviour
{
    private void Awake()
    {
    }
    void Start()
    {
        var oper = SceneManager.LoadSceneAsync("Ingame", LoadSceneMode.Additive);
        oper.completed += (ao) =>
        {
            Debug.Log("Ingame 씬 로드완료");
 
        };
    }
    
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
: