Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 5ba0d42

Browse files
committed
增加UI的快捷创建
1 parent e8f1cad commit 5ba0d42

File tree

10 files changed

+84
-14
lines changed

10 files changed

+84
-14
lines changed

Assets/TextInlineSprite/Editor/CreateSpriteAsset.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public static class CreateSpriteAsset
88
{
9-
[MenuItem("Assets/Create/UGUI Sprite Asset",false,10)]
9+
[MenuItem("Assets/Create/Sprite Asset",false,10)]
1010
static void main()
1111
{
1212
Object target = Selection.activeObject;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using UnityEditor;
4+
using UnityEngine.UI;
5+
using UnityEngine.EventSystems;
6+
7+
public class TextMenuExtender
8+
{
9+
[MenuItem("GameObject/UI/TextInline", false, 10)]
10+
static void CreateCustomGameObject(MenuCommand menuCommand)
11+
{
12+
GameObject go = null;
13+
InlineManager _inline = AssetDatabase.LoadAssetAtPath<InlineManager>("Assets/TextInlineSprite/Prefabs/TextInline.prefab");
14+
if (_inline)
15+
{
16+
go = GameObject.Instantiate(_inline).gameObject;
17+
}
18+
else
19+
{
20+
go = new GameObject();
21+
var line = go.AddComponent<InlineText>();
22+
}
23+
go.name = "InlinText";
24+
GameObject _parent = menuCommand.context as GameObject;
25+
if (_parent == null)
26+
{
27+
_parent = new GameObject("Canvas");
28+
_parent.layer = LayerMask.NameToLayer("UI");
29+
_parent.AddComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
30+
_parent.AddComponent<CanvasScaler>();
31+
_parent.AddComponent<GraphicRaycaster>();
32+
33+
EventSystem _es = GameObject.FindObjectOfType<EventSystem>();
34+
if (!_es)
35+
{
36+
_es = new GameObject("EventSystem").AddComponent<EventSystem>();
37+
_es.gameObject.AddComponent<StandaloneInputModule>();
38+
}
39+
}
40+
GameObjectUtility.SetParentAndAlign(go, _parent);
41+
//注册返回事件
42+
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
43+
Selection.activeObject = go;
44+
}
45+
46+
}
47+

Assets/TextInlineSprite/Editor/TextMenuExtender.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/TextInlineSprite/Examples/Prefab.meta renamed to Assets/TextInlineSprite/Prefabs.meta

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/TextInlineSprite/Prefabs/TextInline.prefab.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/TextInlineSprite/Scripts/InlineManager.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ public class InlineManager : MonoBehaviour {
2626
[Range(1,10)]
2727
private float _AnimationSpeed = 5.0f;
2828

29-
// Use this for initialization
30-
void Awake () {
31-
Initialize();
32-
}
29+
//// Use this for initialization
30+
//void Awake () {
31+
// Initialize();
32+
// }
3333

3434
// Update is called once per frame
3535
void Update () {
@@ -38,8 +38,13 @@ void Update () {
3838
DrawSpriteAnimation();
3939
}
4040

41+
private void OnEnable()
42+
{
43+
Initialize();
44+
}
45+
4146
#if UNITY_EDITOR
42-
protected void OnValidate()
47+
public void OnValidate()
4348
{
4449
Initialize();
4550
}

Assets/TextInlineSprite/Scripts/InlineText.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using UnityEngine.Events;
1717
using System;
1818

19+
[ExecuteInEditMode]
1920
public class InlineText : Text, IPointerClickHandler
2021
{
2122
// 用正则取 [图集ID#表情Tag] ID值==-1 ,表示为超链接
@@ -37,7 +38,7 @@ public class HrefClickEvent : UnityEvent<string> { }
3738
// 超链接信息列表
3839
private readonly List<HrefInfo> _ListHrefInfos = new List<HrefInfo>();
3940
#endregion
40-
41+
4142
/// <summary>
4243
/// 初始化
4344
/// </summary>
@@ -63,7 +64,6 @@ public override void SetVerticesDirty()
6364

6465
//设置新文本
6566
_OutputText = GetOutputText();
66-
6767
}
6868

6969
readonly UIVertex[] m_TempVerts = new UIVertex[4];

Assets/TextInlineSprite/Scripts/SpriteGraphic.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
public class SpriteGraphic : MaskableGraphic {
66

77
public SpriteAsset m_spriteAsset;
8-
98
public override Texture mainTexture
109
{
1110
get
1211
{
13-
if (m_spriteAsset == null|| m_spriteAsset.texSource == null)
12+
if (m_spriteAsset == null|| m_spriteAsset.texSource==null)
1413
return s_WhiteTexture;
1514
else
1615
return m_spriteAsset.texSource;
1716
}
1817
}
1918

20-
2119
protected override void OnEnable()
2220
{
2321
//不调用父类的OnEnable 他默认会渲染整张图片

0 commit comments

Comments
 (0)