首页 > 解决方案 > 我的礼物动画完成后如何添加随机游戏对象?

问题描述

按下按钮时,随机选择游戏对象并开始礼物动画,完成后它应该看起来像图像中的那个。我希望我的随机游戏对象出现在 ?????? 动画结束时。问题是我不知道怎么做。还有一件事,我如何阻止按钮直到礼物动画完成,我一次只希望打开 1 个战利品箱。

礼物图片

public class GiftValue
{
    public int GValue;
    public int GWeight;

    public GiftValue(int gvalue, int gweight)
    {
        GValue = gvalue;
        GWeight = gweight;
    }
}
public List<GiftValue> GiftwithWeight = new List<GiftValue>
{
    new GiftValue(1,        25),
    new GiftValue(2,        25),
    new GiftValue(3,        25),
    new GiftValue(4,        20),
    new GiftValue(5,        5),
};

private readonly List<int> _GiftList = new List<int>();
public Animator OpenGiftAnimation;
public Text txtSellGiftAmount;
private int GiftAmount=10; 

private void Start()
{
    foreach (GiftValue kvp in GiftwithWeight)
    {
        for (int i = 0; i < kvp.GWeight; i++)
        {
            _GiftList.Add(kvp.GValue);
        }
    }

}
public int GetRandomNumber()
{
    // get a random inxed from 0 to 99
    int randomIndex = Random.Range(0, _GiftList.Count);
    // get the according value

    return _GiftList[randomIndex];

}
public void OpenGiftBtn()
{
    if(GiftAmount>0)
    {

        OpenGiftAnimation.Play("OpenAnimation", -1, 0f);
        int itemGiftNumber = GetRandomNumber();
        int itemGiftIndex = GiftwithWeight.FindIndex(w => w.GValue == itemGiftNumber);
        GiftAmount--;
        txtSellGiftAmount.text = GiftAmount.ToString();
        Debug.Log($"The odds for this were {GiftwithWeight[itemGiftIndex].GWeight / 100f:P} !");


    }

}

标签: c#unity3d

解决方案


使用动画师和随机项目的最佳实践 - 它只是在动画开始时生成您的对象并仅在需要时显示它。例如,您可以在 animator 中使用下一个流程(此外,您可以在更改 animator 状态时显示/隐藏“Claim”/等按钮):

在此处输入图像描述


推荐阅读