2020년 8월 12일 수요일

[Unity] ManualInventory , ManualSlot , ManualRollItem

 using System.Collections;

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

public class ManualInventory : MonoBehaviour
{


    public static ManualInventory instance;

    //prefabs
    public Transform prefabs;
    public List<ManualSlotmanualSlots = new List<ManualSlot>();
    public Transform draggingItem;
    public ManualSlot enteredSlot;


    void Awake()
    {
        instance = this;
    }

    void Start()
    {
        New_SlotMake(10);

        //Debug.Log("ManulInventory Start!!!");
    }

    void Update()
    {
        if (Input.GetKeyDown("1"))
        {
            AddItem(ItemDatabase.instance.predict_rollItems);
        }

        if (Input.GetKeyDown("2"))
        {
            Init();
        }
    }


    
    public void GetData(Toggle toggle)
    {
        Init();
        if (toggle.isOn)
        {
            //Debug.Log("predict_rollItems Init!!!");   
            AddItem(ItemDatabase.instance.predict_rollItems);
        }
        else
        {
            AddItem(ItemDatabase.instance.Guidance_rollItems);
        }
    }





    void AddItem(List<ManualRollItemitems)
    {
        for (int i = 0i < items.Counti++)
        {
            manualSlots[i].item = items[i];
            ItemImageChange(manualSlots[i]);
        }
    }


    public void ItemImageChange(ManualSlot _slot)
    {
        _slot.item.Seq_NM = _slot.transform.GetSiblingIndex();

        if (_slot.item.itemValue == -1)
            _slot.transform.GetChild(0).gameObject.SetActive(false);
        else
        {
            _slot.transform.GetChild(0).gameObject.SetActive(true);
            _slot.transform.GetChild(0).GetChild(0).gameObject.SetActive(true);

            _slot.transform.GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>().text = _slot.item.Seq_NM + "," + _slot.item.Roll_NM;
        }
    }




    void New_SlotMakeint yCount)
    {
        for (int y = 0y < yCounty++)
        {
            Transform newSlot = Instantiate(prefabstransform);
            newSlot.name = "Slot" + (y + 1);
            newSlot.transform.GetChild(0).gameObject.SetActive(false);
            manualSlots.Add(newSlot.GetComponent<ManualSlot>());
        }
        Init();

    }
    void Init()
    {
        foreach (var x in manualSlots)
        {
            x.item = new ManualRollItem();
            x.transform.GetChild(0).gameObject.SetActive(false);
        }
    }
}





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

[System.Serializable]
public class ManualRollItem  {

    public string Roll_NM;
    public int itemValue;
    public int Seq_NM;
    // Use this for initialization

    public ManualRollItem(string _Roll_NM,int _itemValue,int _Seq_NM)
    {
        Roll_NM = _Roll_NM;
        itemValue = _itemValue;
        Seq_NM = _Seq_NM;
    }

    public ManualRollItem()
    {
        itemValue = -1;
    }
}



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ManualSlot : MonoBehaviourIDragHandlerIPointerEnterHandlerIPointerExitHandlerIEndDragHandler
{
    public ManualRollItem item;

    public void OnDrag(PointerEventData data)
    {
        if (transform.childCount > 0)
        {
            transform.GetChild(0).parent = ManualInventory.instance.draggingItem;
        }
        ManualInventory.instance.draggingItem.GetChild(0).position = data.position;
    }

    public void OnPointerEnter(PointerEventData data)
    {
        ManualInventory.instance.enteredSlot = this;
    }

    public void OnPointerExit(PointerEventData data)
    {
        ManualInventory.instance.enteredSlot = null;
    }

    public void OnEndDrag(PointerEventData data)
    {
        ManualInventory.instance.draggingItem.GetChild(0).parent = transform;
        transform.GetChild(0).localPosition = Vector3.zero;

        //내용은 간단합니다. enteredSlot이 null이 아닐 때, 즉 비어있는 상태가 아닐 때 두 슬롯 간의 아이템 교환이 이루어지고 ItemImageChange 함수를 통해 아이템에 맞는 Image로 변경시켜 줍니다.
        if (ManualInventory.instance.enteredSlot != null)
        {
            ManualRollItem tempItem = item;
            item = ManualInventory.instance.enteredSlot.item;
            ManualInventory.instance.enteredSlot.item = tempItem;
            ManualInventory.instance.ItemImageChange(this);
            ManualInventory.instance.ItemImageChange(ManualInventory.instance.enteredSlot);
        }

        

    }

    
}





댓글 없음:

댓글 쓰기

git rejected error(feat. cherry-pick)

 문제 아무 생각 없이 pull을 받지않고 로컬에서 작업! 커밋, 푸시 진행을 해버렷다. push에선 remote와 다르니 당연히 pull을 진행해라고 하지만 로컬에서 작업한 내용을 백업하지 않고 진행하기에는 부담스럽다(로컬작업 유실 가능성) 해결하려...