Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {   // delete inventory
            if (!eventSystem.IsPointerOverGameObject(-1) && from != null)
            {
                from.GetComponent <Image>().color = Color.white;
                from.clearSlot();
                Destroy(GameObject.Find("Hover"));
                hoverObject = null;
                from        = null;
                to          = null;
            }
        }


        if (hoverObject != null)
        {
            // code making icon hover around screen
            Vector2 position;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out position);
            position.Set(position.x, position.y - hoverYOffset); //??
            hoverObject.transform.position = canvas.transform.TransformPoint(position);
            player.GetComponent <UnityEngine.AI.NavMeshAgent>().ResetPath();
        }
    }
Example #2
0
    // move item
    public void MoveItem(GameObject clicked)
    {
        if (from == null)
        {
            if (!clicked.GetComponent <Slots>().isEmpty)
            {
                from = clicked.GetComponent <Slots>(); // initialize from
                from.GetComponent <Image>().color = Color.gray;


                hoverObject = Instantiate(iconPrefab);                                             // an instance of hovering object
                hoverObject.GetComponent <Image>().sprite = clicked.GetComponent <Image>().sprite; //get the image
                hoverObject.name = "Hover";                                                        // change hover icon's name

                RectTransform hoverTransform   = hoverObject.GetComponent <RectTransform>();
                RectTransform clickedTransform = clicked.GetComponent <RectTransform>();

                hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, clickedTransform.sizeDelta.x);
                hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, clickedTransform.sizeDelta.y);

                // make it visible
                hoverObject.transform.SetParent(GameObject.Find("InventoryCanvas").transform, true);
                hoverObject.transform.localScale = from.gameObject.transform.localScale; // set local scale
            }
        }
        else if (to == null)
        {
            to = clicked.GetComponent <Slots>();
            Destroy(GameObject.Find("Hover"));
        }

        if (from != null && to != null)
        {
            //swap two items
            Stack <Item> tmpTo = new Stack <Item>(to.Items); // store to as temp var
            to.swapItem(from.Items);
            if (tmpTo.Count == 0)
            {
                from.clearSlot();
            }
            else
            {
                from.swapItem(tmpTo);
            }


            from.GetComponent <Image>().color = Color.white; // reset color
            // resetting
            from        = null;
            to          = null;
            hoverObject = null; // done moving
        }
    }