Use UI with an update loop or a reactive style?

What is the paradigm in Unity when it comes to generating UI from data? There are two ways in my perspective:

  • Rerender UI on every frame using the underlying state that determines it
  • Rerender UI when something changed, either through an event on state difference detection

Which one is better in terms of performance and fit for a multiplayer game?

If you are worrying about UI performance before you have your game running, you’re doing it wrong. UI is rarely a bottleneck in any case, and only the profiler can tell you what is the bottleneck.

When working with UnityEngine.UI objects, generally you put them in scene and modify their properties over time. By leaving them alone there may be a mesh generation benefit, but again you shouldn’t even REMOTELY care about that at this stage. Write the game first. Seriously.

That’s my bad. What is meant is productivity rather than performance. If it is a loop it just works in the background and I don’t have to think about it but if it is reactive then I have to think about edge cases. Also, for some reason when I am using a loop, the UI animation doesn’t work. Is this “normal”? What can be done to make the animation work?

Here is the loop code:

        var itemList = Registry.Instance.itemList;
        // Discard all the old items
        foreach(Transform itemTransform in itemList.transform)
        {
            Destroy(itemTransform.gameObject);
        }
        // Render new items
        for (int i = 0; i < 10; i++)
        {
            var item = new World.Item
            {
                Name = World.Item.Types.Name.Branch,
                Quantity = 13,
            };
            var newItem = Inventory.Utils.CreateInventoryItem(item);
            newItem.transform.SetParent(itemList.transform, false);
        }

@Kurt-Dekker , by modifying properties over time you mean changing the inventory object every frame (loop) or just changing on events?

Unity’s animations are attached by object name, as named precisely in the hierarchy. When you destroy and remake these, are you matching the names to the animation targeted properties?

This is an example piece of YAML from inside a Unity animation asset:

AnimationClip:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_Name: ANIM_GiftBox_Open

In the above anim, if there is no GameObject named ANIM_GiftBox_Open the animation won’t do anything.

@Kurt-Dekker I am not sure because I use a UI package. Should this be important if the object I am instantiating is self-sufficient? What I mean is that it works without setting anything in particular (including animations) on instantiation because it inherits everything from a prefab.

I think what is happening is that the animation is simply being interrupted by instantiating inventory items on every frame. How does this sound to you?

I can’t speak to what is actually happening, but it is an unusual approach to create stuff and destroy it every frame, and that may have consequences to such things like animation timing or state machine flow, particularly if those things are in the object you’re destroying / remaking.

You may want to spend some time studying some of the Unity tutorials on UI and animation because this is really well-travelled ground, something has a bunch of well-established best practice approaches in Unity.

1 Like