2

嗨,我正在制作单人纸牌游戏。现在我在屏幕上显示我所有的卡片 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;

        }
    }
}
4

1 回答 1

0

你的问题在这里:

        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 =将运行通过外部语句的每个 代码块- 即该垂直行中的所有卡片。rectif

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

        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;
                }                    
            }
        }
于 2012-11-16T11:08:03.653 回答