using Assets.Scripts; using DG.Tweening; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class LongMessageScript : MonoBehaviour { // Start is called before the first frame update float startX; private void Awake() { startX = transform.Find("TextContainer/Content").localPosition.x; #if (UNITY_ANDROID || UNITY_IOS) UIManager.AddEvent(transform.Find("BtnClose").gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, b => { gameObject.SetActive(false); }); #endif } void Start() { } float timer = 1; public float? textWidth = null; private bool isAnimated = false; Transform content; public void Initial(string txt) { content = transform.Find("TextContainer/Content"); content.GetComponent().text = txt; LayoutRebuilder.ForceRebuildLayoutImmediate(content.GetComponent()); content.localPosition = new Vector2(startX + maxWidth, transform.Find("TextContainer/Content").localPosition.y); isAnimated = false; timer = 2; } float playtime = 0; float speed = 10f; float datatimer = 300f; #if UNITY_STANDALONE_WIN float maxWidth = 1087; #else float maxWidth = 350; #endif private bool animating = false; // Update is called once per frame void Update() { //动画 timer -= Time.deltaTime; if (timer <= 0) { if (animating) return; var currentWith = ((RectTransform)content).rect.width; animating = true; var wait = Mathf.Max(Mathf.Floor(currentWith / speed), 2f); var tween = content.DOLocalMoveX(-currentWith, wait).SetEase(Ease.Linear); tween.onComplete += () => { //初始化位置 content.transform.localPosition = new Vector3(maxWidth, 0, 0); animating = false; }; datatimer += 5f; } } void returnAni() { var txtTransform = transform.Find("TextContainer/Content"); txtTransform.localPosition = new Vector2(startX + maxWidth, transform.Find("TextContainer/Content").localPosition.y); //transform.Find("TextContainer/Content").DOLocalMoveX(startX, playtime); } }