I am working on an inventory system with dragable items. The problem is, that it only works in the Editor when I select iOS as the build platform. When I switch to Android it doesnt work anymore and nothing happens (also no Errors, just nothing). If I build the app as an apk it also does not work in the app, but if I click on play and then switch to simulator in the play window it works again (when android is the build platform). Very weird, I hope somebody can help, here is my DragHandler script
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DragDrop : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
{
private Canvas canvas;
private RectTransform rectTransform;
private CanvasGroup canvasGroup;
private Transform equipment;
private Vector2 beforePos;
private List<RaycastResult> _hits = new List<RaycastResult>();
private bool isSave;
private ItemHolder itemHolder;
private Transform beforeParent;
private void Awake()
{
rectTransform = GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
itemHolder = GetComponent<ItemHolder>();
}
private void Start()
{
canvas = GameObject.FindGameObjectWithTag("Menu Canvas").GetComponent<Canvas>();
}
public void OnBeginDrag(PointerEventData eventData)
{
itemHolder.itemSlot.DefaultColor();
beforePos = rectTransform.anchoredPosition;
canvasGroup.blocksRaycasts = false;
canvasGroup.alpha = .7f;
beforeParent = transform.parent;
transform.SetParent(GameObject.FindGameObjectWithTag("Equipment").transform);
if (itemHolder.itemSlot.isStorage)
{
StorageManager.instance.Leave(itemHolder);
}
}
public void OnDrag(PointerEventData eventData)
{
rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData)
{
transform.SetParent(beforeParent);
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
var rayEventData = new PointerEventData(EventSystem.current) { position = Input.mousePosition };
_hits.Clear();
var graphicsRaycasters = FindObjectsOfType<GraphicRaycaster>();
foreach (var graphicsRaycaster in graphicsRaycasters)
{
graphicsRaycaster.Raycast(eventData, _hits);
}
isSave = false;
// Do something with the hits
foreach (var result in _hits)
{
if (result.gameObject.CompareTag("Storage"))
{
//itemStats.itemSlot.Unequip();
StorageManager.instance.Store(itemHolder);
isSave = true;
break;
}
else if (result.gameObject.TryGetComponent(out ItemSlot itemSlot) == true )
{
if (itemSlot.CorrectSlot(itemHolder) && itemSlot.isStorage == false)
{
//itemStats.itemSlot.Unequip();
itemSlot.Equip(itemHolder);
isSave = true;
itemHolder.itemSlot.RecolorSlot();
break;
}
}
}
if (isSave == false)
{
if (itemHolder.itemSlot == null)
{
StorageManager.instance.Store(itemHolder);
}
else
{
itemHolder.itemSlot.RecolorSlot();
rectTransform.anchoredPosition = beforePos;
}
}
}
public void OnPointerDown(PointerEventData eventData)
{
}
}