powerfun-unity/Assets/MessageScript.cs

156 lines
4.3 KiB
C#

using Assets.Scenes.Ride.Scripts;
using Assets.Scripts;
using DG.Tweening;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
public class MessageScript : MonoBehaviour
{
public GameObject _message;
RectTransform content;
List<MessageModel> data = new List<MessageModel>();
bool animating = false;
float timer = 1f;
float speed = 50f;
float datatimer = 300f;
private void Awake()
{
content = (RectTransform)transform.Find("Scroll View/Viewport/Content");
//EventQueueSystem.AddListener<LinkedMessageEvent>(LinkedMessageHandler);
}
private void LinkedMessageHandler(LinkedMessageEvent e)
{
if (App.globalMessageQueue.Count == 5)
{
App.globalMessageQueue.Dequeue();
}
App.globalMessageQueue.Enqueue(e);
data.Add(new MessageModel
{
Avatar = e.avatar,
Content = "<color=#5C5C6E>" + e.nickname+"</color>"+e.content,
Type = 1,
});
}
void Start()
{
InitAsync();//初始化数据
}
float currentWith = 0f;
// Update is called once per frame
void Update()
{
//数据源
datatimer -= Time.deltaTime;
if (datatimer <= 0)
{
GetData();
datatimer += 300f;
}
//动画
timer -= Time.deltaTime;
if (timer <= 0)
{
if (animating)
return;
currentWith = 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 += () => {
//刷新内容
Refresh();
//初始化位置
content.transform.localPosition = new Vector3(1245f, 0, 0);
};
datatimer += 5f;
}
}
private async Task InitAsync()
{
await GetData();
Refresh();
}
private void Refresh()
{
Utils.DestroyChildren(content.transform);
//创建新的内容
foreach (var item in data)
{
if (!item.OutDated && item.Type != 0 || item.Type == 0)
{
var cell = Instantiate<GameObject>(_message, content);
var headImage = cell.transform.Find("bg/Avatar").GetComponent<Image>();
var text = cell.transform.Find("Message").GetComponent<Text>();
//创建头像
if (!string.IsNullOrEmpty(item.Avatar))
{
Utils.DisplayImage(headImage, item.Avatar);
}
//创建消息
text.GetComponent<Text>().text = item.Content;
cell.SetActive(true);
LayoutRebuilder.ForceRebuildLayoutImmediate(content);
item.OutDated = true;
}
}
LayoutRebuilder.ForceRebuildLayoutImmediate(content);
//释放动画锁
animating = false;
}
private async Task GetData()
{
foreach (var item in data)
{
if (item.Type == 0)
{
data.Remove(item);
}
}
var res = await ConfigHelper.mapApi.GetMaxRanking();
if (res.result)
{
var maxList = res.data;
foreach (var item in maxList)
{
data.Add(new MessageModel
{
Avatar = item.WxHeadImg,
Content = $"{item.NickName}{item.Message}",
Type = 0,
});
}
}
var r = await ConfigHelper.userApi.GetNotify();
if (r.result)
{
var info = r.data["Content"].ToString().Replace("\r\n", " ");
if (!string.IsNullOrEmpty(info))
{
data.Add(new MessageModel
{
Avatar = string.Empty,
Content = info,
Type = 0,
});
}
}
}
public class MessageModel {
public string Avatar { get; set;}
public string Content { get; set; }
public int Type { get; set; }
public bool OutDated { get; set; }
}
}