So I'm working on a relatively complex label hack to allow me to encode input data, then hide it and display a rendered sprite of the input in question over top. I have it working (finally), but I ran into a really weird bug, or at least what seems like a bug. While using PrintExactCharacterPositions I got great results so long as the label was a single line. As soon as wrapping occurs however I'm finding my verts are off in their X position by some multiple of how many lines from the top of the label they are. To put another way, the verts array for the first line is perfect, second line is off in X by 1*n, 3rd is off by 2*n, and on like that.
Its a pretty hard to show what's going on, hence the lack of images. Here is a section of the code that may be relevant (I've stripped a bunch of stuff out of the actual function that isn't relevant to the NGUI side, so if it looks a bit out of context that's why):
targetLabel.UpdateNGUIText();
lastText = NGUIText.StripSymbols(targetLabel.processedText);
string textSoFar = "";
Vector2 offset = targetLabel.pivotOffset;
BetterList
<Vector3
> verts
= new BetterList
<Vector3
>(); BetterList
<int> indicies
= new BetterList
<int>();
NGUIText.Update();
NGUIText.PrintApproximateCharacterPositions(targetLabel.processedText, verts, indicies);
Vector2 characterSize = NGUIText.CalculatePrintedSize("O");
Bounds b = targetLabel.CalculateBounds();
string[] tokens
= lastText
.Split(new char[] {' ',
'\n'},
System.StringSplitOptions.None); int idx = 0;
foreach(string tok in tokens) {
if (tok.StartsWith("$i:")) {
Vector3 vertA = verts[(textSoFar.Length) * 2];
Vector3 vertB = verts[(textSoFar.Length + tok.Length) * 2 - 1];
int defIdx = int.Parse(tok.Substring(3).Replace("@", ""));
UiManager.InputSpriteEntry ie = GameManager.uiManager.inputSpriteDefs[defIdx];
UISprite sprite = NGUITools.AddSprite(targetLabel.gameObject, GameManager.uiManager.defaultAtlas, ie.sprite);
sprite.depth = 200;
sprite.MakePixelPerfect();
sprite.keepAspectRatio = UIWidget.AspectRatioSource.BasedOnHeight;
sprite.height = fixedSize ? (int)scaleFactor : (int)(characterSize.y * scaleFactor);
if (forceCenter) {
sprite.transform.localPosition = Vector3.zero;
} else {
Vector3 pos = (vertA + vertB) * 0.5f;
pos.x -= offset.x * b.size.x ;
pos.y += (1f - offset.y) * b.size.y;
sprite.transform.localPosition = pos;
}
}
if (idx == tokens.Length - 1) {
textSoFar = string.Format("{0}{1}", textSoFar, tok);
} else {
textSoFar = string.Format("{0}{1}{2}", textSoFar, tok, lastText[textSoFar.Length + tok.Length]);
}
idx++;
}
}
Oddly, PrintApproximateCharacterPositions seems more accurate for multiline labels, so I'm using that for now as the overall error is lower, but would be awesome to get Exact working. Any ideas more than welcome.