Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
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
Archives
Today
Total
관리 메뉴

행복한 개구리

Unity 21.04.20. UI구현 본문

Unity/수업내용

Unity 21.04.20. UI구현

HappyFrog 2021. 4. 20. 17:17
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;

public class Test : MonoBehaviour
{
    public GameObject[] arrEmpty;
    private List<UIItem> items;

    private GameObject prefab;
    public Transform empty;
    public SpriteAtlas atlas;
    void Start()
    {
        this.prefab = Resources.Load<GameObject>("UIItem");
        this.items = new List<UIItem>();
        this.AddItem(100);
        this.AddItem(200);

        this.GetItem(100);
    }

    public void AddItem(int id) {
        //100번에 해당하는 아이템의 스프라이트를 보여준다
        var parent = this.arrEmpty[this.items.Count];
        var go = Instantiate<GameObject>(this.prefab, parent.transform);
        var uiItem = go.GetComponent<UIItem>();
        this.items.Add(uiItem);
        string spName = "";
        if (id == 100) {
            spName = "inventory_item_clover";
        }
        else if(id == 200){
            spName = "inventory_item_hammer";
        }
        Sprite sp = this.atlas.GetSprite(spName);
        uiItem.Init(id, sp, 4);
    }

    private void GetItem(int id) {
        var uiItem = this.items.Find(x => x.id == id);
        if (uiItem != null) {
            for (int i = 0; i < this.arrEmpty.Length; i++) {
                var go = this.arrEmpty[i];
                if (go.transform.childCount > 0) {
                    var child = go.transform.GetChild(0);
                    if (child != null)
                    {
                        var target = child.GetComponent<UIItem>();
                        if (target.id == id)
                        {
                            Debug.Log(target.id);

                            Destroy(target.gameObject);
                            this.items.Remove(uiItem);

                            break;
                        }
                    }
                }
            }
        }
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIItem : MonoBehaviour
{
    public int id;
    public Image icon;
    public Text txtCount;
    public Button btn;

    //초기화 
    public void Init(int id, Sprite icon, int amount) {
        this.id = id;
        this.icon.sprite = icon;
        this.txtCount.text = amount.ToString();
    }
}

인벤토리에 아이템의 GameObject를 넣는 스크립트이다. (Data는 따로 넣어야함)

============================================================================

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Item : MonoBehaviour
{
    public int id = 1;
    public float speed = 5f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.Rotate(0, this.speed * Time.deltaTime, 0);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIIngame : MonoBehaviour
{
    public Button btnInventory;    
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIPopup : MonoBehaviour
{
    public Button btnClose;
    public GameObject inventory;
    
    public void Open()
    {
        this.inventory.gameObject.SetActive(true);
    }

    public void Close()
    {
        this.inventory.gameObject.SetActive(false);
    }
    
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    public UIIngame uiInGame;
    public UIPopup uiPopup;
    public Hero hero;
    void Start()
    {

        this.hero.OnGetItem = (id) =>
        {
            Debug.LogFormat("아이템 {0}을 획득했습니다.", id);
        };
        this.uiInGame.btnInventory.onClick.AddListener(() => this.uiPopup.Open());

        this.uiPopup.btnClose.onClick.AddListener(() => this.uiPopup.Close());
    }

    
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hero : MonoBehaviour
{
    public float speed = 2f;
    public Animation anime;

    public System.Action<int> OnGetItem;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(MoveForwardImpl());
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    IEnumerator MoveForwardImpl()
    {
        this.anime.Play("run@loop");
        while (true)
        {
            this.transform.Translate(Vector3.forward * this.speed * Time.deltaTime);
            yield return null;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Item")
        {
            var item = other.GetComponent<Item>();
            
            this.OnGetItem(item.id);
            Destroy(item.gameObject);
        }
        Debug.Log(other.name);
    }
}

Hero가 아이템을 먹는 처리를 하게 해주는 스크립트이다. 단, 먹었을 때, 필드에 있던 Item의 게임오브젝트는 사라지고 새로운 게임오브젝트가 Hero의 손에 쥐어지게 된다.

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

Unity UI 21.04.22.수업내용 UI애니메이션 연출  (0) 2021.04.22
Unity GUI 21.04.21.수업내용  (0) 2021.04.21
Unity UI 21.04.19.수업내용  (0) 2021.04.19
Unity 3D 21.04.16.수업내용  (0) 2021.04.16
Unity 3D 21.04.15.수업내용  (0) 2021.04.15