增加自定义不是房间的消息发送

This commit is contained in:
2026-05-18 23:31:04 +08:00
parent ce9b1e85e9
commit 3daebf56ab
29 changed files with 2375 additions and 356 deletions

View File

@@ -0,0 +1,201 @@
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}");
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c16475152291466fa053f892d1ec0c8b
timeCreated: 1779100629

View File

@@ -0,0 +1,77 @@
using System;
using Stary.Evo.UIFarme;
using UnityEngine;
using UnityEngine.UI;
namespace Script
{
public class MainPanel : BasePanel
{
/// <summary>
/// 退出按钮
/// </summary>
private Button _arrowLeft;
/// <summary>
/// 房间ID
/// </summary>
private Text _idText;
private MeetingContacts _meetingContacts;
/// <summary>
/// 房间人数
/// </summary>
private Text _roomPeopleNumText;
/// <summary>
/// 时间
/// </summary>
private Text _timeText;
/// <summary>
/// 标题
/// </summary>
private Text _titleText;
public override UITweenType TweenType => UITweenType.Fade;
public override string UIPath => "Canvas";
public override void Initialize(GameObject panelGo)
{
base.Initialize(panelGo);
_titleText = panelGo.transform.Find("Header/title").GetComponent<Text>();
_timeText = panelGo.transform.Find("Header/time").GetComponent<Text>();
_arrowLeft = panelGo.transform.Find("Header/arrow-left").GetComponent<Button>();
_idText = panelGo.transform.Find("MeetingInfoCard/meeting/id").GetComponent<Text>();
_roomPeopleNumText = panelGo.transform.Find("MeetingInfoCard/numberOfPeople/number").GetComponent<Text>();
_meetingContacts = new MeetingContacts();
_meetingContacts.Initialize(panelGo.transform.Find("MeetingContacts").gameObject);
}
public override void OnEnter(Action complete = null)
{
base.OnEnter(complete);
_meetingContacts.OnEnter();
_arrowLeft.onClick.AddListener(OnArrowLeftClick);
}
public override void OnExit(float delay = 0)
{
base.OnExit(delay);
_meetingContacts.OnExit();
_arrowLeft.onClick.RemoveListener(OnArrowLeftClick);
}
private void OnArrowLeftClick()
{
}
public override void OnDestroy()
{
base.OnDestroy();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 653ec51da16e4dd786d0d20f29661d54
timeCreated: 1778848877

View File

@@ -0,0 +1,188 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using RenderStreaming;
using Stary.Evo;
using Unity.RenderStreaming;
using UnityEngine;
using UnityEngine.UI;
namespace Script
{
public class MeetingContacts : IController
{
/// <summary>
/// 联系人列表项字典
/// Key: 参与者ID
/// Value: 联系人列表项实例
/// </summary>
private readonly Dictionary<GameObject, ContactEntryController> _contactEntries = new();
private Image _avatarImage;
private Button _cancelButton;
private Button _confirmButton;
private GameObject _confirmingPop;
/// <summary>
/// 联系人列表项预制体
/// </summary>
private GameObject _contactEntryPrefab;
/// <summary>
/// 全部联系人列表容器
/// </summary>
private Transform _content;
private Text _idText;
private InputField _leaveMessage;
private Text _messageText;
private Text _nameText;
private GameObject _panelGo;
public ContactEntryController CurrentEntry { get; set; }
public IArchitecture GetArchitecture()
{
return MainArchitecture.Interface;
}
public void Initialize(GameObject panelGo)
{
_panelGo = panelGo;
_contactEntryPrefab = Resources.Load<GameObject>("ContactEntry");
_content = _panelGo.transform.Find("MeetingGrid/Viewport/Content");
//邀请界面
_confirmingPop = _panelGo.transform.Find("MeetingGrid/ConfirmingPop").gameObject;
_confirmingPop.SetActive(false);
//确认邀请
_idText = _confirmingPop.transform.Find("bg/id").GetComponent<Text>();
_leaveMessage = _confirmingPop.transform.Find("bg/leaveMessage").GetComponent<InputField>();
_confirmButton = _confirmingPop.transform.Find("bg/invite").GetComponent<Button>();
_cancelButton = _confirmingPop.transform.Find("bg/cancel").GetComponent<Button>();
_avatarImage = _confirmingPop.transform.Find("ParticipantEntry/head").GetComponent<Image>();
_nameText = _confirmingPop.transform.Find("ParticipantEntry/Name").GetComponent<Text>();
_messageText = _confirmingPop.transform.Find("ParticipantEntry/Message").GetComponent<Text>();
}
public async void OnEnter()
{
var response =
await WebRequestSystem.Get<ResponseUsers>(this.GetSystem<IGlobalConfigSystem>().IP, "/signaling/users");
if (response != null && response.totalCount > 0)
for (var i = 0; i < response.totalCount; i++)
{
var item = response.users[i];
if (string.IsNullOrEmpty(item.name) ||
string.IsNullOrEmpty(item.userId)) continue;
var entry = GameObject.Instantiate(_contactEntryPrefab, _content);
var contactEntryController = entry.GetOrAddComponent<ContactEntryController>();
contactEntryController.SetData(item, this);
}
}
public void OnExit()
{
}
public void ClickContactEntry(Sprite avatarImage, string name, UsersItem item)
{
_confirmingPop.SetActive(true);
_idText.text = this.GetSystem<IGlobalConfigSystem>().GetConnectionId();
_avatarImage.sprite = avatarImage;
var avatarName = _avatarImage.transform.Find("Name").GetComponent<Text>();
avatarName.text = name;
_nameText.text = item.name;
_messageText.text = item.userId;
_confirmButton.onClick.AddListener(() =>
{
_confirmingPop.SetActive(false);
CurrentEntry.StartCountdown();
// var avatar = _profileImage.sprite.name.Replace("(Clone)", "");
var userInfo = new
{
type = "invite-call",
data = new
{
connectionId = this.GetSystem<IGlobalConfigSystem>().GetConnectionId(),
targetSocketId = item.socketId,
targetUserId = item.userId,
inviterUserId = this.GetSystem<IGlobalConfigSystem>().GetUserId(),
inviterName = this.GetSystem<IGlobalConfigSystem>().GetConnectionName(),
inviterAvatar =
$"{this.GetSystem<IGlobalConfigSystem>().IP}/images/head/" +
$"{this.GetSystem<IGlobalConfigSystem>().GetConnectionTexture().name}.png"
}
};
SignalingMessageHelper.SendMessage(JsonConvert.SerializeObject(userInfo));
});
_cancelButton.onClick.AddListener(() =>
{
_confirmingPop.SetActive(false);
CurrentEntry = null;
});
}
public class ContactEntryData
{
public string avatar;
public GameObject entry;
public Toggle invitationTog;
public string name;
public string participantId;
public string role;
}
[Serializable]
public class ResponseUsers
{
/// <summary>
/// </summary>
public List<UsersItem> users { get; set; }
/// <summary>
/// </summary>
public int totalCount { get; set; }
}
[Serializable]
public class UsersItem
{
/// <summary>
/// </summary>
public string socketId { get; set; }
/// <summary>
/// </summary>
public string connectionId { get; set; }
/// <summary>
/// </summary>
public string participantId { get; set; }
/// <summary>
/// </summary>
public string role { get; set; }
/// <summary>
/// </summary>
public string userId { get; set; }
/// <summary>
/// </summary>
public string name { get; set; }
/// <summary>
/// </summary>
public string avatar { get; set; }
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 05ce391aaa7a4698941ba9f6b0157614
timeCreated: 1779019493