Beauty of Backpack System

Links to the original text: https://github.com/dougen/Unity-Backpack

 

 

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

/// <summary>
/// Mouse dragging knapsack
/// </summary>
public class DragMe : MonoBehaviour, IBeginDragHandler, IDragHandler
{
	private Vector3 dragOffset;

    public void OnBeginDrag(PointerEventData eventData)
    {
        Vector3 worldPos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform as RectTransform, eventData.position, null, out worldPos))
        {
            dragOffset = new Vector3(transform.position.x - worldPos.x, transform.position.y - worldPos.y, 0f);
            transform.position = worldPos + dragOffset;
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector3 worldPos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform as RectTransform, eventData.position, null, out worldPos))
        {
            transform.position = worldPos + dragOffset;
        }
    }

  
}

This class mainly implements the function of dragging the knapsack by the mouse. In this case, we should pay attention to why there is an offset position. When you do not add the offset position, when the mouse clicks on the backpack, it will find that no matter where the click is knapsack, the mouse will be aligned with the backpack center. This is the role of the center point, so in order to solve this problem, we need to add this offset. Drag this class onto Backpack.

 

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class DropController : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
{
    private Color hightLightColor;
    private Color hideColor;
    private Image image;

    private void Start()
    {
        hightLightColor = new Color(1f, 1f, 1f, 0.2f);
        hideColor = new Color(1f, 1f, 1f, 0f);
        image = GetComponent<Image>();
    }

    public void OnDrop(PointerEventData eventData)
    {
        GameObject item = eventData.pointerDrag;
        item.GetComponent<DragController>().PutItem(transform as RectTransform);
        HideColor();
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (eventData.dragging)
        {
            image.color = hightLightColor;
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (eventData.dragging)
        {
            HideColor();
        }
    }

    public void HideColor()
    {
        image.color = hideColor;
    }
}

Drag this class onto the lattice ItemSlot.

 

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

/// <summary>
//Object grabbing controller, which implements drag function with mouse.
/// </summary>
public class DragController : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler
{
    public RectTransform canvas;
    // The nearest item bar to store items
    public RectTransform lastSlot;

    // This parameter is used to adjust the offset between mouse coordinates and object coordinates when the mouse clicks.
    private Vector3 dragOffset;
    private CanvasGroup canvasGroup;

    private void Start()
    {
        lastSlot = transform.parent as RectTransform;
        canvasGroup = GetComponent<CanvasGroup>();
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        transform.SetParent(canvas);
        canvasGroup.blocksRaycasts = false;
        Vector3 worldPos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(canvas, eventData.position, null, out worldPos))
        {
            dragOffset = new Vector3(transform.position.x - worldPos.x, transform.position.y - worldPos.y, 0f);
            transform.position = worldPos + dragOffset;
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector3 worldPos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(canvas, eventData.position, null, out worldPos))
        {
            transform.position = worldPos + dragOffset;
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        // When there is no alignment grid, return to the original backpack grid
        if (eventData.pointerEnter == null || eventData.pointerEnter.tag != "Slot")
        {
            PutItem(lastSlot);
        }
    }

    // Put items in an item bar
    public void PutItem(RectTransform slot)
    {
        lastSlot = slot;
        transform.SetParent(slot);
        transform.localPosition = Vector3.zero;
        canvasGroup.blocksRaycasts = true;
    }

    // When there are other items that you want to put in your own compartment, the two sides exchange positions.
    public void OnDrop(PointerEventData eventData)
    {
        // Let the highlighting effect of the item bar disappear first
        lastSlot.GetComponent<DropController>().HideColor();
        var dc = eventData.pointerDrag.GetComponent<DragController>();
        var tempSlot = dc.lastSlot;
        dc.PutItem(lastSlot);
        PutItem(tempSlot);
    }
}
using System.Collections;

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class Tooltips : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public RectTransform canvas; // Canvas reference
    public GameObject itemInfoPanel; // Item Information Panel
    public Vector2 offset; // Tooltips panel offset

    private static float hoverTimer = 1.0f; // Mouse hover time
    private static GameObject tooltips = null; // Unique reference to property panel

    private float timer = 0f;
    private bool pointEntered = false;

    private ItemInfo info;

    void Start()
    {
        info.name = "Flame bow";
        info.type = "Bow and arrow";
        info.prop = "attack";
        info.value = 5;
    }

    void Update()
    {
        if (pointEntered && timer <= hoverTimer)
        {
            timer += Time.deltaTime;
            if (timer > hoverTimer)
            {
                PopupToolTips();
            }
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        pointEntered = true;
        if (eventData.dragging)
        {
            if (tooltips != null)
            {
                tooltips.SetActive(false);
            }
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        pointEntered = false;
        timer = 0f;
        if (tooltips != null)
        {
            tooltips.SetActive(false);
        }
    }

    public void PopupToolTips()
    {
        Vector3 initPos = new Vector3(transform.position.x + offset.x, transform.position.y + offset.y, transform.position.z);
        if (tooltips == null)
        {
            tooltips = Instantiate(itemInfoPanel, initPos, Quaternion.identity);
            tooltips.transform.SetParent(canvas);
        }
        tooltips.transform.position = initPos;
        tooltips.GetComponent<ItemInfoPanel>().SetInfoPanel(info);
        tooltips.SetActive(true);
    }
}

These two categories are supported on the items.

using UnityEngine;
using UnityEngine.UI;

public class ItemInfoPanel : MonoBehaviour
{
    public Font font;


    public void SetInfoPanel(ItemInfo info)
    {
		var childs = GetComponentsInChildren<Text>(true);
		if (childs.Length > 0)
		{
			foreach (var child in childs)
			{
				Destroy(child.gameObject);
			}
		}
        AddText("ItemName", string.Format("<color=cyan><b>{0}</b></color>", info.name));
        AddText("ItemType", string.Format("Item type:{0}", info.type));
		AddText("ItemPro", string.Format("{0}: +{1}",info.prop,info.value));
    }

    private void AddText(string objectName, string text)
    {
        var obj = new GameObject(objectName);
        obj.transform.SetParent(transform);
        var typeText = obj.AddComponent(typeof(Text)) as Text;
        typeText.font = font;
        typeText.text = text;
    }


}

This class is dragged onto the information prompt panel.

public struct ItemInfo
{
    public string name;
    public string type;
    public string prop;
    public int value;
}

This is an information structure.

The backpack system is over here.

Tags: github Unity

Posted on Sun, 06 Oct 2019 23:16:03 -0700 by chrishide87