gpt4 book ai didi

c# - 单卡怎么拖

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:38 24 4
gpt4 key购买 nike

嗨,我正在制作一款单人纸牌游戏。现在我在屏幕上以 13 x 4 的比例显示我所有的卡片。我可以拖动卡片,但不知何故,整行从上到下都被选中了。如果我向左或向右移动拖动,它会选择旁边的卡片和整行。

所以我的问题是:我怎样才能选择一张卡片并拖动它而不拖动其他卡片?

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;

}
}
}

最佳答案

你的问题在这里:

        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; // THIS LINE IS THE PROBLEM
{
rect.y = -Input.mousePosition.y + Screen.height;
}
}
}

发生的事情是您的行 rect.x = Input.mousePosition.x; 紧跟在 if 语句之后,并有条件地执行。然后,包含 rect.y = 的代码块将运行传递外部 if 语句的 every rect -即该垂直线上的所有卡片。

我怀疑您需要将内部支撑向上移动一行,如下所示:

        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)
{ // THIS BRACE IS MOVED UP
rect.x = Input.mousePosition.x;
rect.y = -Input.mousePosition.y + Screen.height;
}
}
}

关于c# - 单卡怎么拖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13415175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com