forked from DFVSQY/EmojiText
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
56 changed files
with
248 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -242,4 +242,8 @@ ModelManifest.xml | |
.paket/paket.exe | ||
|
||
# FAKE - F# Make | ||
.fake/ | ||
.fake/ | ||
/Temp | ||
*.csproj | ||
*.sln | ||
/Library |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
#if UNITY_EDITOR | ||
using UnityEditor; | ||
public class EmojiEditor | ||
{ | ||
[MenuItem("GameObject/UI/EmojiText",priority = 0)] | ||
public static void CreateEmojiText() | ||
{ | ||
Transform par = null; | ||
if(Selection.gameObjects.Length > 0) | ||
{ | ||
par = Selection.gameObjects[0].transform; | ||
if(AssetDatabase.Contains(par.gameObject)) | ||
{ | ||
par = null; | ||
} | ||
} | ||
GameObject go = new GameObject("EmojiText"); | ||
go.transform.SetParent(par); | ||
go.AddComponent<EmojiText>(); | ||
RectTransform rt = go.transform as RectTransform; | ||
rt.localScale = Vector3.one; | ||
rt.localPosition = Vector3.zero; | ||
rt.sizeDelta = new Vector3(100f,100f); | ||
EditorGUIUtility.PingObject(go); | ||
} | ||
} | ||
#endif | ||
|
||
[System.Serializable] | ||
public struct EmojiSprites | ||
{ | ||
public string key; | ||
public Sprite sprite; | ||
} | ||
|
||
[CreateAssetMenu(fileName = "EmojiData",menuName = "CreateEmojiData",order = 1)] | ||
public class EmojiData : ScriptableObject | ||
{ | ||
//public Dictionary<string,Sprite> datas = new Dictionary<string,Sprite>(); | ||
public List<EmojiSprites> datas = new List<EmojiSprites>(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
|
||
public class EmojiText : Text | ||
{ | ||
private struct EmojiStruct | ||
{ | ||
public int posIndex; | ||
public string des; | ||
|
||
public EmojiStruct(int posIndex,string des) | ||
{ | ||
this.posIndex = posIndex; | ||
this.des = des; | ||
} | ||
} | ||
|
||
private static char emSpace = '\u2001'; | ||
private VertexHelper vh; | ||
private List<EmojiStruct> emojis = new List<EmojiStruct>(); | ||
|
||
private static EmojiData _asset; | ||
private static EmojiData asset | ||
{ | ||
get | ||
{ | ||
if(_asset == null) | ||
{ | ||
_asset = Resources.Load<EmojiData>("EmojiData/EmojiData"); | ||
if(_asset == null) | ||
{ | ||
Debug.LogError("_asset is null"); | ||
} | ||
} | ||
return _asset; | ||
} | ||
} | ||
|
||
private static Dictionary<string,Sprite> _data; | ||
private static Dictionary<string,Sprite> data | ||
{ | ||
get | ||
{ | ||
if(_data == null) | ||
{ | ||
_data = new Dictionary<string,Sprite>(); | ||
List<EmojiSprites> es = asset.datas; | ||
foreach(var e in es) | ||
{ | ||
if(!_data.ContainsKey(e.key)) | ||
{ | ||
_data.Add(e.key,e.sprite); | ||
} | ||
else | ||
{ | ||
Debug.LogError("emoji repeat,key:" + e.key); | ||
} | ||
} | ||
} | ||
return _data; | ||
} | ||
} | ||
|
||
public override string text | ||
{ | ||
get | ||
{ | ||
return base.text; | ||
} | ||
|
||
set | ||
{ | ||
if(!string.IsNullOrEmpty(value)) | ||
{ | ||
value = ParserText(value); | ||
} | ||
base.text = value; | ||
StartCoroutine(ShowEmoji()); | ||
} | ||
} | ||
|
||
protected override void OnPopulateMesh(VertexHelper toFill) | ||
{ | ||
vh = toFill; | ||
base.OnPopulateMesh(toFill); | ||
} | ||
|
||
private IEnumerator ShowEmoji() | ||
{ | ||
yield return new WaitUntil(() => | ||
{ | ||
return cachedTextGenerator.vertexCount > 0; | ||
}); | ||
|
||
int count = emojis.Count; | ||
if(count > 0) | ||
{ | ||
List<UIVertex> verts = new List<UIVertex>(); | ||
vh.GetUIVertexStream(verts); | ||
for(int i = 0; i < count; i++) | ||
{ | ||
int index = emojis[i].posIndex; | ||
Image image = null; | ||
if(i >= transform.childCount) // 可复用的emoji不够时 | ||
{ | ||
GameObject go = new GameObject("emoji"); | ||
image = go.AddComponent<Image>(); | ||
go.transform.SetParent(transform); | ||
go.transform.localScale = Vector3.one; | ||
} | ||
else | ||
{ | ||
image = transform.GetChild(i).GetComponent<Image>(); | ||
} | ||
RectTransform rt = image.rectTransform; | ||
rt.gameObject.SetActive(true); | ||
rt.sizeDelta = new Vector2(fontSize,fontSize); | ||
float x = verts[index * 6].position.x + fontSize / 2; | ||
float y = verts[index * 6].position.y + fontSize / 4; | ||
rt.localPosition = new Vector3(x,y,0f); | ||
image.sprite = data[emojis[i].des]; | ||
} | ||
for(int i = count; i < transform.childCount; i++) | ||
{ | ||
Transform ch = transform.GetChild(i); | ||
ch.gameObject.SetActive(false); | ||
} | ||
} | ||
} | ||
|
||
private string ParserText(string content) | ||
{ | ||
emojis.Clear(); | ||
StringBuilder sb = new StringBuilder(); | ||
int i = 0; | ||
int length = content.Length; | ||
while(i < length) | ||
{ | ||
char c = content[i]; | ||
int end = i + 3; //[微笑] 共占据4个字符 | ||
if(end >= length || !c.Equals('[')) | ||
{ | ||
sb.Append(c); | ||
i++; | ||
} | ||
else | ||
{ | ||
string s = content.Substring(i,4); | ||
if(data.ContainsKey(s)) | ||
{ | ||
sb.Append(emSpace); | ||
emojis.Add(new EmojiStruct(sb.Length - 1,s)); | ||
i += 4; | ||
} | ||
else | ||
{ | ||
sb.Append(c); | ||
i++; | ||
} | ||
} | ||
} | ||
return sb.ToString(); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
|
||
public class Test : MonoBehaviour | ||
{ | ||
private Text _text; | ||
private Text text | ||
{ | ||
get | ||
{ | ||
if(_text == null) | ||
{ | ||
_text = transform.Find("EmojiText").GetComponent<Text>(); | ||
} | ||
return _text; | ||
} | ||
} | ||
|
||
private IEnumerator Start() | ||
{ | ||
WaitForSeconds ws = new WaitForSeconds(3f); | ||
|
||
text.text = "[大笑]哈哈[大哭]哇哇[礼物]蛋糕[玫瑰]"; | ||
yield return ws; | ||
text.text = "[饥饿]This[无语]is[调皮]a[狂汗]EmojiText[鄙视]example[高兴]"; | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
m_EditorVersion: 5.5.1f1 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.