powerfun-unity/Assets/LongMessageScript.cs

79 lines
2.3 KiB
C#
Raw Normal View History

2022-07-06 17:48:55 +08:00
using Assets.Scripts;
using DG.Tweening;
2022-01-24 15:36:18 +08:00
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()
{
}
2022-07-06 17:48:55 +08:00
float timer = 1;
2022-01-24 15:36:18 +08:00
public float? textWidth = null;
private bool isAnimated = false;
2022-07-06 17:48:55 +08:00
Transform content;
2022-01-24 15:36:18 +08:00
public void Initial(string txt)
{
2022-07-06 17:48:55 +08:00
content = transform.Find("TextContainer/Content");
content.GetComponent<Text>().text = txt;
LayoutRebuilder.ForceRebuildLayoutImmediate(content.GetComponent<RectTransform>());
content.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;
2022-07-06 17:48:55 +08:00
float speed = 10f;
float datatimer = 300f;
2022-01-24 15:36:18 +08:00
#if UNITY_STANDALONE_WIN
float maxWidth = 1087;
#else
float maxWidth = 350;
#endif
2022-07-06 17:48:55 +08:00
private bool animating = false;
2022-01-24 15:36:18 +08:00
// Update is called once per frame
void Update()
{
2022-07-06 17:48:55 +08:00
//动画
2022-01-24 15:36:18 +08:00
timer -= Time.deltaTime;
if (timer <= 0)
{
2022-07-06 17:48:55 +08:00
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;
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
}
}