【问题标题】:How to get current frame from Animated Tile/Tilemap.animationFrameRate in Unity如何从 Unity 中的 Animated Tile/Tilemap.animationFrameRate 获取当前帧
【发布时间】:2020-05-13 23:01:54
【问题描述】:

我正在统一使用 2dExtras 中的瓷砖地图和动画瓷砖。

我的瓦片有 6 帧,速度为 2f,而瓦片地图的帧率为 2。

放置的新图块始终从第 1 帧开始,然后立即跳转到已放置的其他图块的当前帧,图块地图使每个图块保持相同的速度,这是我想要的工作。

但是我希望新放置的图块从其他图块当前所在的帧开始(而不是放置从第 1 帧跳到第 4 帧的图块)我希望新图块从第 4 帧开始

我已经找到了如何选择我想要开始的帧,但是我在检索动画当前所在的帧时遇到了问题,所以我想知道如何准确地访问给定瓷砖地图的当前动画帧(或者给定的图块,我可以创建一个虚拟图块并从中读取信息,如何获取动画图块的当前帧?)

动画 tilemaps 功能似乎缺少检索此信息的功能,当我尝试 tilemap.getsprite 时,它​​总是返回序列的第一帧(不返回当前显示的精灵),而且似乎没有是从 tilemap.animationFrameRate 轮询信息的任何方法。

我认为另一种方法是设置时钟并将其与动画的速率同步,但由于我无法获得确切的帧速率持续时间,因此时钟最终会不同步。

任何帮助将不胜感激!

【问题讨论】:

  • 你可以尝试使用 AnimationState.normalizedTime
  • 似乎找不到访问磁贴动画状态的方法,我该如何准确地实现呢? tilemap 和 tilebase 似乎都没有 animationstate 属性

标签: unity3d


【解决方案1】:

我找到了解决这个问题的方法。但这不是 100% 的保险。 首先,我使用了 SuperTile2Unity。这似乎不是重点。

private void LateUpdate()
{
    // I use this variable to monitor the run time of the game
    this.totalTime += Time.deltaTime;
}

private void func()
{
    // ...
    TileBase[] currentTiles = tilemap.GetTilesBlock(new BoundsInt(new Vector3Int(0, 0, 0), new Vector3Int(x, y, 1)));

    Dictionary<string, Sprite> tempTiles = new Dictionary<string, Sprite>();
    //I use SuperTiled2Unity. But it doesn't matter, the point is to find animated tile
    foreach (SuperTiled2Unity.SuperTile tile in currentTiles)
    {
        if (tile == null)
        {
            continue;
        }

        if (tile.m_AnimationSprites.Length > 1 && !tempTiles.ContainsKey(tile.name))
        {
            // find animated tile current frame
            // You can easily find that the way SuperTile2Unity is used to process animation is to generate a sprite array based on the time of each frame set by Tiled animation and the value of AnimationFrameRate parameter.
            // The length of array is always n times of AnimationFrameRate. You can debug to find this.
            tempTiles.Add(tile.name, tile.m_AnimationSprites[GetProbablyFrameIndex(tile.m_AnimationSprites.Length)]);
        }
    }
    //...
}

private int GetProbablyFrameIndex(int totalFrame)
{
    //According to the total running time and the total length of  tile animation and AnimationFrameRate, the approximate frame index can be deduced.
    int overFrameTime = (int)(totalTime * animationFrameRate);

    return overFrameTime % totalFrame;
}

我做了一些测试。至少在 30 分钟内,动画不会出现偏差,但可能存在临界值。如果超过临界时间,可能会出现错误。这取决于 AnimationFrameRate 的大小和 totalTime 的累积方式。毕竟,我们不知道统一何时以及如何处理animatedTile。

【讨论】:

    【解决方案2】:

    您可以尝试使用 [1] 中介绍的实现,如下所示: MyAnimator.GetCurrentAnimatorClipInfo(0)[0].clip.length * (MyAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime % 1) * MyAnimator.GetCurrentAnimatorClipInfo(0)[0].clip.frameRate;

    [1]https://gamedev.stackexchange.com/questions/165289/how-to-fetch-a-frame-number-from-animation-clip

    【讨论】:

    • 问题是瓷砖和瓷砖地图都没有动画师或动画剪辑
    猜你喜欢
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多