Files
webRtc/Assets/Script/MainPanel/ContactEntryController.cs
2026-05-22 15:43:00 +08:00

153 lines
5.5 KiB
C#

using System;
using System.Collections;
using System.IO;
using RenderStreaming;
using Script.Util;
using Stary.Evo;
using UnityEngine;
using UnityEngine.UI;
namespace Script
{
public class ContactEntryController : MonoBehaviour, IController
{
private Image _background;
private Text _backgroundName;
private GameObject _confirmBtnTime;
private Text _confirmBtnTimeText;
private Button _confirmButton;
private bool _isCountingDown;
private MeetingContacts _meetingContacts;
private Transform _messageText;
private Transform _nameText;
private MainPanel.UsersItem _usersItem;
private void OnDestroy()
{
StopAllCoroutines();
_isCountingDown = false;
}
public IArchitecture GetArchitecture()
{
return MainArchitecture.Interface;
}
public void SetData(MainPanel.UsersItem item, MeetingContacts meetingContactsController)
{
_usersItem = item;
_meetingContacts = meetingContactsController;
_background = transform.Find("headBackground/image").GetComponent<Image>();
_backgroundName = transform.Find("headBackground/Name").GetComponent<Text>();
///头像赋值
if (string.IsNullOrEmpty(item.avatar))
{
var randomColor = WebRTCUtil.GetRandomColor();
if (_background != null) _background.color = randomColor;
if (_backgroundName != null)
if (!string.IsNullOrEmpty(item.name))
_backgroundName.text = item.name.Substring(0, 1);
}
else
{
if (_background != null)
if (_background != null)
WebRTCUtil.DownloadAndSetAvatar(item.avatar, _background);
if (_backgroundName != null) _backgroundName.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);
}
}
}