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

165 lines
5.9 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.IO;
using RenderStreaming;
2026-05-19 22:40:52 +08:00
using Script.Util;
using Stary.Evo;
using UnityEngine;
using UnityEngine.UI;
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;
2026-05-19 22:40:52 +08:00
private MainPanel.UsersItem _usersItem;
private void OnDestroy()
{
StopAllCoroutines();
_isCountingDown = false;
}
public IArchitecture GetArchitecture()
{
return MainArchitecture.Interface;
}
2026-05-19 22:40:52 +08:00
public void SetData(MainPanel.UsersItem item, MeetingContacts meetingContactsController)
{
_usersItem = item;
_meetingContacts = meetingContactsController;
///头像赋值
if (string.IsNullOrEmpty(item.avatar))
{
2026-05-19 22:40:52 +08:00
var randomColor = WebRTCUtil.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>();
2026-05-19 22:40:52 +08:00
if (imageComponent != null) WebRTCUtil.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);
}
}
}