它是一款手机游戏,我想让用户点击一张特定的牌,然后在后端存储该点击牌的实例,以便玩家可以使用投掷按钮投掷该牌(如图所示)。我需要写一个脚本存储卡被窃听。
答案 0 :(得分:1)
您可以公开声明一个数组并将每张卡放入该数组中。所以你可以这样选择你的卡片:
cardArray[2].gameObject <-- you will get gameObject of your 3rd card
答案 1 :(得分:0)
你只需要在某个地方保留它的引用,创建一个脚本,该脚本是播放器的手,其中包含List
GameObjects
,你将能够使用{{1我真的建议你研究ScriptableObjects它们非常强大,非常适合存储数据,减少资产的冗余..
纸牌游戏可能是实施ScriptableObjects的最具竞争力的项目之一,您将能够动态创建卡片,您还可以从编辑器内部AssetMenu 创建卡片p>
答案 2 :(得分:0)
如何点击卡片?
将其实施为统一UI按钮。为此,您可以为卡片制作一个UI按钮预制件,然后在卡片管理器脚本中将其用作预制件,以根据他们的信息生成卡片。
假设您有一个CardInfo脚本和一个CardManager脚本。
Using UnityEngine;
Using UnityEngine.UI;
Using System;
Using System.Generics;
//Manager class for a collection of cards.
public CardManager{
[Header("Cards")] //sets a header in the inspector.
public Transform CardParent; //Would be great to be a scroll views content for scrolling.
public GameObject CardPrefab; //A UI button prefab
public List<CardInfo> Cards; //List of your cards, set in the inspector.
public void SpawnCards() //Sets up all cards.
{
foreach(var card in Cards){
GameObject c = Instantiate(CardPrefab, CardParent); //Instantiate card with parent UI object.
Image img = c.GetComponent<Image>();
img.sprite = c.CardImageNotSelected; //Set card image.
c.name = CardName + CardNumber; //Set GameObject name.
Button btn = c.GetComponent<Button>();
btn.onClick.AddListener(() => ToggleCardSelect(card, img)); //Sets up what happens when you click a card.
//I did so that when you click the card it toggles the image between 2.
}
}
void ToggleCardSelect(CardInfo card, Image img){
card.CurrentlySelected = !card.CurrentlySelected; //Toggle boolean.
if (card.CurrentlySelected)
{
//Deselect if already selected.
img.sprite = CardImageNotSelected;
}
else
{
//Select
img.sprite = CardImageSelected;
}
}
}
//CardInfo represents a single card.
[System.Serializable] //allows a cardInfo to be viewable in a list in the inspector without being a simple property or inheriting monobehaviour.
public CardInfo{
public string CardName;
public int CardNumber;
public sprite CardImageSelected;
public sprite CardImageNotSelected;
public bool CurrentlySelected;
}
现在这个脚本刚刚写完了,所以它可能无法正常工作。但是,你当然应该能够使它适应工作。