129 lines
4.5 KiB
C#
129 lines
4.5 KiB
C#
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using Games.ChatHistory;
|
|||
|
|
|||
|
public class ChatLinkEmoji :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 _EmojiID = "";
|
|||
|
private GameObject _EmojiObj = null;
|
|||
|
private RectTransform _EmojiTransform = null;
|
|||
|
private Vector2 _EmojiBaseSize = Vector2.zero;
|
|||
|
|
|||
|
//private static Dictionary<string, Stack<GameObject>> _IdleMotions = new Dictionary<string, Stack<GameObject>>();
|
|||
|
|
|||
|
public void SetLinkEmoji(int emojiID)
|
|||
|
{
|
|||
|
_EmojiID = emojiID.ToString();
|
|||
|
StrSend = StrSendStart + ((int)ChatLinkType.Emoji).ToString() + StrSplit + emojiID + StrSendEnd;
|
|||
|
StrInput = string.Format(InputStr, emojiID);
|
|||
|
}
|
|||
|
|
|||
|
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 != 2)
|
|||
|
return;
|
|||
|
|
|||
|
if (_EmojiWidthFixed < 0)
|
|||
|
{
|
|||
|
_EmojiWidthFixed = UIManager.Instance().UICanvasRect.sizeDelta.x / Screen.width;
|
|||
|
}
|
|||
|
_EmojiWidthFixed = 1;
|
|||
|
//_EmojiID = int.Parse(linkParams[1]);
|
|||
|
CreateEmojiItem(text, linkParams[1]);
|
|||
|
if (_EmojiObj != null)
|
|||
|
{
|
|||
|
_EmojiTransform = _EmojiObj.GetComponent<RectTransform>();
|
|||
|
var emojiSprite = _EmojiObj.GetComponent<Image>().sprite;
|
|||
|
_EmojiBaseSize = new Vector2(emojiSprite.rect.width, emojiSprite.rect.height);
|
|||
|
float emojiWidth = _EmojiBaseSize.x * _EmojiWidthFixed;
|
|||
|
float emojiHeight = _EmojiBaseSize.y * _EmojiWidthFixed;
|
|||
|
float quadWidth = 0.5f;
|
|||
|
float quadHeight = 0.5f;
|
|||
|
StrShow = string.Format(_ReplaceEmojiStr, emojiSprite.rect.width, quadWidth, quadHeight);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public override void SetLinkAfterLayout(Text text)
|
|||
|
{
|
|||
|
base.SetLinkAfterLayout(text);
|
|||
|
if (_EmojiObj == null)
|
|||
|
return;
|
|||
|
|
|||
|
if (text.cachedTextGenerator.characters.Count < StartPosInShowText + 18)
|
|||
|
return;
|
|||
|
|
|||
|
_EmojiTransform.anchoredPosition = new Vector2(
|
|||
|
text.cachedTextGenerator.characters[StartPosInShowText + 17].cursorPos.x * _WidthFixed,
|
|||
|
text.cachedTextGenerator.characters[StartPosInShowText + 17].cursorPos.y * _HeightFixed);
|
|||
|
|
|||
|
//Debug.Log("_EmojiTransform.anchoredPosition:" + _EmojiTransform.anchoredPosition);
|
|||
|
//if (_EmojiTransform.anchoredPosition.x >= 0 && _EmojiTransform.anchoredPosition.y <= 0)
|
|||
|
{
|
|||
|
_EmojiObj.gameObject.SetActive(true);
|
|||
|
}
|
|||
|
//_EmojiTransform.sizeDelta = new Vector2(_EmojiBaseSize.x / _WidthFixed, _EmojiBaseSize.y / _HeightFixed);
|
|||
|
_EmojiTransform.sizeDelta = new Vector2(_EmojiBaseSize.x, _EmojiBaseSize.y);
|
|||
|
//_EmojiTransform.sizeDelta = new Vector2(_EmojiBaseSize.x, _EmojiBaseSize.y);
|
|||
|
}
|
|||
|
|
|||
|
private void CreateEmojiItem(Text text, string emojiId)
|
|||
|
{
|
|||
|
_EmojiID = emojiId;
|
|||
|
var emojiTrans = ResourcePool.Instance.PullEmoji("Emoji" + emojiId, text.transform);
|
|||
|
if(emojiTrans == null)
|
|||
|
{
|
|||
|
//_EmojiObj = ResourceManager.InstantiateResource("UI/Motion/Emoji" + emojiID, "Emoji" + emojiID) as GameObject;
|
|||
|
|
|||
|
//if (_EmojiObj == null)
|
|||
|
return;
|
|||
|
}
|
|||
|
_EmojiObj = emojiTrans.gameObject;
|
|||
|
emojiTrans.localScale = Vector3.one;
|
|||
|
emojiTrans.localPosition = Vector3.zero;
|
|||
|
emojiTrans.localRotation = Quaternion.identity;
|
|||
|
_EmojiObj.gameObject.SetActive(false);
|
|||
|
}
|
|||
|
|
|||
|
public override void ClearLink()
|
|||
|
{
|
|||
|
base.ClearLink();
|
|||
|
if (_EmojiObj == null)
|
|||
|
return;
|
|||
|
|
|||
|
|
|||
|
var rectTransform = _EmojiObj.transform as RectTransform;
|
|||
|
if (rectTransform != null)
|
|||
|
//RevcEmojiObj(_EmojiObj);
|
|||
|
GameManager.gameManager.StartCoroutine((RevcEmojiObj(_EmojiObj)));
|
|||
|
else
|
|||
|
_EmojiObj.SetActive(false);
|
|||
|
}
|
|||
|
|
|||
|
private IEnumerator RevcEmojiObj(GameObject emojiObj)
|
|||
|
{
|
|||
|
yield return null;
|
|||
|
if (emojiObj != null)
|
|||
|
{
|
|||
|
ResourcePool.Instance.PushEmoji(emojiObj.transform as RectTransform);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void RemoveEmojiObj()
|
|||
|
{
|
|||
|
if (_EmojiObj != null)
|
|||
|
{
|
|||
|
ResourcePool.Instance.PushEmoji(_EmojiObj.transform as RectTransform);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|