This repository has been archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
ShowOffEmoji.cs
161 lines (137 loc) · 5.24 KB
/
ShowOffEmoji.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Text;
using UnityEngine.UI;
public class ShowOffEmoji : MonoBehaviour {
public TextAsset textAsset;
public Text bicycleAndUSFlagText;
public Text footballText;
public Text receipeText;
public RawImage rawImageToClone;
private Dictionary<string, Rect> emojiRects = new Dictionary<string, Rect>();
private static char emSpace = '\u2001';
void Awake()
{
#if !UNITY_EDITOR && UNITY_WEBGL
WebGLInput.captureAllKeyboardInput = false;
#endif
}
// Use this for initialization
void Start ()
{
this.ParseEmojiInfo(this.textAsset.text);
StartCoroutine(this.SetUITextThatHasEmoji(this.bicycleAndUSFlagText, "bicyclist: \U0001F6B4, and US flag: \U0001F1FA\U0001F1F8"));
StartCoroutine(this.SetUITextThatHasEmoji(this.footballText, "⚽ ➕ ❤ = I love football"));
StartCoroutine(this.SetUITextThatHasEmoji(this.receipeText, "\U0001F3B5 \U0001F3B6 \U0001F3B7 \U0001F3B8 \U0001F3B9 \U0001F3BA"));
}
private static string GetConvertedString(string inputString)
{
string[] converted = inputString.Split('-');
for (int j = 0; j < converted.Length; j++)
{
converted[j] = char.ConvertFromUtf32(Convert.ToInt32(converted[j], 16));
}
return string.Join(string.Empty, converted);
}
private void ParseEmojiInfo(string inputString)
{
using (StringReader reader = new StringReader(inputString))
{
string line = reader.ReadLine();
while (line != null && line.Length > 1)
{
// We add each emoji to emojiRects
string[] split = line.Split(' ');
float x = float.Parse(split[1], System.Globalization.CultureInfo.InvariantCulture);
float y = float.Parse(split[2], System.Globalization.CultureInfo.InvariantCulture);
float width = float.Parse(split[3], System.Globalization.CultureInfo.InvariantCulture);
float height = float.Parse(split[4], System.Globalization.CultureInfo.InvariantCulture);
this.emojiRects[GetConvertedString(split[0])] = new Rect(x, y, width, height);
line = reader.ReadLine();
}
}
}
private struct PosStringTuple
{
public int pos;
public string emoji;
public PosStringTuple(int p, string s)
{
this.pos = p;
this.emoji = s;
}
}
public void SetReceipeTextFromJavascript(string input)
{
foreach (Transform child in receipeText.transform)
{
Destroy(child.gameObject);
}
StartCoroutine(this.SetUITextThatHasEmoji(this.receipeText, input));
}
public IEnumerator SetUITextThatHasEmoji(Text textToEdit, string inputString)
{
List<PosStringTuple> emojiReplacements = new List<PosStringTuple>();
StringBuilder sb = new StringBuilder();
int i = 0;
while (i < inputString.Length)
{
string singleChar = inputString.Substring(i, 1);
string doubleChar = "";
string fourChar = "";
if (i < (inputString.Length - 1))
{
doubleChar = inputString.Substring(i, 2);
}
if (i < (inputString.Length - 3))
{
fourChar = inputString.Substring(i, 4);
}
if (this.emojiRects.ContainsKey(fourChar))
{
// Check 64 bit emojis first
sb.Append(emSpace);
emojiReplacements.Add(new PosStringTuple(sb.Length - 1, fourChar));
i += 4;
}
else if (this.emojiRects.ContainsKey(doubleChar))
{
// Then check 32 bit emojis
sb.Append(emSpace);
emojiReplacements.Add(new PosStringTuple(sb.Length - 1, doubleChar));
i += 2;
}
else if (this.emojiRects.ContainsKey(singleChar))
{
// Finally check 16 bit emojis
sb.Append(emSpace);
emojiReplacements.Add(new PosStringTuple(sb.Length - 1, singleChar));
i++;
}
else
{
sb.Append(inputString[i]);
i++;
}
}
// Set text
textToEdit.text = sb.ToString();
yield return null;
// And spawn RawImages as emojis
TextGenerator textGen = textToEdit.cachedTextGenerator;
// One rawimage per emoji
for (int j = 0; j < emojiReplacements.Count; j++)
{
int emojiIndex = emojiReplacements[j].pos;
GameObject newRawImage = GameObject.Instantiate(this.rawImageToClone.gameObject);
newRawImage.transform.SetParent(textToEdit.transform);
Vector3 imagePos = new Vector3(textGen.verts[emojiIndex * 4].position.x, textGen.verts[emojiIndex * 4].position.y, 0);
newRawImage.transform.localPosition = imagePos;
RawImage ri = newRawImage.GetComponent<RawImage>();
ri.uvRect = emojiRects[emojiReplacements[j].emoji];
}
}
}