73 lines
2.2 KiB
C#
73 lines
2.2 KiB
C#
|
|
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 = 2;
|
|||
|
|
public float? textWidth = null;
|
|||
|
|
private bool isAnimated = false;
|
|||
|
|
public void Initial(string txt)
|
|||
|
|
{
|
|||
|
|
var txtTransform = transform.Find("TextContainer/Content");
|
|||
|
|
txtTransform.GetComponent<Text>().text = txt;
|
|||
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(txtTransform.GetComponent<RectTransform>());
|
|||
|
|
txtTransform.localPosition = new Vector2(startX, transform.Find("TextContainer/Content").localPosition.y);
|
|||
|
|
isAnimated = false;
|
|||
|
|
timer = 2;
|
|||
|
|
}
|
|||
|
|
float playtime = 0;
|
|||
|
|
#if UNITY_STANDALONE_WIN
|
|||
|
|
float maxWidth = 1087;
|
|||
|
|
#else
|
|||
|
|
float maxWidth = 350;
|
|||
|
|
#endif
|
|||
|
|
// Update is called once per frame
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
timer -= Time.deltaTime;
|
|||
|
|
if (timer <= 0)
|
|||
|
|
{
|
|||
|
|
var width = transform.Find("TextContainer/Content").GetComponent<RectTransform>().sizeDelta.x;
|
|||
|
|
if (textWidth != width)
|
|||
|
|
{
|
|||
|
|
this.textWidth = width;
|
|||
|
|
}
|
|||
|
|
//do
|
|||
|
|
if (textWidth.HasValue && textWidth > maxWidth)
|
|||
|
|
{
|
|||
|
|
var offset = textWidth.Value - maxWidth;
|
|||
|
|
var game = transform.Find("TextContainer/Content");
|
|||
|
|
var v = 30 / 0.3f;
|
|||
|
|
playtime = offset / v;
|
|||
|
|
//匀速播放
|
|||
|
|
game.DOLocalMoveX(startX - offset, playtime).onComplete
|
|||
|
|
= () => Invoke("returnAni", 1.5f);
|
|||
|
|
}
|
|||
|
|
timer += (playtime * 2f + 1.5f + 2f);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
void returnAni()
|
|||
|
|
{
|
|||
|
|
transform.Find("TextContainer/Content").DOLocalMoveX(startX, playtime);
|
|||
|
|
}
|
|||
|
|
}
|