8 public class Marquee : MonoBehaviour {
9 [SerializeField]
private Integer charactersPerSecond =
default;
10 [SerializeField]
private Integer repeats =
default;
11 [SerializeField]
private String showing =
default;
12 [SerializeField]
private Trigger showingComplete =
default;
15 private Fiber display;
17 private Scroller scroller;
18 private string textToDisplay;
22 void show(Emitter emitter) {
23 var text = showing.Value;
24 if (
string.IsNullOrEmpty(text))
return;
30 float pixelsPerSecond = 0;
32 void prepare(Fiber fiber) {
33 if (
string.IsNullOrEmpty(textToDisplay)) {
38 content.text = textToDisplay;
39 content.resizeTextMaxSize = content.cachedTextGenerator.fontSizeUsedForBestFit;
40 var rect = content.rectTransform;
41 rect.SetSizeWithCurrentAnchors(axis: RectTransform.Axis.Horizontal, size: content.preferredWidth);
43 Vector3[] corners =
new Vector3[4];
44 rect.GetWorldCorners(corners);
45 float pixelsWide = corners[2].x - corners[0].x;
46 float pixelsPerCharacter = (pixelsWide / textToDisplay.Length);
47 pixelsPerSecond = charactersPerSecond * pixelsPerCharacter;
50 void step(Fiber fiber) {
51 if (!scroller.Step(pixelsPerSecond * Time.fixedUnscaledDeltaTime) || (repeat == 0)) fiber.Break();
54 display = Fiber.Instance.Do(prepare).Begin.Begin.Do(step).Again.Until(_ => --repeat <= 0);
55 display.OnComplete.Listen(_ => showingComplete.Fire());
56 showing.Emitter.Listen(show);
57 if (showing.Emitter.Firings > 0) show(showing.Emitter);
60 private void OnDestroy() => display.Dispose();
63 protected internal void Start() {
64 RectTransform viewport = GetComponent<RectTransform>();
65 content = GetComponentInChildren<Text>();
67 scroller =
new Scroller(content: content.rectTransform, viewport: viewport) {
68 StepSize = {x = -1, y = 0}
73 protected internal void OnDisable() {
void Awake()
hook into emitters for displaying text and recording completion then prepare the visual ...
Visual object to display a message scrolling across the screen