There is a script for tugging items in the inventory. It works using the interfaces IBeginDragHandler, IDragHandler and IEndDragHandler. How to make Unity distinguish which mouse button I clicked. I can drag objects by holding the right or left mouse button, but I want to hold the right mouse button and I can simply drag objects. And when you click the left mouse button, my items were divided by 2.
2 answers
You can add input validation (just not like the previous speaker - why do you need to know each Update what you clicked, if you can check it when pressed?) And the IPointerClickHandler interface for "sharing" :) In your case, this:
public void OnBeginDrag(PointerEventData eventData) { if (Input.GetMouseButtonDown(1)) { //ΡΡΡ Π»ΠΎΠ³ΠΈΠΊΠ° Π½Π°ΡΠ°Π»Π° ΠΏΠ΅ΡΠ΅ΡΠ°ΡΠΊΠΈΠ²Π°Π½ΠΈΡ. ΠΈ ΠΊΠ°ΠΊΠΎΠΉ-Π½ΠΈΠ±ΡΠ΄Ρ ΡΠ»Π°ΠΆΠΎΠΊ _isDraging = true, //ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΡΠΎΠ²Π΅ΡΡΠ΅ΡΡΡ Π² ΠΎΡΡΠ°Π»ΡΠ½ΡΡ
ΠΌΠ΅ΡΠΎΠ΄Π°Ρ
ΠΏΠ΅ΡΠ΅ΡΡΠ³ΠΈΠ²Π°Π½ΠΈΡ. } } public void OnPointerClick(PointerEventData eventData); { if (Input.GetMouseButtonDown(0)) { //ΡΡΡ Π»ΠΎΠ³ΠΈΠΊΠ° ΡΠ°Π·Π΄Π΅Π»Π΅Π½ΠΈΡ. } } The OnPointerClick method will only work if the cursor has been lowered and raised above the same element.
|
Then, for starters, before all actions, determine the button pressed by the player:
https://docs.unity3d.com/ru/530/ScriptReference/Input.GetMouseButtonDown.html
// ΠΡΠΈΠΌΠ΅Ρ using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Update() { if (Input.GetMouseButtonDown(0)) Debug.Log("Pressed left click."); // Π»ΠΈΠ±ΠΎ Π²Π°Ρ ΠΊΠΎΠ΄ if (Input.GetMouseButtonDown(1)) Debug.Log("Pressed right click."); // Π»ΠΈΠ±ΠΎ Π²Π°Ρ ΠΊΠΎΠ΄ if (Input.GetMouseButtonDown(2)) Debug.Log("Pressed middle click."); // Π»ΠΈΠ±ΠΎ Π²Π°Ρ ΠΊΠΎΠ΄ } } |