首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iTween MoveTo并同时设置一个新的父级。

iTween MoveTo并同时设置一个新的父级。
EN

Stack Overflow用户
提问于 2020-04-15 11:21:32
回答 1查看 364关注 0票数 0

我正在尝试建立一个纸牌游戏,我想把卡片从家长A到父母B动画。最初,卡的父母是父A。是否可以使用i吐温的MoveTo方法将卡从父A区域移动到父B区域,并将父B设置为其父?

代码语言:javascript
复制
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方法中,我将卡片从表区域移动到甲板区域。最后,我把父母安排在甲板上。

代码语言:javascript
复制
[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++;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-15 13:05:31

请参阅SetParent

可选参数

worldPositionStays 如果是true,则修改父相对位置、比例和旋转,使物体保持与以前相同的世界空间位置、旋转和尺度。

worldPositionStays参数的默认值为true。

代码语言:javascript
复制
card.transform.SetParent(deck.transform, false);

使对象被放置到新的父中,保持相对位置

您想要的是保持的世界位置,所以传递给true (或者没有什么,因为true是默认的)

代码语言:javascript
复制
card.transform.SetParent(deck.transform);

等于

代码语言:javascript
复制
card.transform.SetParent(deck.transform, true);

等于简单地分配Transform.parent

代码语言:javascript
复制
card.transform.parent = deck.transform;

现在看一下您的方法:

代码语言:javascript
复制
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完成或启动之前作为回调执行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61227438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档