powerfun-unity/Assets/LongMessageScript.cs

79 lines
2.6 KiB
C#
Raw Normal View History

2022-01-24 15:36:18 +08:00
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 + maxWidth, transform.Find("TextContainer/Content").localPosition.y);
2022-01-24 15:36:18 +08:00
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;
2022-01-24 15:36:18 +08:00
var game = transform.Find("TextContainer/Content");
var v = 20 / 0.3f;
2022-01-24 15:36:18 +08:00
playtime = offset / v;
//匀速播放
game.DOLocalMoveX(startX - offset, playtime).onComplete
= () =>
{
var txtTransform = transform.Find("TextContainer/Content");
txtTransform.localPosition = new Vector2(startX + maxWidth, transform.Find("TextContainer/Content").localPosition.y);
};
2022-01-24 15:36:18 +08:00
}
timer += (playtime + 0.5f);
2022-01-24 15:36:18 +08:00
}
}
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);
2022-01-24 15:36:18 +08:00
}
}