public virtual void ItemLeft(GameObject item) { // release the item from this object's hierarchy item.transform.SetParent(JohnnyUITools.GetMyCanvas(gameObject).transform); currentItem = null; }
/// <summary> /// find mouse position under the gameobject's canvas /// </summary> /// <param name="gameObject"></param> /// <returns>Vector2: mouse position</returns> public static Vector2 GetMousePosInMyCanvas(GameObject gameObject) { // find the canvas I'm currently under Canvas myCanvas = JohnnyUITools.GetMyCanvas(gameObject); // calculate mouse's position using this canvas' coordinates (anchor = lower left corner) float mousePosX = Input.mousePosition.x; float mousePosY = Input.mousePosition.y; float canvasWidth = myCanvas.GetComponent <RectTransform>().sizeDelta.x; float canvasHeight = myCanvas.GetComponent <RectTransform>().sizeDelta.y; float mousePosInCanvasX = mousePosX / Screen.width * canvasWidth; float mousePosInCanvasY = mousePosY / Screen.height * canvasHeight; return(new Vector2(mousePosInCanvasX, mousePosInCanvasY)); }