[Unity Learning] UI playback system based entirely on Ultimate Replay 2.0

foreword

The previous two sections have introduced the UI playback system combined with JSON and Ultimate Replay 2.0 that I used in the project. It was a little adjustment I made when the project structure was special and the code was not easy to change. But in the development process of the past few days, I found that usually only Ultimate Replay 2.0 can be used to achieve UI playback in most cases.

For the case of fixed UI real-time update

This has been introduced in the JSON-based UI playback system (double speed) (2) .

The case of real-time updating of the UI for code generation

In this case, for example, ScrollRect displays scrolling messages in real time, and code is needed to generate UI elements in real time. Here, ReplayBehaviour in Ultimate Replay 2.0 is needed, and scripts that need to keep running during playback need to inherit the ReplayBehaviour base class , the method that needs to be played back should be modified with the [ReplayMethod] feature, and the RecordMethodCall method is used to call the method that needs to be played back during the recording process, so as to record the calling time of this method for playback, as follows:

using System.Collections;
using System.Collections.Generic;
using UltimateReplay;
using UnityEngine;
using UnityEngine.UI;

public class ReplayUIByUR2 : ReplayBehaviour
{
    
    
    [ReplayVar]public string textA;
    [ReplayVar] public int textB;
    public Text textPrefab;
    public RectTransform contentRoot;
    public MScrollRect scrollRect;
    private float interval = 1.5f;
    private float time = 0;
    private string testText;

    private void Update()
    {
    
    
        if (IsRecording)
        {
    
    
            time += Time.deltaTime;
            if (time >= interval)
            {
    
    
                textA = "测试:" + Random.Range(0, 10).ToString();
                textB = Random.Range(0, 10);
                testText = Time.timeSinceLevelLoad.ToString();
                RecordMethodCall(AddText,testText);
                time = 0;
            }
        }
    }

    [ReplayMethod]
    public void AddText(string str)
    {
    
    
        UIHelper.AddScrollText(textPrefab, contentRoot, str, scrollRect);
    }
}

However, the UI generated by the code played back by this method cannot be automatically destroyed. It is necessary to manually destroy the generated dynamic UI elements before each recording and playback. As shown below, this method is called before Record and Replay:

    private void ClearUIData()
    {
    
    
        for (int i = 0; i < uiContentRoot.childCount;i++)
        {
    
    
            GameObject obj = uiContentRoot.GetChild(i).gameObject;
            Destroy(obj);
        }
    }

The effect is as follows:
Please add a picture descriptionNote:

  • Sometimes when using the ReplayMethod playback method, a certain operation will be played back repeatedly. For example, the scrolling UI only updates the data of 85.252, but the number of 85.252 is played back four or five times during playback. This situation has not yet been resolved, but fortunately it is This happens only in rare cases.
  • The .replay file used for playback cannot be placed in the StreamingAssets folder, or a warning will be reported when recording, saying that we repeatedly write empty data to a certain file.

Demo download

Demo

Guess you like

Origin blog.csdn.net/dong89801033/article/details/127859551