public void OnDrop(DragAndDropCell cellFrom, DragAndDropCell cellTo, DragAndDropItem item) { if ((item != null) && (cellFrom != cellTo)) { cellTo.SwapItems(cellFrom, item); } }
/// <summary> /// This item started to drag. /// </summary> /// <param name="eventData"></param> public void OnBeginDrag(PointerEventData eventData) { if (dragDisabled == false) { Animator anim = LINCE.GetComponent <Animator>(); anim.SetInteger("estaComiendo", 1); sourceCell = GetCell(); // Remember source cell draggedItem = this; // Set as dragged item // Create item's icon icon = new GameObject(); icon.transform.SetParent(canvas.transform); icon.name = "Icon"; myImage = GetComponent <Image>(); myImage.color = new Color(myImage.color.r, myImage.color.g, myImage.color.b, 0); //Se quita el item del plato myImage.raycastTarget = false; // Disable icon's raycast for correct drop handling Image iconImage = icon.AddComponent <Image>(); iconImage.raycastTarget = false; iconImage.sprite = myImage.sprite; RectTransform iconRect = icon.GetComponent <RectTransform>(); // Set icon's dimensions RectTransform myRect = GetComponent <RectTransform>(); iconRect.pivot = new Vector2(0.5f, 0.5f); iconRect.anchorMin = new Vector2(0.5f, 0.5f); iconRect.anchorMax = new Vector2(0.5f, 0.5f); iconRect.sizeDelta = new Vector2(myRect.rect.width, myRect.rect.height); if (OnItemDragStartEvent != null) { OnItemDragStartEvent(this); // Notify all items about drag start for raycast disabling } } }
static public event DragEvent OnItemDragEndEvent; // Drag end event /// <summary> /// This item is dragged /// </summary> /// <param name="eventData"></param> public void OnBeginDrag(PointerEventData eventData) { sourceCell = GetComponentInParent <DragAndDropCell>(); // Remember source cell draggedItem = this; // Set as dragged item icon = new GameObject("Icon"); // Create object for item's icon Image image = icon.AddComponent <Image>(); image.sprite = draggedItem.GetComponent <Image>().sprite; image.raycastTarget = false; // Disable icon's raycast for correct drop handling RectTransform iconRect = icon.GetComponent <RectTransform>(); // Set icon's dimensions //iconRect.sizeDelta = new Vector2( GetComponent<RectTransform>().sizeDelta.x, // GetComponent<RectTransform>().sizeDelta.y); iconRect.sizeDelta = new Vector2(96, 140); Canvas canvas = GetComponentInParent <Canvas>(); // Get parent canvas if (canvas != null) { // Display on top of all GUI (in parent canvas) icon.transform.SetParent(canvas.transform, true); // Set canvas as parent icon.transform.SetAsLastSibling(); // Set as last child in canvas transform } if (OnItemDragStartEvent != null) { OnItemDragStartEvent(this); // Notify all about item drag start } }
/// <summary> /// Put item into this cell. /// </summary> /// <param name="item">Item.</param> private void PlaceItem(DragAndDropItem item) { if (item != null) { DestroyItem(); // Remove current item from this cell myDadItem = null; DragAndDropCell cell = item.GetComponentInParent <DragAndDropCell>(); if (cell != null) { if (cell.unlimitedSource == true) { string itemName = item.name; item = Instantiate(item); // Clone item from source cell item.name = itemName; } } item.transform.SetParent(transform, false); item.transform.localPosition = Vector3.zero; item.MakeRaycast(true); myDadItem = item; Image dadImage = myDadItem.GetComponent <Image>(); if (dadImage.color.a == 0) { float r = dadImage.color.r; float g = dadImage.color.g; float b = dadImage.color.b; dadImage.color = new Color(r, g, b, 1); } } UpdateBackgroundState(); }
void Awake() { allItemTypes = new List <InventoryItem.ItemType>() { InventoryItem.ItemType.Artifact, InventoryItem.ItemType.Backpack, InventoryItem.ItemType.Bag, InventoryItem.ItemType.Suit, InventoryItem.ItemType.Container, InventoryItem.ItemType.Detector, InventoryItem.ItemType.Flashlight, InventoryItem.ItemType.Helmet, InventoryItem.ItemType.Knife, InventoryItem.ItemType.Mask, InventoryItem.ItemType.Other, InventoryItem.ItemType.PrimaryWeapon, InventoryItem.ItemType.SecondaryWeapon, InventoryItem.ItemType.Vest }; player = Player.Instance; foreach (InventoryItem item in player.Inventory.GetInventoryItems()) { GameObject newCell = Instantiate(inventoryCellPrefab); DragAndDropCell cellController = newCell.GetComponent <DragAndDropCell>(); cellController.acceptableItems = new List <InventoryItem.ItemType>(allItemTypes); newCell.GetComponent <Transform>().SetParent(backpack1Area); AddItemToCell(item, newCell.GetComponent <DragAndDropCell>()); } weaponCondition = new ProgressBar(weaponConditionBar, 1, 1); suitCondition = new ProgressBar(suitConditionBar, 1, 1); }
/// <summary> /// Swap items between two cells /// </summary> /// <param name="firstCell"> Cell </param> /// <param name="secondCell"> Cell </param> public void SwapItems(DragAndDropCell firstCell, DragAndDropCell secondCell) { if ((firstCell != null) && (secondCell != null)) { DragAndDropItem firstItem = firstCell.GetItem(); // Get item from first cell DragAndDropItem secondItem = secondCell.GetItem(); // Get item from second cell // Swap items if (firstItem != null) { firstItem.transform.SetParent(secondCell.transform, false); firstItem.transform.localPosition = Vector3.zero; firstItem.MakeRaycast(true); } if (secondItem != null) { secondItem.transform.SetParent(firstCell.transform, false); secondItem.transform.localPosition = Vector3.zero; secondItem.MakeRaycast(true); } // Update states firstCell.UpdateMyItem(); secondCell.UpdateMyItem(); firstCell.UpdateBackgroundState(); secondCell.UpdateBackgroundState(); } }
/// <summary> /// This item started to drag. /// </summary> /// <param name="eventData"></param> public void OnBeginDrag(PointerEventData eventData) { if (dragDisabled == false) { sourceCell = GetCell(); // Remember source cell draggedItem = this; // Set as dragged item // Create item's icon icon = new GameObject(); icon.transform.SetParent(canvas.transform); icon.name = "Icon"; Image myImage = GetComponent <Image>(); myImage.raycastTarget = false; // Disable icon's raycast for correct drop handling Image iconImage = icon.AddComponent <Image>(); iconImage.raycastTarget = false; iconImage.sprite = myImage.sprite; iconImage.preserveAspect = true; RectTransform iconRect = icon.GetComponent <RectTransform>(); // Set icon's dimensions RectTransform myRect = GetComponent <RectTransform>(); iconRect.pivot = new Vector2(0.5f, 0.5f); iconRect.anchorMin = new Vector2(0.5f, 0.5f); iconRect.anchorMax = new Vector2(0.5f, 0.5f); iconRect.localScale = myRect.localScale; iconRect.sizeDelta = new Vector2(myRect.rect.width, myRect.rect.height); if (OnItemDragStartEvent != null) { OnItemDragStartEvent(this); // Notify all items about drag start for raycast disabling } } }
/// <summary> /// Creates an item cell, showing the name and description /// </summary> /// <param name="cell">Cell.</param> /// <param name="itemObject">Item object.</param> private void createItemCell(DragAndDropCell cell, Item itemObject) { GameObject item = Instantiate(Resources.Load("Item", typeof(GameObject))) as GameObject; item.name = "Item"; item.transform.Find("Text").GetComponent <Text> ().text = itemObject.Name + " - " + itemObject.Desc; cell.PlaceItem(item); }
public void OnDrop(PointerEventData data) { if ((DragAndDropItem.draggedItemContentCopy != null) && DragAndDropItem.draggedItemContentCopy.activeSelf) { DragAndDropItem draggedItem = DragAndDropItem.draggedItem; DragAndDropCell sourceCell = DragAndDropItem.sourceCell; this.dropController.OnDrop(sourceCell, this, draggedItem); } }
public void SwapItems(DragAndDropCell sourceCell, DragAndDropItem item) { DragAndDropItem item2 = this.GetItem(); this.PlaceItem(item); if (item2 != null) { sourceCell.PlaceItem(item2); } }
private void refreshKuyruk(DragAndDropCell cell, GameObject item) { Debug.Log("refresKuyruk soruce cell: " + cell.GetComponent <Image>().sprite.name.ToString()); string name = item.name; item = Instantiate(item); item.name = name; PlaceItemForCell(cell, item); //StartCoroutine(GameObject.Find("GameManager").GetComponent<wheelManager>().refKuyruk(item)); }
void OnItemPlace(DragAndDropCell.DropDescriptor desc) { DummyControlUnit sourceSheet = desc.sourceCell.GetComponentInParent<DummyControlUnit>(); DummyControlUnit destinationSheet = desc.destinationCell.GetComponentInParent<DummyControlUnit>(); // If item dropped between different sheets if (destinationSheet != sourceSheet) { Debug.Log(desc.item.name + " is dropped from " + sourceSheet.name + " to " + destinationSheet.name); } }
/// <summary> /// Initialises variables and creates item objects as required /// </summary> void Start() { player = GameObject.Find("Player"); player.SetActive(false); itemContainers = new DragAndDropCell[6]; playerContainers = new GameObject[6]; data = PlayerData.instance.data; items = data.Items; players = data.Players; GameObject container; GameObject stats; Texture2D image; //Find all cells for (int i = 0; i < 6; i++) { //Find and store the item and player container itemContainers [i] = GameObject.Find("Item" + i).GetComponent <DragAndDropCell>(); playerContainers [i] = GameObject.Find("Player" + i); //If there is an item in the item inventory if (items[i] != null) { //Load an item object in this position to drag and drop createItemCell(itemContainers[i], data.Items[i]); } //If there's not a player at this current position if (players [i] == null) { Destroy(playerContainers [i]); } else { container = playerContainers [i].transform.Find("Container").gameObject; image = players [i].Image; container.transform.Find("Image").GetComponent <Image>().sprite = Sprite.Create(image, new Rect(0.0f, 0.0f, image.width, image.height), new Vector2(0.5f, 0.5f)); container.transform.Find("Name").GetComponent <Text> ().text = players [i].Name; stats = container.transform.Find("Stats").gameObject; stats.transform.Find("Attack").GetComponent <Text> ().text = "Attack: " + players [i].Attack.ToString(); stats.transform.Find("Defence").GetComponent <Text> ().text = "Defence: " + players [i].Defence.ToString(); stats.transform.Find("Magic").GetComponent <Text> ().text = "Magic: " + players [i].Magic.ToString() + " / " + players[i].MaximumMagic.ToString(); stats.transform.Find("Luck").GetComponent <Text> ().text = "Luck: " + players [i].Luck.ToString(); stats.transform.Find("Speed").GetComponent <Text> ().text = "Speed: " + players [i].Speed.ToString(); if (players [i].Item != null) { DragAndDropCell itemCell = playerContainers [i].transform.Find("Container/Stats/Item").GetComponent <DragAndDropCell> (); createItemCell(itemCell, players [i].Item); //Disabled the container from having items dragged into it and set to grey to indicate this //playerContainers [i].enabled = false; //playerContainers [i].GetComponent<Image> ().color = Color.grey; } } } }
private void Awake() { this.dragAndDropCell = base.GetComponent <DragAndDropCell>(); if (this.dropInnerGlow) { this.dropInnerGlow.gameObject.SetActive(false); } if (this.dropOuterGlow) { this.dropOuterGlow.gameObject.SetActive(false); } }
/// <summary> /// Item is dropped in this cell /// </summary> /// <param name="data"></param> public void OnDrop(PointerEventData data) { if (DragAndDropItem.icon != null) { DragAndDropItem item = DragAndDropItem.draggedItem; DragAndDropCell sourceCell = DragAndDropItem.sourceCell; if (DragAndDropItem.icon.activeSelf == true) // If icon inactive do not need to drop item into cell { if ((item != null) && (sourceCell != this)) { DropEventDescriptor desc = new DropEventDescriptor(); switch (cellType) // Check this cell's type { case CellType.DropOnly: // Item only can be dropped into destination cell Text labelText = this.transform.Find("labelTemplate").GetComponent <Text> (); if (labelText.text == "Choose One") { break; } desc.item = item; desc.sourceCell = sourceCell; desc.destinationCell = this; SendRequest(desc); // Send drop request FindObjectOfType <AudioManager> ().Play("tileInBucket"); StartCoroutine(NotifyOnDragEnd(desc)); // Send notification after drop will be finished if (desc.permission == true) // If drop permitted by application { PlaceItem(item); // Place dropped item in this cell } break; default: break; } } } if (item != null) { if (item.GetComponentInParent <DragAndDropCell> () == null) // If item have no cell after drop { Destroy(item.gameObject); // Destroy it } } UpdateMyItem(); UpdateBackgroundState(); sourceCell.UpdateMyItem(); sourceCell.UpdateBackgroundState(); } }
/// <summary> /// Resets all temporary conditions. /// </summary> private void ResetConditions() { if (icon != null) { Destroy(icon); // Destroy icon on item drop } if (OnItemDragEndEvent != null) { OnItemDragEndEvent(this); // Notify all cells about item drag end } draggedItem = null; icon = null; sourceCell = null; }
/// <summary> /// Put new item in this cell /// </summary> /// <param name="itemObj"> New item's object with DragAndDropItem script </param> /// public void PlaceItemForCell(DragAndDropCell cell, GameObject itemObj) { cell.RemoveItem(); // Remove current item from this cell if (itemObj != null) { itemObj.transform.SetParent(cell.transform, false); itemObj.transform.localPosition = Vector3.zero; DragAndDropItem item = itemObj.GetComponent <DragAndDropItem>(); if (item != null) { item.MakeRaycast(true); } cell.SetBackgroundState(true); } }
public void OnEndDrag(PointerEventData eventData) { if (icon != null) { Destroy(icon); // Destroy icon on item drop } MakeVisible(true); // Make item visible in cell if (OnItemDragEndEvent != null) { OnItemDragEndEvent(this); // Notify all cells about item drag end } draggedItem = null; icon = null; sourceCell = null; }
/// <summary> /// Resets all temporary conditions. /// </summary> private void ResetConditions() { this.GetComponent <Image>().enabled = true; if (icon != null) { Destroy(icon); // Destroy icon on item drop } if (OnItemDragEndEvent != null) { OnItemDragEndEvent(this); // Notify all cells about item drag end } draggedItem = null; icon = null; sourceCell = null; }
/// <summary> /// Resets all temporary conditions. /// </summary> private void ResetConditions() { if (icon != null) { Destroy(icon); // Destroy icon on item drop } if (OnItemDragEndEvent != null) { OnItemDragEndEvent(this); // Notify all cells about item drag end } draggedItem = null; icon = null; sourceCell = null; GetComponent <Image>().enabled = true; FindObjectOfType <AudioManager>().Play("endDrag"); }
/// <summary> /// This item is dropped /// </summary> /// <param name="eventData"></param> public void OnEndDrag(PointerEventData eventData) { if (icon != null) { //txtNumber.transform.SetParent(gameObject.transform, false); Destroy(icon); // Destroy icon on item drop } MakeVisible(true); // Make item visible in cell if (OnItemDragEndEvent != null) { OnItemDragEndEvent(this); // Notify all cells about item drag end } draggedItem = null; icon = null; sourceCell = null; }
public void OnEndDrag(PointerEventData eventData) { if ((eventData == null) || (eventData.button != PointerEventData.InputButton.Right)) { if (draggedItemContentCopy != null) { Destroy(draggedItemContentCopy); } this.MakeVisible(true); if (OnItemDragEndEvent != null) { OnItemDragEndEvent(this, eventData); } draggedItem = null; draggedItemContentCopy = null; sourceCell = null; } }
/// <summary> /// This item is dropped /// </summary> /// <param name="eventData"></param> public void OnEndDrag(PointerEventData eventData) { MouseInput mouseInput = GameObject.FindObjectOfType <MouseInput> (); mouseInput.isDragging = false; if (icon != null) { Destroy(icon); // Destroy icon on item drop } MakeVisible(true); // Make item visible in cell if (OnItemDragEndEvent != null) { OnItemDragEndEvent(this); // Notify all cells about item drag end } draggedItem = null; icon = null; sourceCell = null; }
/// <summary> /// Put item into this cell. /// </summary> /// <param name="item">Item.</param> private void PlaceItem(DragAndDropItem item) { if (item != null) { DestroyItem(); // Remove current item from this cell myDadItem = null; DragAndDropCell cell = item.GetComponentInParent <DragAndDropCell>(); if (cell != null) { if (cell.unlimitedSource == true) { string itemName = item.name; item = Instantiate(item); // Clone item from source cell item.name = itemName; } } item.transform.SetParent(transform, false); item.transform.localPosition = Vector3.zero; item.MakeRaycast(true); myDadItem = item; myDadItem.GetComponent <CanvasGroup>().alpha = 0.5f; //Hace que el item no sea visible cuando llega a la boca GameObject canvas = gameObject.transform.parent.gameObject; AtributosMascota atributos = canvas.transform.Find("MANAGER CANVAS").GetComponent <AtributosMascota>(); Animator anim = LINCE.GetComponent <Animator>(); anim.SetInteger("estaComiendo", 2); GestionComida gestorComida = canvas.transform.Find("Plato").GetComponent <GestionComida>(); GameObject comida = canvas.transform.Find("Plato").gameObject.transform.Find("Comida").gameObject; TMP_Text porcentaje = canvas.transform.Find("Plato").gameObject.transform.Find("Porcentaje").gameObject.GetComponent <TMP_Text>(); comida.GetComponent <DragAndDropItem>().enabled = false;; atributos.subirHambre(int.Parse(porcentaje.text)); gestorComida.EliminarItem(item.gameObject); Image imagen = comida.GetComponent <Image>(); imagen.color = new Color(imagen.color.r, imagen.color.g, imagen.color.b, 0.5f); //Item del plato StartCoroutine(ExecuteAfterTime(3)); } UpdateBackgroundState(); }
public void OnBeginDrag(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Right) { sourceCell = base.GetComponentInParent <DragAndDropCell>(); draggedItem = this; draggedItemContentCopy = this.GetCopy(this.itemContent); Canvas componentInParent = sourceCell.transform.parent.GetComponentInParent <Canvas>(); if (componentInParent != null) { draggedItemContentCopy.transform.SetParent(componentInParent.transform, false); draggedItemContentCopy.transform.SetAsLastSibling(); } this.MakeVisible(false); if (OnItemDragStartEvent != null) { OnItemDragStartEvent(this, eventData); } } }
/// <summary> /// This item is dropped /// </summary> /// <param name="eventData"></param> public void OnEndDrag(PointerEventData eventData) { if (icon != null) { Destroy(icon); // Destroy icon on item drop } MakeVisible(true); // Make item visible in cell if (OnItemDragEndEvent != null) { OnItemDragEndEvent(this); // Notify all cells about item drag end } GameObject child = draggedItem.transform.GetChild(0).gameObject; GameObject parentCell = draggedItem.transform.parent.gameObject; draggedItem.GetComponent <RectTransform> ().sizeDelta = parentCell.GetComponent <RectTransform> ().sizeDelta; child.GetComponent <RectTransform> ().sizeDelta = parentCell.GetComponent <RectTransform> ().sizeDelta; draggedItem = null; icon = null; sourceCell = null; }
/// <summary> /// Swap items between to cells /// </summary> /// <param name="firstCell"> Cell </param> /// <param name="secondCell"> Cell </param> public void SwapItems(DragAndDropCell firstCell, DragAndDropCell secondCell) { if ((firstCell != null) && (secondCell != null)) { DragAndDropItem firstItem = firstCell.GetItem(); // Get item from first cell DragAndDropItem secondItem = secondCell.GetItem(); // Get item from second cell ItemSlot slot1 = firstCell.GetComponent <ItemSlot>(); ItemSlot slot2 = secondCell.GetComponent <ItemSlot>(); Item item1 = slot1.item; Image image1 = slot1.image; Text text1 = slot1.counterText; Item item2 = slot2.item; Image image2 = slot2.image; Text text2 = slot2.counterText; if (firstItem != null) { slot1.item = item2; slot1.image = image2; slot1.counterText = text2; // Place first item into second cell firstItem.transform.SetParent(secondCell.transform, false); firstItem.transform.localPosition = Vector3.zero; secondCell.SetBackgroundState(true); } if (secondItem != null) { slot2.item = item1; slot2.image = image1; slot2.counterText = text1; // Place second item into first cell secondItem.transform.SetParent(firstCell.transform, false); secondItem.transform.localPosition = Vector3.zero; firstCell.SetBackgroundState(true); } } }
/// <summary> /// Swap items between two cells /// </summary> /// <param name="firstCell"> Cell </param> /// <param name="secondCell"> Cell </param> public void SwapItems(DragAndDropCell firstCell, DragAndDropCell secondCell) { if ((firstCell != null) && (secondCell != null)) { DragAndDropItem firstItem = DragAndDropItem.draggedItem; // Get item from first cell DragAndDropItem secondItem = secondCell.GetItem(); // Get item from second cell // Swap items if (secondItem != null) { if (secondItem.transform.parent != null) { secondItem.transform.parent.DetachChildren(); DragAndDropItem tempItem = Instantiate(secondItem); tempItem.GetComponent <PorteScript>().SetEntree1(false); tempItem.GetComponent <PorteScript>().SetEntree2(false); tempItem.GetComponent <PorteScript>().SetSortie(false); Destroy(secondItem.gameObject); secondItem = null; secondItem = tempItem; } secondItem.transform.SetParent(firstCell.transform, false); secondItem.transform.localPosition = Vector3.zero; secondItem.MakeRaycast(true); } if (firstItem != null) { firstItem.transform.SetParent(secondCell.transform, false); firstItem.transform.localPosition = Vector3.zero; firstItem.MakeRaycast(true); } // Update states firstCell.UpdateMyItem(); secondCell.UpdateMyItem(); //Debug.Log(secondItem.GetComponent<PorteScript>().IsSortie()); //firstCell.UpdateBackgroundState(); //secondCell.UpdateBackgroundState(); } }
/// <summary> /// Initialises variables and creates item objects as required /// </summary> void Start() { player = GameObject.Find("Player"); player.SetActive(false); itemContainers = new DragAndDropCell[6]; playerContainers = new GameObject[6]; data = PlayerData.instance.data; items = data.Items; players = data.Players; //Find all cells for (int i = 0; i < 6; i++) { //Find and store the item and player container itemContainers [i] = GameObject.Find("Item" + i).GetComponent <DragAndDropCell>(); playerContainers [i] = GameObject.Find("Player" + i); //If there is an item in the item inventory if (items[i] != null) { //Load an item object in this position to drag and drop createItemCell(itemContainers[i], data.Items[i]); } //If there's not a player at this current position if (players [i] == null) { Destroy(playerContainers [i]); } else { updatePlayerStats(i); if (players [i].Item != null) { DragAndDropCell itemCell = playerContainers [i].transform.Find("Container/Stats/Item").GetComponent <DragAndDropCell> (); createItemCell(itemCell, players [i].Item); //Disabled the container from having items dragged into it and set to grey to indicate this //playerContainers [i].enabled = false; //playerContainers [i].GetComponent<Image> ().color = Color.grey; } } } }
/// <summary> /// Put item into this cell. /// </summary> /// <param name="item">Item.</param> private void PlaceItem(DragAndDropItem item) { if (item != null) { DestroyItem(); // Remove current item from this cell myDadItem = null; DragAndDropCell cell = item.GetComponentInParent <DragAndDropCell>(); if (cell != null) { if (cell.unlimitedSource == true) { string itemName = item.name; item = Instantiate(item); // Clone item from source cell item.name = itemName; } } item.transform.SetParent(transform, false); item.transform.localPosition = Vector3.zero; item.MakeRaycast(true); myDadItem = item; } }
/// <summary> /// Swap items between to cells /// </summary> /// <param name="firstCell"> Cell </param> /// <param name="secondCell"> Cell </param> public void SwapItems(DragAndDropCell firstCell, DragAndDropCell secondCell) { if ((firstCell != null) && (secondCell != null)) { DragAndDropItem firstItem = firstCell.GetItem(); // Get item from first cell DragAndDropItem secondItem = secondCell.GetItem(); // Get item from second cell if (firstItem != null) { // Place first item into second cell firstItem.transform.SetParent(secondCell.transform, false); firstItem.transform.localPosition = Vector3.zero; secondCell.SetBackgroundState(true); } if (secondItem != null) { // Place second item into first cell secondItem.transform.SetParent(firstCell.transform, false); secondItem.transform.localPosition = Vector3.zero; firstCell.SetBackgroundState(true); } } }
/// <summary> /// This item is dragged /// </summary> /// <param name="eventData"></param> public void OnBeginDrag(PointerEventData eventData) { sourceCell = GetComponentInParent<DragAndDropCell>(); // Remember source cell draggedItem = this; // Set as dragged item icon = new GameObject("Icon"); // Create object for item's icon Image image = icon.AddComponent<Image>(); image.sprite = GetComponent<Image>().sprite; image.raycastTarget = false; // Disable icon's raycast for correct drop handling RectTransform iconRect = icon.GetComponent<RectTransform>(); // Set icon's dimensions iconRect.sizeDelta = new Vector2( GetComponent<RectTransform>().sizeDelta.x, GetComponent<RectTransform>().sizeDelta.y); Canvas canvas = GetComponentInParent<Canvas>(); // Get parent canvas if (canvas != null) { // Display on top of all GUI (in parent canvas) icon.transform.SetParent(canvas.transform, true); // Set canvas as parent icon.transform.SetAsLastSibling(); // Set as last child in canvas transform } if (OnItemDragStartEvent != null) { OnItemDragStartEvent(this); // Notify all about item drag start } }
/// <summary> /// This item is dragged /// </summary> public void OnDragStart() { dragedItem = null; sourceCell = null; if (dragEnabled == true) { sourceCell = GetComponentInParent<DragAndDropCell>(); // Remember source cell dragedItem = this; // Set as dragged item MakeRaycast(false); // Disable raycast Canvas canvas = GetComponentInParent<Canvas>(); // Get parent canvas if (canvas != null) { // Displya on top of all GUI (in current canvas) canvasTransform = canvas.transform; // Remember parent canvas transform transform.SetParent(canvasTransform, true); // Set canvas as parent transform.SetAsLastSibling(); // Set as last child in canvas transform } if (OnItemDragStartEvent != null) { OnItemDragStartEvent(); // Notify all about item drag start } if (sourceCell != null) { sourceCell.ItemDragStart(); // Notify source cell about item drag start } } }
/// <summary> /// This item is droped /// </summary> public void OnDragEnd() { bool placed = false; if (OnItemDragEndEvent != null) { OnItemDragEndEvent(); // Notify all about item drag end } if (transform.parent == canvasTransform) // If item did not put in cell { if (sourceCell != null) { sourceCell.ItemReturn(); // Return item to source cell } else { Debug.Log("Something wrong"); } } else { placed = true; } dragedItem = null; sourceCell = null; if (placed == true) { // Notify parents about item placed in new cell gameObject.SendMessageUpwards("OnItemPlace", this, SendMessageOptions.DontRequireReceiver); } }
/// <summary> /// This item is dropped /// </summary> /// <param name="eventData"></param> public void OnEndDrag(PointerEventData eventData) { if (icon != null) { Destroy(icon); // Destroy icon on item drop } MakeVisible(true); // Make item visible in cell if (OnItemDragEndEvent != null) { OnItemDragEndEvent(this); // Notify all cells about item drag end } draggedItem = null; icon = null; sourceCell = null; }