Files
webRtc/Assets/Script/MainPanel/ContactEntryController.cs

201 lines
7.1 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.IO;
using RenderStreaming;
using Stary.Evo;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;
namespace Script
{
public class ContactEntryController : MonoBehaviour, IController
{
private Transform _background;
private Transform _backgroundName;
private GameObject _confirmBtnTime;
private Text _confirmBtnTimeText;
private Button _confirmButton;
private bool _isCountingDown;
private MeetingContacts _meetingContacts;
private Transform _messageText;
private Transform _nameText;
private MeetingContacts.UsersItem _usersItem;
private void OnDestroy()
{
StopAllCoroutines();
_isCountingDown = false;
}
public IArchitecture GetArchitecture()
{
return MainArchitecture.Interface;
}
public void SetData(MeetingContacts.UsersItem item, MeetingContacts meetingContactsController)
{
_usersItem = item;
_meetingContacts = meetingContactsController;
///头像赋值
if (string.IsNullOrEmpty(item.avatar))
{
var randomColor = GetRandomColor();
_background = transform.Find("headBackground");
if (_background != null)
{
var image = _background.GetComponent<Image>();
if (image != null) image.color = randomColor;
}
_backgroundName = _background.transform.Find("Name");
if (_backgroundName != null)
{
var textComponent = _backgroundName.GetComponent<Text>();
if (textComponent != null && !string.IsNullOrEmpty(item.name))
textComponent.text = item.name.Substring(0, 1);
}
}
else
{
_background = transform.Find("headBackground");
if (_background != null)
{
var imageComponent = _background.GetComponent<Image>();
if (imageComponent != null) DownloadAndSetAvatar(item.avatar, imageComponent);
}
_backgroundName = _background.transform.Find("Name");
if (_backgroundName != null) _backgroundName.GetComponent<Text>().text = "";
}
if (!string.IsNullOrEmpty(item.name))
_nameText = transform.Find("Name");
if (_nameText != null)
{
var textComponent = _nameText.GetComponent<Text>();
if (textComponent != null) textComponent.text = item.name;
}
if (!string.IsNullOrEmpty(item.userId))
{
_messageText = transform.Find("Message");
if (_messageText != null)
{
var textComponent = _messageText.GetComponent<Text>();
if (textComponent != null) textComponent.text = item.userId;
}
}
if (!string.IsNullOrEmpty(item.role))
{
var role = item.role;
var idle = transform.Find("Name/state/idle");
var participant = transform.Find("Name/state/participant");
var host = transform.Find("Name/state/host");
if (role.Equals("host"))
{
host.gameObject.SetActive(true);
participant.gameObject.SetActive(false);
idle.gameObject.SetActive(false);
}
else if (role.Equals("participant"))
{
participant.gameObject.SetActive(true);
host.gameObject.SetActive(false);
idle.gameObject.SetActive(false);
}
else if (role.Equals("idle"))
{
idle.gameObject.SetActive(true);
host.gameObject.SetActive(false);
participant.gameObject.SetActive(false);
}
}
_confirmButton = transform.Find("callBtn").GetComponent<Button>();
_confirmButton.onClick.AddListener(OnClickEntry);
_confirmBtnTime = _confirmButton.transform.Find("timebg").gameObject;
_confirmBtnTimeText = _confirmBtnTime.transform.Find("Text").GetComponent<Text>();
_confirmBtnTime.gameObject.SetActive(false);
}
private void OnClickEntry()
{
if (_isCountingDown)
{
Debug.Log("倒计时进行中,无法点击");
return;
}
Debug.Log($"点击了联系人: {_usersItem.name}");
// 这里可以添加点击联系人后的逻辑,比如打开聊天窗口
// 或者发送邀请请求等
_meetingContacts.ClickContactEntry(_background.GetComponent<Image>().sprite,
_backgroundName.GetComponent<Text>().text, _usersItem);
_meetingContacts.CurrentEntry = this;
}
public void StartCountdown()
{
if (_isCountingDown) return;
_isCountingDown = true;
_confirmButton.interactable = false;
_confirmBtnTime.gameObject.SetActive(true);
StartCoroutine(CountdownCoroutine(50));
}
private IEnumerator CountdownCoroutine(int seconds)
{
Debug.Log($"开始{seconds}秒倒计时");
for (var i = seconds; i >= 0; i--)
{
_confirmBtnTimeText.text = i.ToString();
Debug.Log($"倒计时: {i}秒");
yield return new WaitForSeconds(1);
}
Debug.Log("倒计时结束");
_isCountingDown = false;
_confirmButton.interactable = true;
_confirmBtnTime.gameObject.SetActive(false);
}
private Color GetRandomColor()
{
return new Color(
Random.Range(0.2f, 0.8f),
Random.Range(0.2f, 0.8f),
Random.Range(0.2f, 0.8f),
1f
);
}
private async void DownloadAndSetAvatar(string avatarUrl, Image targetImage)
{
try
{
var tempPath = Path.Combine(Application.temporaryCachePath, $"avatar_{Guid.NewGuid()}.png");
var result = await WebRequestSystem.GetFile(avatarUrl, tempPath);
if (result.code == 200)
{
var bytes = File.ReadAllBytes(tempPath);
var texture = new Texture2D(2, 2);
texture.LoadImage(bytes);
var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),
Vector2.one * 0.5f);
targetImage.sprite = sprite;
File.Delete(tempPath);
}
}
catch (Exception e)
{
Debug.LogError($"下载头像失败: {e.Message}");
}
}
}
}