我正在尝试建立一个纸牌游戏,我想把卡片从家长A到父母B动画。最初,卡的父母是父A。是否可以使用i吐温的MoveTo方法将卡从父A区域移动到父B区域,并将父B设置为其父?
public void InitializeCards(int numOfCards) {
for (int i = 0; i < numOfCards; i++) {
GameObject card = PhotonNetwork.Instantiate(
"CardDisplay",
new Vector3(0, 0, 0),
Quaternion.identity
);
card.transform.SetParent(table.transform, false);
card.GetPhotonView().RPC("Initialize", RpcTarget.Others, false, null);
card.GetPhotonView().RPC("Initialize", player, true, null);
card.transform.SetParent(deck.transform, false);
}
}在此方法中,我创建卡片,首先将其父表设置为表,然后在Initialize方法中,我将卡片从表区域移动到甲板区域。最后,我把父母安排在甲板上。
[PunRPC]
void Initialize(bool isMine, string label) {
if (label == null) {
this.card = CreateRandomCard();
this.label.text = this.card.label;
this.number.text = this.card.number;
Debug.Log(this.card.number);
this.description.text = this.card.desc;
this.typeText.text = this.card.type;
this.cardImage.sprite = this.card.cardSprite;
} else {
switch (label) {
case "Attack":
this.card = new AttackCard();
break;
case "Defense":
this.card = new DefenseCard();
break;
case "Heal":
this.card = new HealCard();
break;
case "Take Card":
this.card = new TakeCard();
break;
case "Special Attack":
this.card = new SpecialAttack();
break;
case "Billizard":
this.card = new BillizardCard();
break;
default:
break;
}
this.label.text = this.card.label;
this.number.text = this.card.number;
this.description.text = this.card.desc;
this.typeText.text = this.card.type;
this.cardImage.sprite = this.card.cardSprite;
}
if (isMine) {
GameManager.GetLocal().numOfcards++;
iTween.MoveTo(this.gameObject, iTween.Hash("position", new Vector3(-400, -375, 0), "time", 1.5f, "islocal", true));
} else {
GameManager.GetRemote().numOfcards++;
}
}发布于 2020-04-15 13:05:31
请参阅SetParent
可选参数
worldPositionStays 如果是
true,则修改父相对位置、比例和旋转,使物体保持与以前相同的世界空间位置、旋转和尺度。
和
worldPositionStays参数的默认值为true。
card.transform.SetParent(deck.transform, false);使对象被放置到新的父中,保持相对位置。
您想要的是保持的世界位置,所以传递给true (或者没有什么,因为true是默认的)
card.transform.SetParent(deck.transform);等于
card.transform.SetParent(deck.transform, true);等于简单地分配Transform.parent
card.transform.parent = deck.transform;现在看一下您的方法:
card.transform.SetParent(table.transform);
card.GetPhotonView().RPC("Initialize", RpcTarget.Others, false, null);
card.GetPhotonView().RPC("Initialize", player, true, null);
card.transform.SetParent(deck.transform);两个问题:
card.transform.SetParent(deck.transform);基本上被称为“立即”,甚至可能在吐温开始之前。你可以跳过card.transform.SetParent(table.transform);InitializeCards的一个客户端上执行此操作。在所有其他客户端上,card将保留其原始父客户机(可能是null =场景根)您更希望在Initialize方法本身内进行育儿,该方法在所有客户端上都会被调用,并在moveTo完成或启动之前作为回调执行。
https://stackoverflow.com/questions/61227438
复制相似问题