107 lines
3.6 KiB
C#
107 lines
3.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Games.ChatHistory;
|
|
|
|
public class ChatLinkImage : ChatLinkBase
|
|
{
|
|
public const string InputStr = "#{0}";
|
|
public const string _ReplaceEmojiStr = "<color=#00000000><quad material=0 size={0} x=0.1 y=0.1 width={1} height={2} /></color>";
|
|
|
|
|
|
private string _ImgName = "";
|
|
private int _Width = 22;
|
|
private int _Height = 22;
|
|
|
|
private Image _Image = null;
|
|
private RectTransform _ImageTransform;
|
|
|
|
public static string GetLinkImageStr(string imgName, int width, int heigh)
|
|
{
|
|
var strSend = StrSendStart + ((int)ChatLinkType.Image).ToString() + StrSplit + imgName + StrSplit + width + StrSplit + heigh + StrSendEnd;
|
|
return strSend;
|
|
}
|
|
|
|
public override void SetLinkBySendStr(Text text, ChatHistoryItem chatHistory, string linkStr, string[] linkParams)
|
|
{
|
|
base.SetLinkBySendStr(text, chatHistory, linkStr, linkParams);
|
|
|
|
StrSend = linkStr;
|
|
StrInput = linkStr;
|
|
if (linkParams.Length != 4)
|
|
return;
|
|
|
|
_ImgName = linkParams[1];
|
|
_Width = int.Parse(linkParams[2]);
|
|
_Height = int.Parse(linkParams[3]);
|
|
|
|
if (_EmojiWidthFixed < 0)
|
|
{
|
|
_EmojiWidthFixed = UIManager.Instance().UICanvasRect.sizeDelta.x / Screen.width;
|
|
}
|
|
_EmojiWidthFixed = 1;
|
|
//_EmojiID = int.Parse(linkParams[1]);
|
|
CreateImageItem(text);
|
|
if (_Image != null)
|
|
{
|
|
_ImageTransform = _Image.GetComponent<RectTransform>();
|
|
float emojiWidth = _Width * _EmojiWidthFixed;
|
|
float emojiHeight = _Height * _EmojiWidthFixed;
|
|
float quadWidth = 0.5f;
|
|
float quadHeight = 0.5f;
|
|
StrShow = string.Format(_ReplaceEmojiStr, _Width, quadWidth, quadHeight);
|
|
}
|
|
}
|
|
|
|
public override void SetLinkAfterLayout(Text text)
|
|
{
|
|
base.SetLinkAfterLayout(text);
|
|
if (_Image == null)
|
|
return;
|
|
_Image.gameObject.SetActive(true);
|
|
_ImageTransform.anchoredPosition = new Vector2(
|
|
text.cachedTextGeneratorForLayout.characters[StartPosInShowText + 17].cursorPos.x * _WidthFixed,
|
|
text.cachedTextGeneratorForLayout.characters[StartPosInShowText + 17].cursorPos.y * _HeightFixed);
|
|
|
|
//_EmojiTransform.sizeDelta = new Vector2(_EmojiBaseSize.x / _WidthFixed, _EmojiBaseSize.y / _HeightFixed);
|
|
_ImageTransform.sizeDelta = new Vector2(_Width, _Height);
|
|
//_EmojiTransform.sizeDelta = new Vector2(_EmojiBaseSize.x, _EmojiBaseSize.y);
|
|
}
|
|
|
|
private void CreateImageItem(Text text)
|
|
{
|
|
ChatInfoLinkMask maskLink = null;
|
|
GameObject maskObj = null;
|
|
_Image = ResourcePool.Instance.GetIdleUIItem<Image>("UI/ChatImage");
|
|
if (_Image != null)
|
|
{
|
|
maskObj = maskLink.gameObject;
|
|
}
|
|
else
|
|
{
|
|
maskObj = ResourceManager.InstantiateResource("UI/ChatImage", "ChatImage") as GameObject;
|
|
_Image = maskObj.GetComponent<Image>();
|
|
}
|
|
|
|
//var emojiGO = GameObject.Instantiate(emoji) as GameObject;
|
|
maskObj.transform.SetParent(text.transform);
|
|
maskObj.transform.localScale = Vector3.one;
|
|
maskObj.transform.localPosition = Vector3.zero;
|
|
maskObj.transform.localRotation = Quaternion.Euler(Vector3.zero);
|
|
maskObj.gameObject.SetActive(false);
|
|
|
|
LoadAssetBundle.Instance.SetImageSprite(_Image, _ImgName);
|
|
}
|
|
|
|
public override void ClearLink()
|
|
{
|
|
base.ClearLink();
|
|
if (_Image == null)
|
|
return;
|
|
|
|
GameObject.Destroy(_Image);
|
|
}
|
|
|
|
}
|