浏览代码

clip

/main
fzhangtj 6 年前
当前提交
67b248f3
共有 16 个文件被更改,包括 416 次插入316 次删除
  1. 6
      Assets/UIWidgets/Tests/Menu.cs
  2. 2
      Assets/UIWidgets/painting/text_span.cs
  3. 1
      Assets/UIWidgets/painting/text_style.cs
  4. 2
      Assets/UIWidgets/rendering/shifted_box.cs
  5. 1
      Assets/UIWidgets/ui/painting/canvas_impl.cs
  6. 2
      Assets/UIWidgets/ui/text.cs
  7. 11
      Assets/UIWidgets/ui/txt/linebreaker.cs
  8. 251
      Assets/UIWidgets/ui/txt/paragraph.cs
  9. 1
      Assets/UIWidgets/ui/txt/paragraph_builder.cs
  10. 70
      Assets/UIWidgets/Resources/UIWidgets_Text.shader
  11. 3
      Assets/UIWidgets/Resources/UIWidgets_Text.shader.meta
  12. 152
      Assets/UIWidgets/Tests/Paragraph.cs
  13. 3
      Assets/UIWidgets/Tests/Text.cs.meta
  14. 106
      Assets/UIWidgets/Tests/Text.cs
  15. 118
      Assets/UIWidgets/editor/TextMesh.cs
  16. 3
      Assets/UIWidgets/editor/TextMesh.cs.meta

6
Assets/UIWidgets/Tests/Menu.cs


EditorWindow.GetWindow(typeof(RenderBoxes));
}
[MenuItem("UIWidgetsTests/Text")]
public static void renderText() {
EditorWindow.GetWindow(typeof(Text));
[MenuItem("UIWidgetsTests/RenderParagraph")]
public static void renderRenderParagraph() {
EditorWindow.GetWindow(typeof(Paragraph));
}
}
}

2
Assets/UIWidgets/painting/text_span.cs


public readonly List<TextSpan> children;
public readonly GestureMock recognizer;
public TextSpan(TextStyle style, string text, List<TextSpan> children)
public TextSpan(string text = "", TextStyle style = null, List<TextSpan> children = null)
{
this.text = text;
this.style = style;

1
Assets/UIWidgets/painting/text_style.cs


color: color,
decoration: decoration,
fontWeight: fontWeight,
fontStyle: fontStyle,
fontSize: fontSize == null ? null : fontSize * textScaleFactor,
letterSpacing: letterSpacing,
wordSpacing: wordSpacing,

2
Assets/UIWidgets/rendering/shifted_box.cs


}
public class RenderPadding : RenderShiftedBox {
RenderPadding(
public RenderPadding(
EdgeInsets padding = null,
RenderBox child = null
) : base(child) {

1
Assets/UIWidgets/ui/painting/canvas_impl.cs


public void drawMesh(Mesh mesh, Material material)
{
prepareGL(material);
material.SetPass(0);
Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
}

2
Assets/UIWidgets/ui/text.cs


public static readonly double defaultFontSize = 14.0;
public static readonly FontWeight defaultFontWeight = FontWeight.w400;
public static readonly FontStyle defaultFontStyle = FontStyle.normal;
public static readonly Color defaultColor = Color.fromARGB(255, 255, 255, 255);
public static readonly Color defaultColor = Color.fromARGB(255, 0, 0, 0);
public Color color;
public double? fontSize;
public FontWeight? fontWeight;

11
Assets/UIWidgets/ui/txt/linebreaker.cs


private float _width;
private int _lineStart;
private int _wordStart;
private int _runIndex = 0;
private int _spaceCount = 0;
private int tabCount = 4;
private double _lineLength;

{
return _lines;
}
_lineStart = blockStart;
_wordStart = blockStart;
_spaceCount = 0;
float offsetX = 0.0f;
var runIterator = _runs.iterator();
for (var charIndex = blockStart; charIndex < blockEnd; charIndex++)

var font = _styleRunFonts[runIterator.runIndex];
if (run.start == charIndex)
{
font.RequestCharactersInTexture(_text.Substring(run.start, run.end - run.start), 0, run.style.UnityFontStyle);
}
var style = run.style;
CharacterInfo charInfo;

251
Assets/UIWidgets/ui/txt/paragraph.cs


using System;
using System.Collections.Generic;
using System.Text;
using JetBrains.Annotations;
using UnityEngine.Experimental.UIElements;
struct Range<T>: IEquatable<Range<T>>
{
public Range(T start, T end)
{
this.start = start;
this.end = end;
}
public bool Equals(Range<T> other)
{
return EqualityComparer<T>.Default.Equals(start, other.start) && EqualityComparer<T>.Default.Equals(end, other.end);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is Range<T> && Equals((Range<T>) obj);
}
public override int GetHashCode()
{
unchecked
{
return (EqualityComparer<T>.Default.GetHashCode(start) * 397) ^ EqualityComparer<T>.Default.GetHashCode(end);
}
}
public static bool operator ==(Range<T> left, Range<T> right)
{
return left.Equals(right);
}
public static bool operator !=(Range<T> left, Range<T> right)
{
return !left.Equals(right);
}
public T start, end;
}
class LineRange
{
public LineRange(int start, int end, int endExcludingWhitespace, int endIncludingNewLine, bool hardBreak)

public int prevWordEnd;
}
private static readonly Shader textShader;
static Paragraph() {
textShader = Shader.Find("UIWidgets/Text Shader");
if (textShader == null) {
throw new Exception("UIWidgets/Text Shader Lines not found");
}
}
private bool _needsLayout = true;
private string _text;

private List<LineRange> _lineRanges = new List<LineRange>();
private List<double> _lineWidths = new List<double>();
private Vector2[] _characterPositions;
private CharacterInfo[] _characterInfos;
private double _alphabeticBaseline;
private double _ideographicBaseline;
private float[] _characterSize;
private List<double> _lineHeights = new List<double>();
private bool _didExceedMaxLines;
// mesh
//
public const char CHAR_NBSP = '\u00A0';
public static bool isWordSpace(char ch)
{
return ch == ' ' || ch == CHAR_NBSP;
}
get { return 0.0; }
get { return _lineHeights.Count == 0 ? 0 : _lineHeights[_lineHeights.Count - 1]; }
get { return 0.0; }
get { return _minIntrinsicWidth; }
get { return 0.0; }
get { return _maxIntrinsicWidth; }
get { return 0.0; }
get { return _width; }
}

public bool didExceedMaxLines
{
get { return false; }
get { return _didExceedMaxLines; }
//var mesh = ;
for (int runIndex = 0; runIndex < _runs.size; ++runIndex)
{
var run = _runs.getRun(runIndex);

}
}
// canvas.drawMesh(mesh, _text, _runs);
// if (!_needsLayout && _width == constraints.width)
// {
// return;
// }
if (!_needsLayout && _width == constraints.width)
{
return;
}
computeLineBreak();
computeLineBreak();
layoutLines();
var maxLines = _paragraphStyle.maxLines ?? 0;
_didExceedMaxLines = maxLines == 0 || _lineRanges.Count <= maxLines;
var lineLimits = maxLines == 0 ? _lineRanges.Count : Math.Min(maxLines, _lineRanges.Count);
layoutLines(lineLimits);
_maxIntrinsicWidth = 0;
double lineBlockWidth = 0;
for (int i = 0; i < _lineRanges.Count; ++i)
double maxWordWidth = 0;
for (int lineNumber = 0; lineNumber < lineLimits; ++lineNumber)
var line = _lineRanges[i];
lineBlockWidth += _lineWidths[i];
if (line.hardBreak)
var line = _lineRanges[lineNumber];
var words = findWords(line.start, line.end);
words.ForEach((word) =>
_maxIntrinsicWidth = Math.Max(lineBlockWidth, _maxIntrinsicWidth);
lineBlockWidth = 0;
}
Debug.Assert(word.start < word.end);
double wordWidth = _characterPositions[word.end - 1].x - _characterPositions[word.start].x +
_characterWidths[word.end - 1];
if (wordWidth > maxWordWidth)
{
maxWordWidth = wordWidth;
}
});
computeWidthMetrics(maxWordWidth);
public void setText(string text, StyledRuns runs)

}
}
private void wordWrap(LayoutContext context)
private void computeWidthMetrics(double maxWordWidth)
if (context.wordStart == context.lineStart)
_maxIntrinsicWidth = 0;
double lineBlockWidth = 0;
for (int i = 0; i < _lineWidths.Count; ++i)
context.wordStart = context.index;
var line = _lineRanges[i];
lineBlockWidth += _lineWidths[i];
if (line.hardBreak)
{
_maxIntrinsicWidth = Math.Max(lineBlockWidth, _maxIntrinsicWidth);
lineBlockWidth = 0;
}
}
if (_paragraphStyle.maxLines == 1 || (((_paragraphStyle.maxLines??0) == 0) &&
!string.IsNullOrEmpty(_paragraphStyle.ellipsis)))
{
_minIntrinsicWidth = _maxIntrinsicWidth;
}
else
{
_minIntrinsicWidth = Math.Min(maxWordWidth, _maxIntrinsicWidth);
}
}

_characterInfos = new CharacterInfo[_text.Length];
_characterSize = new float[_text.Length];
_lineHeights.Clear();
_lineRanges.Clear();
_lineWidths.Clear();
_characterWidths = new float[_text.Length];
if (_styleRunFonts == null)
{

{
_styleRunFonts[i] = Font.CreateDynamicFontFromOSFont(run.style.safeFontFamily,
run.style.UnityFontSize);
_styleRunFonts[i].material.shader = textShader;
_styleRunFonts[i].RequestCharactersInTexture(_text.Substring(run.start, run.end - run.start), 0,
run.style.UnityFontStyle);
// for (var i = 0; i < _runs.size; i++)
// {
// var run = _runs.getRun(i);
// // run.font.RequestCharactersInTexture(_text.Substring(run.start, run.end - run.start), run.font.fontSize, run.) ;
// }
// private void layoutBlock(int start, int end, ref int styleIndex)
// {
//
// }
private void layoutLines()
private void layoutLines(int lineLimits)
for (int i = 0; i < _lineRanges.Count; i++)
for (int lineNumber = 0; lineNumber < lineLimits; lineNumber++)
var line = _lineRanges[i];
var line = _lineRanges[lineNumber];
for (;runIndex < _runs.size;runIndex++)
for (;;)
if (run.start < run.end)
if (run.start < run.end && run.start < line.end && run.end > line.start)
{
var font = _styleRunFonts[runIndex];
var ascent = font.ascent * (run.style.height??1.0);

}
}
if (runIndex + 1 < _runs.size)
if (runIndex + 1 >= _runs.size)
{
break;
}
if (run.end < line.end)
{
runIndex++;
}
else
var nextRun = _runs.getRun(runIndex + 1);
if (nextRun.start >= line.end)
{
break;
}
break;
if (lineNumber == 0)
{
_alphabeticBaseline = maxAscent;
_ideographicBaseline = maxAscent; // todo Properly implement ideographic_baseline
}
lastDescent = maxDescent;
yOffset += maxAscent + lastDescent;
for (var charIndex = line.start; charIndex < line.end; charIndex++)

_lineHeights.Add((_lineHeights.Count == 0 ? 0 : _lineHeights[_lineHeights.Count - 1]) +
Math.Round(maxAscent + maxDescent));
_lineRanges.Clear();
_lineWidths.Clear();
var newLinePositions = new List<int>();
for (var i = 0; i < _text.Length; i++)
{

{
_lineRanges.Add(new LineRange(blockStart, blockEnd, blockEnd, blockEnd + 1, true));
_lineWidths.Add(0);
continue;
}
lineBreaker.doBreak(blockStart, blockEnd);

var vertices = new Vector3[_text.Length * 4];
var triangles = new int[_text.Length * 6];
var uv = new Vector2[_text.Length * 4];
Vector3 pos = new Vector3((float)x, (float)y, 0);
var offset = new Vector2((float) x, (float) y);
Vector3 offset = new Vector3((float)x, (float)y, 0);
var result = font.GetCharacterInfo(_text[charIndex], out charInfo);
var position = _characterPositions[charIndex] + offset;
var result = font.GetCharacterInfo(_text[charIndex], out charInfo, run.style.UnityFontSize, run.style.UnityFontStyle);
var position = _characterPositions[charIndex];
vertices[4 * charIndex + 0] = pos + new Vector3(position.x + charInfo.minX, position.y - charInfo.maxY, 0);
vertices[4 * charIndex + 1] = pos + new Vector3(position.x + charInfo.maxX, position.y - charInfo.maxY, 0);
vertices[4 * charIndex + 2] = pos + new Vector3(position.x + charInfo.maxX, position.y - charInfo.minY, 0);
vertices[4 * charIndex + 3] = pos + new Vector3(position.x + charInfo.minX, position.y - charInfo.minY, 0);
vertices[4 * charIndex + 0] = offset + new Vector3(position.x + charInfo.minX, position.y - charInfo.maxY, 0);
vertices[4 * charIndex + 1] = offset + new Vector3(position.x + charInfo.maxX, position.y - charInfo.maxY, 0);
vertices[4 * charIndex + 2] = offset + new Vector3(position.x + charInfo.maxX, position.y - charInfo.minY, 0);
vertices[4 * charIndex + 3] = offset + new Vector3(position.x + charInfo.minX, position.y - charInfo.minY, 0);
if (_text[charIndex] != ' ' && _text[charIndex] != '\t')
if (_text[charIndex] != ' ' && _text[charIndex] != '\t' && _text[charIndex] != '\n')
{
uv[4 * charIndex + 0] = charInfo.uvTopLeft;
uv[4 * charIndex + 1] = charInfo.uvTopRight;

}
triangles[6 * charIndex + 0] = 4 * charIndex + 0;
triangles[6 * charIndex + 1] = 4 * charIndex + 1;
triangles[6 * charIndex + 2] = 4 * charIndex + 2;

mesh.colors = colors;
return mesh;
}
private List<Range<int>> findWords(int start, int end)
{
var inWord = false;
int wordStart = 0;
List<Range<int>> words = new List<Range<int>>();
for (int i = start; i < end; ++i) {
bool isSpace = isWordSpace(_text[i]);
if (!inWord && !isSpace) {
wordStart = i;
inWord = true;
} else if (inWord && isSpace) {
words.Add(new Range<int>(wordStart, i));
inWord = false;
}
}
if (inWord) {
words.Add(new Range<int>(wordStart, end));
}
return words;
}
}

1
Assets/UIWidgets/ui/txt/paragraph_builder.cs


public Paragraph build()
{
_runs.endRunIfNeeded(_text.Length);
var paragraph = new Paragraph();
paragraph.setText(_text.ToString(), _runs);
paragraph.setParagraphStyle(_paragraphStyle);

70
Assets/UIWidgets/Resources/UIWidgets_Text.shader


Shader "UIWidgets/Text Shader" {
Properties {
_MainTex ("Font Texture", 2D) = "white" {}
_Color ("Text Color", Color) = (1,1,1,1)
}
SubShader {
Tags {
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
}
Lighting Off Cull Off ZTest Always ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f {
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float2 clipUV : TEXCOORD1;
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform fixed4 _Color;
#include "UIWidgets_CG.cginc"
v2f vert (appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.vertex = UnityObjectToClipPos(v.vertex);
float3 eyePos = UnityObjectToViewPos(v.vertex);
o.color = v.color * _Color;
o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
o.clipUV = mul(UIWidgets_GUIClipMatrix, float4(eyePos.xy, 0, 1.0));
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = i.color;
col.a *= tex2D(_MainTex, i.texcoord).a;
float pixelScale = 1.0f / abs(ddx(i.clipUV.x));
col.a *= getClipAlpha(i.clipUV, pixelScale);
return col;
}
ENDCG
}
}
}

3
Assets/UIWidgets/Resources/UIWidgets_Text.shader.meta


fileFormatVersion: 2
guid: 3d9aebae673b434491eb311fc9d0df0f
timeCreated: 1536033642

152
Assets/UIWidgets/Tests/Paragraph.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UIWidgets.editor;
using UIWidgets.painting;
using UIWidgets.rendering;
using UIWidgets.ui;
using UnityEditor;
using UnityEngine;
using Color = UIWidgets.ui.Color;
using FontStyle = UIWidgets.ui.FontStyle;
using Rect = UIWidgets.ui.Rect;
namespace UIWidgets.Tests
{
public class Paragraph : EditorWindow
{
private readonly Func<RenderBox>[] _options;
private readonly string[] _optionStrings;
private int _selected;
Paragraph() {
this._options = new Func<RenderBox>[] {
this.text,
this.textHeight,
this.textOverflow
};
this._optionStrings = this._options.Select(x => x.Method.Name).ToArray();
this._selected = 0;
this.titleContent = new GUIContent("RenderBoxes");
}
private WindowAdapter windowAdapter;
private RendererBindings rendererBindings;
[NonSerialized] private bool hasInvoked = false;
void OnGUI() {
var selected = EditorGUILayout.Popup("test case", this._selected, this._optionStrings);
if (selected != this._selected || !this.hasInvoked) {
this._selected = selected;
this.hasInvoked = true;
var renderBox = this._options[this._selected]();
this.rendererBindings.setRoot(renderBox);
}
if (this.windowAdapter != null) {
this.windowAdapter.OnGUI();
}
}
void Update() {
if (this.windowAdapter != null) {
this.windowAdapter.Update();
}
}
private void OnEnable() {
this.windowAdapter = new WindowAdapter(this);
this.rendererBindings = new RendererBindings(this.windowAdapter);
}
void OnDestroy() {
this.windowAdapter = null;
this.rendererBindings = null;
}
RenderBox none() {
return null;
}
private RenderBox box(RenderParagraph p, int width = 300, int height = 300)
{
return new RenderConstrainedOverflowBox(
minWidth: width,
maxWidth: width,
minHeight: height,
maxHeight: height,
alignment: Alignment.center,
child: new RenderDecoratedBox(
decoration: new BoxDecoration(
color: new Color(0xFFFFFFFF),
borderRadius: BorderRadius.all(3),
border: Border.all(Color.fromARGB(255, 255, 0, 0), 1)
),
child: new RenderPadding(EdgeInsets.all(10), p
)
)
);
}
RenderBox text()
{
return box(
new RenderParagraph(new TextSpan("", children:
new List<TextSpan>()
{
new TextSpan("Real-time 3D revolutioni淡粉色的方式地方zes the animation pipeline ", null),
new TextSpan(style: new painting.TextStyle(color: Color.fromARGB(255, 255, 0, 0)),
text: "for Disney Television Animation's “Baymax Dreams"),
new TextSpan(" Unity Widgets"),
new TextSpan(" Text"),
new TextSpan("Real-time 3D revolutionizes the animation pipeline "),
new TextSpan(style: new painting.TextStyle(color: Color.fromARGB(125, 255, 0, 0)),
text: "Transparent Red Text\n\n"),
new TextSpan(style: new painting.TextStyle(fontWeight: FontWeight.w700),
text: "Bold Text Test Bold Textfs Test: FontWeight.w70\n\n"),
new TextSpan(style: new painting.TextStyle(fontStyle: FontStyle.italic),
text: "This is FontStyle.italic Text This is FontStyle.italic Text\n\n"),
new TextSpan(style: new painting.TextStyle(fontStyle: FontStyle.italic, fontWeight: FontWeight.w700),
text: "This is FontStyle.italic And 发撒放豆腐sad 发生的 Bold Text This is FontStyle.italic And Bold Text\n\n"),
new TextSpan(style: new painting.TextStyle(fontSize: 18),
text: "FontSize 18: Get a named matrix value from the shader."),
new TextSpan(style: new painting.TextStyle(fontSize: 14),
text: "FontSize 14"),
})));
}
RenderBox textOverflow()
{
return box(
new RenderParagraph(new TextSpan("", children:
new List<TextSpan>()
{
new TextSpan("Real-time 3D revolutionizes:\n the animation pipeline.\n\n\revolutionizesn\n\nReal-time 3D revolutionizes the animation pipeline ", null),
})), 200, 80);
}
RenderBox textHeight()
{
var text =
"Hello UIWidgets. Real-time 3D revolutionize \nReal-time 3D revolutionize\nReal-time 3D revolutionize\n\n";
return box(
new RenderParagraph(new TextSpan(text: "", children:
new List<TextSpan>()
{
new TextSpan(style: new painting.TextStyle(height: 1),
text: "Height 1.0 Text:" + text),
new TextSpan(style: new painting.TextStyle(height: 1.2),
text: "Height 1.2 Text:" + text),
new TextSpan(style: new painting.TextStyle(height: 1.5),
text: "Height 1.5 Text:" + text),
})));
}
}
}

3
Assets/UIWidgets/Tests/Text.cs.meta


fileFormatVersion: 2
guid: f4121086094d45de9e29781269270102
timeCreated: 1535424100

106
Assets/UIWidgets/Tests/Text.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UIWidgets.editor;
using UIWidgets.painting;
using UIWidgets.rendering;
using UIWidgets.ui;
using UnityEditor;
using UnityEngine;
using Color = UIWidgets.ui.Color;
using FontStyle = UIWidgets.ui.FontStyle;
using Rect = UIWidgets.ui.Rect;
namespace UIWidgets.Tests
{
public class Text : EditorWindow
{
private readonly Func<RenderBox>[] _options;
private readonly string[] _optionStrings;
private int _selected;
Text() {
this._options = new Func<RenderBox>[] {
this.text,
};
this._optionStrings = this._options.Select(x => x.Method.Name).ToArray();
this._selected = 0;
this.titleContent = new GUIContent("RenderBoxes");
}
private WindowAdapter windowAdapter;
private RendererBindings rendererBindings;
[NonSerialized] private bool hasInvoked = false;
void OnGUI() {
var selected = EditorGUILayout.Popup("test case", this._selected, this._optionStrings);
if (selected != this._selected || !this.hasInvoked) {
this._selected = selected;
this.hasInvoked = true;
var renderBox = this._options[this._selected]();
this.rendererBindings.setRoot(renderBox);
}
if (this.windowAdapter != null) {
this.windowAdapter.OnGUI();
}
}
void Update() {
if (this.windowAdapter != null) {
this.windowAdapter.Update();
}
}
private void OnEnable() {
this.windowAdapter = new WindowAdapter(this);
this.rendererBindings = new RendererBindings(this.windowAdapter);
}
void OnDestroy() {
this.windowAdapter = null;
this.rendererBindings = null;
}
RenderBox none() {
return null;
}
RenderBox text()
{
/*
*bool inherit, Color color, double? fontSize, FontWeight fontWeight,
FontStyle fontStyle, double letterSpacing, double wordSpacing,
TextBaseline textBaseline, double height, TextDecoration decoration
*
*/
var style1 = new painting.TextStyle(true, Color.fromARGB(255, 0, 0, 0), null, FontWeight.w400, FontStyle.normal, 1,
1, TextBaseline.alphabetic, 1, TextDecoration.none);
var style2 = new painting.TextStyle(true, Color.fromARGB(255, 255, 0, 0), null, FontWeight.w400, FontStyle.normal, 1,
1, TextBaseline.alphabetic, 1, TextDecoration.none, fontFamily:"Helvetica");
return new RenderConstrainedOverflowBox(
minWidth: 200,
maxWidth: 200,
minHeight: 100,
maxHeight: 100,
alignment: Alignment.center,
// child: new RenderParagraph(new TextSpan(style1, "is is FontStyle fontStyle, double letterSpacing, double wordSpacing, ",
// null))
child: new RenderParagraph(new TextSpan(style1, "",
new List<TextSpan>()
{
new TextSpan(null, "Hello sda fdf asdf asd fadsfdfs fdsffsdf d sdfsfsf sd fsfsdf", null),
new TextSpan(null, "TextBaseline textBaseline, double height, TextDecoration decoration", null),
new TextSpan(style2, " Unity Widgets", null),
new TextSpan(null, " Text", null)
}))
);
}
}
}

118
Assets/UIWidgets/editor/TextMesh.cs


using UnityEditor;
using UnityEngine;
namespace UIWidgets.editor
{
public class TextMeshWindow : EditorWindow
{
Font font;
string str = "Hello World";
Mesh mesh;
// Add menu named "My Window" to the Window menu
[MenuItem("TextMeshWindow/TextMeshWindow")]
static void Init()
{
// Get existing open window or if none, make a new one:
TextMeshWindow window = (TextMeshWindow)EditorWindow.GetWindow(typeof(TextMeshWindow));
window.Show();
}
void OnFontTextureRebuilt(Font changedFont)
{
if (changedFont != font)
return;
TextGenerator t = new TextGenerator();
RebuildMesh();
}
void RebuildMesh()
{
// Generate a mesh for the characters we want to print.
var vertices = new Vector3[str.Length * 4];
var triangles = new int[str.Length * 6];
var uv = new Vector2[str.Length * 4];
Vector3 pos = Vector3.zero;
for (int i = 0; i < str.Length; i++)
{
// Get character rendering information from the font
CharacterInfo ch;
font.GetCharacterInfo(str[i], out ch);
vertices[4 * i + 0] = pos + new Vector3(ch.minX, -ch.maxY + 100, 0);
vertices[4 * i + 1] = pos + new Vector3(ch.maxX, -ch.maxY + 100, 0);
vertices[4 * i + 2] = pos + new Vector3(ch.maxX, -ch.minY + 100, 0);
vertices[4 * i + 3] = pos + new Vector3(ch.minX, -ch.minY + 100, 0);
uv[4 * i + 0] = ch.uvTopLeft;
uv[4 * i + 1] = ch.uvTopRight;
uv[4 * i + 2] = ch.uvBottomRight;
uv[4 * i + 3] = ch.uvBottomLeft;
triangles[6 * i + 0] = 4 * i + 0;
triangles[6 * i + 1] = 4 * i + 1;
triangles[6 * i + 2] = 4 * i + 2;
triangles[6 * i + 3] = 4 * i + 0;
triangles[6 * i + 4] = 4 * i + 2;
triangles[6 * i + 5] = 4 * i + 3;
// Advance character position
pos += new Vector3(ch.advance, 0, 0);
}
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uv;
}
private void OnEnable()
{
font = Font.CreateDynamicFontFromOSFont("Helvetica", 16);
// Set the rebuild callback so that the mesh is regenerated on font changes.
Font.textureRebuilt += OnFontTextureRebuilt;
// Request characters.
font.RequestCharactersInTexture(str);
// Set up mesh.
mesh = new Mesh();
// Generate font mesh.
RebuildMesh();
}
void OnGUI()
{
if (Event.current.type == EventType.Repaint)
{
font.RequestCharactersInTexture(str);
Material mat = font.material;
mat.SetPass(0);
Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
// Graphics.DrawMesh(mesh, new Vector3(0, 0, 100), Quaternion.identity, font.material, 0);
}
if (GUI.Button(new Rect(200, 200, 100, 100), "Set Font"))
{
font = Font.CreateDynamicFontFromOSFont("Helvetica", 16);
// Request characters.
font.RequestCharactersInTexture(str);
// Set up mesh.
mesh = new Mesh();
// Generate font mesh.
RebuildMesh();
}
}
void OnUpdate()
{
font.RequestCharactersInTexture(str);
}
}
}

3
Assets/UIWidgets/editor/TextMesh.cs.meta


fileFormatVersion: 2
guid: 3f658931d0dc4f06817e18cb3a999189
timeCreated: 1535014129
正在加载...
取消
保存