powerfun-unity/Assets/HomeMessageController.cs

59 lines
1.6 KiB
C#
Raw Permalink Normal View History

2021-12-30 16:51:04 +08:00
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HomeMessageController : MonoBehaviour
{
// Start is called before the first frame update
float startX;
private void Awake()
{
startX = transform.Find("TextContainer/Content").localPosition.x;
}
void Start()
{
}
float timer = 2;
public float? textWidth = null;
private bool isAnimated = false;
2022-01-24 15:36:18 +08:00
public void Initial(float textWidth)
{
transform.Find("TextContainer/Content").localPosition = new Vector2(startX, transform.Find("TextContainer/Content").localPosition.y);
isAnimated = false;
2022-01-24 15:36:18 +08:00
this.textWidth = textWidth;
}
float playtime = 0;
2022-01-10 14:58:57 +08:00
#if UNITY_STANDALONE_WIN
float maxWidth = 370;
2022-01-10 14:58:57 +08:00
#else
float maxWidth = 180;
#endif
2021-12-30 16:51:04 +08:00
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
//do
2022-01-10 14:58:57 +08:00
if (textWidth.HasValue && textWidth > maxWidth && !isAnimated)
2021-12-30 16:51:04 +08:00
{
isAnimated = true;
2022-01-10 14:58:57 +08:00
var offset = textWidth.Value - maxWidth;
2021-12-30 16:51:04 +08:00
var game = transform.Find("TextContainer/Content");
var v = 20 / 0.3f;
playtime = offset / v;
//匀速播放
game.DOLocalMoveX(startX - offset, playtime).onComplete
2021-12-30 16:51:04 +08:00
= () => Invoke("returnAni", 1.5f);
}
timer += 3;
2021-12-30 16:51:04 +08:00
}
}
void returnAni()
{
transform.Find("TextContainer/Content").DOLocalMoveX(startX, playtime);
2021-12-30 16:51:04 +08:00
}
}