Skip to content

Commit

Permalink
添加了一个EmojiText使用示例
Browse files Browse the repository at this point in the history
  • Loading branch information
DFVSQY committed Feb 20, 2017
1 parent 78fd2d4 commit 9c20ffc
Show file tree
Hide file tree
Showing 56 changed files with 248 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,8 @@ ModelManifest.xml
.paket/paket.exe

# FAKE - F# Make
.fake/
.fake/
/Temp
*.csproj
*.sln
/Library
Binary file added Assets/EmojiText/Emojis/三汗.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/亲嘴.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/冷汗.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/发困.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/可爱.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/叹气.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/喷水.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/坏笑.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/大吐.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/大哭.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/大笑.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/大赞.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/奇怪.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/委屈.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/害羞.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/尴尬.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/得意.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/微笑.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/惊恐.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/惊讶.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/愤怒.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/无语.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/狂汗.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/玫瑰.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/生气.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/EmojiText/Emojis/疑问.png
Binary file added Assets/EmojiText/Emojis/睡觉.png
Binary file added Assets/EmojiText/Emojis/礼物.png
Binary file added Assets/EmojiText/Emojis/耍酷.png
Binary file added Assets/EmojiText/Emojis/调皮.png
Binary file added Assets/EmojiText/Emojis/鄙视.png
Binary file added Assets/EmojiText/Emojis/钱眼.png
Binary file added Assets/EmojiText/Emojis/饥饿.png
Binary file added Assets/EmojiText/Emojis/高兴.png
Binary file not shown.
46 changes: 46 additions & 0 deletions Assets/EmojiText/Scripts/EmojiData.cs
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>();
}

167 changes: 167 additions & 0 deletions Assets/EmojiText/Scripts/EmojiText.cs
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 added Assets/Example/Scenes/01.unity
Binary file not shown.
29 changes: 29 additions & 0 deletions Assets/Example/Scripts/Test.cs
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 added ProjectSettings/AudioManager.asset
Binary file not shown.
Binary file added ProjectSettings/ClusterInputManager.asset
Binary file not shown.
Binary file added ProjectSettings/DynamicsManager.asset
Binary file not shown.
Binary file added ProjectSettings/EditorBuildSettings.asset
Binary file not shown.
Binary file added ProjectSettings/EditorSettings.asset
Binary file not shown.
Binary file added ProjectSettings/GraphicsSettings.asset
Binary file not shown.
Binary file added ProjectSettings/InputManager.asset
Binary file not shown.
Binary file added ProjectSettings/NavMeshAreas.asset
Binary file not shown.
Binary file added ProjectSettings/NetworkManager.asset
Binary file not shown.
Binary file added ProjectSettings/Physics2DSettings.asset
Binary file not shown.
Binary file added ProjectSettings/ProjectSettings.asset
Binary file not shown.
1 change: 1 addition & 0 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
m_EditorVersion: 5.5.1f1
Binary file added ProjectSettings/QualitySettings.asset
Binary file not shown.
Binary file added ProjectSettings/TagManager.asset
Binary file not shown.
Binary file added ProjectSettings/TimeManager.asset
Binary file not shown.
Binary file added ProjectSettings/UnityConnectSettings.asset
Binary file not shown.

0 comments on commit 9c20ffc

Please sign in to comment.