我知道这听起来可能很奇怪,但是我需要制作一个记忆游戏,将不同的图像而不是两个相同的图像进行匹配,例如,一个数字和一只食指抬起的手
这是我的实际代码,可匹配两个相同的图像,它克隆了使相同图像出现两次的卡片
下面的场景控制器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneController : MonoBehaviour {
public const int gridRows = 2;
public const int gridCols = 5;
public const float offsetX = 4f;
public const float offsetY = 5f;
[SerializeField] private MainCard originalCard;
[SerializeField] private Sprite[] images;
private void Start()
{
Vector3 startPos = originalCard.transform.position; //The first position of the cards, all other cards are based on this one
int[] numbers = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4};
numbers = ShuffleArray(numbers);
for(int i = 0; i < gridCols; i++)
{
for(int j = 0; j < gridRows; j++)
{
MainCard card;
if(i == 0 && j == 0)
{
card = originalCard;
}
else
{
card = Instantiate(originalCard) as MainCard;
}
int index = j * gridCols + i;
int id = numbers[index];
card.ChangeSprite(id, images[id]);
float posX = (offsetX * i) + startPos.x;
float posY = (offsetY * j) + startPos.y;
card.transform.position = new Vector3(posX, posY, startPos.z);
}
}
}
private int[] ShuffleArray(int[] numbers)
{
int[] newArray = numbers.Clone() as int[];
for(int i = 0; i < newArray.Length; i++)
{
int tmp = newArray[i];
int r = Random.Range(i, newArray.Length);
newArray[i] = newArray[r];
newArray[r] = tmp;
}
return newArray;
}
//-------------------------------------------------------------------------------------------------------------------------------------------
private MainCard _firstRevealed;
private MainCard _secondRevealed;
private int _match = 5;
[SerializeField] private TextMesh matchLabel;
public bool canReveal
{
get { return _secondRevealed == null; }
}
public void CardRevealed(MainCard card)
{
if(_firstRevealed == null)
{
_firstRevealed = card;
}
else
{
_secondRevealed = card;
StartCoroutine(CheckMatch());
}
}
private IEnumerator CheckMatch()
{
if(_firstRevealed.id == _secondRevealed.id)
{
_match--;
matchLabel.text = "Pares Restantes: " + _match;
if(_match == 1){
matchLabel.text = "Somente " + _match + " Par Restante";
}
if(_match == 0){
SceneManager.LoadScene("Sucesso");
}
}
else
{
yield return new WaitForSeconds(0.5f);
_firstRevealed.Unreveal();
_secondRevealed.Unreveal();
}
_firstRevealed = null;
_secondRevealed = null;
}
public void Menu()
{
SceneManager.LoadScene("Menu");
}
public void Fases()
{
SceneManager.LoadScene("Fases");
}
public void Sair()
{
Application.Quit ();
}
public void Level1()
{
SceneManager.LoadScene("Level 1");
}
public void Sucesso()
{
SceneManager.LoadScene("Sucesso");
}
}
下面的主卡类别
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainCard : MonoBehaviour {
[SerializeField] private SceneController controller;
[SerializeField] private GameObject Card_Back;
public void OnMouseDown()
{
if(Card_Back.activeSelf && controller.canReveal)
{
Card_Back.SetActive(false);
controller.CardRevealed(this);
}
}
private int _id;
public int id
{
get { return _id; }
}
public void ChangeSprite(int id, Sprite image)
{
_id = id;
GetComponent<SpriteRenderer>().sprite = image; //Isso pega o Sprite Renderer e muda a propriedade do Sprite
}
public void Unreveal()
{
Card_Back.SetActive(true);
}
}
PS:对不起,我的英语
答案 0 :(得分:0)
您可以像以下简单示例一样将每个图像封装在类的对象中:
class CardImage : MonoBehaviour
{
public Sprite cardSprite;
public int cardNumber;
}
然后在您的游戏逻辑中,您可以检查cardNumber字段,以比较两张卡片,如下所示:
if (card1.cardNumber == card2.cardNumber)
{
// Do what you have to do when two cards are equal
}
有关OOP和封装的更多信息,请参见this