嗨,我正在制作单人纸牌游戏。现在我在屏幕上显示我所有的卡片 13 x 4。我可以拖动卡片,但不知何故,从上到下的整行都被选中了。如果我向左或向右移动拖动,它会选择它旁边的卡片和整行。
所以我的问题是:我怎样才能选择 1 张卡然后拖动它而不用我拖动其他卡?
using UnityEngine;
using System.Collections;
public class SetupCards : MonoBehaviour
{
int numberOfCards = 52;
int numberOfCardSorts = 4;
public float verticalSize = 1.4f;
void OnGUI()
{
int currentSort = 0;
int cardNumber = 0;
for (int i = 0; i < numberOfCards; ++i)
{
int numberOfCardsPerSort = numberOfCards / numberOfCardSorts;
float cardSizeX = Screen.width / numberOfCardsPerSort;
if (cardNumber == numberOfCardsPerSort)
{
++currentSort;
cardNumber = 0;
}
Vector2 cardSize = new Vector2(cardSizeX, cardSizeX * verticalSize);
Rect rect = new Rect(
cardSize.x * cardNumber,
cardSize.y * currentSort,
cardSize.x,
cardSize.y);
string texturePath = "Textures/cards/";
switch (currentSort)
{
case 0:
texturePath += "diamond/diamond_";
break;
case 1:
texturePath += "clover/clover_";
break;
case 2:
texturePath += "heart/heart_";
break;
case 3:
texturePath += "spade/spade_";
break;
}
texturePath += cardNumber.ToString();
Texture texture = Resources.Load(texturePath) as Texture;
if (Input.GetMouseButton(0))
{
if (Input.mousePosition.x <= rect.x + rect.width &&
Input.mousePosition.x >= rect.x)
{
if(-Input.mousePosition.y <= rect.y + rect.height &&
-Input.mousePosition.y >= rect.y)
rect.x = Input.mousePosition.x;
{
rect.y = -Input.mousePosition.y + Screen.height;
}
}
}
GUI.Label(rect, texture);
++cardNumber;
}
}
}