Last active
January 15, 2018 06:54
-
-
Save dskjal/0eb9a38316217d3731a252898cc6ab34 to your computer and use it in GitHub Desktop.
Unity の uGUI を使ってルビを配置する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BEGIN MIT LICENSE BLOCK // | |
// | |
// Copyright (c) 2017 dskjal | |
// This software is released under the MIT License. | |
// http://opensource.org/licenses/mit-license.php | |
// | |
// END MIT LICENSE BLOCK // | |
/* | |
* *注意* | |
* 改行の処理はしてない.ルビを必要とする部分が途中で改行されないよう処理する必要がある. | |
* http://dskjal.com/unity/detect-unity-ugui-break-pos.html を参照. | |
*/ | |
/* | |
* 設定方法 | |
* uGUI の Text にこのスクリプトをつける. | |
* テキストのセンタリング設定をした uGUI の Text のプレハブを作り TextPrefab にセット | |
* プレハブのフォントサイズをテキストのフォントサイズの 1/2 ぐらいにする | |
*/ | |
using UnityEngine; | |
using UnityEngine.UI; | |
[RequireComponent(typeof(Text), typeof(RectTransform))] | |
public class RubyText : MonoBehaviour { | |
Text text; | |
RectTransform rt; | |
public GameObject TextPrefab; // テキストはセンタリングしておくこと | |
class RubyPos { | |
public int start; // ルビの開始インデックス | |
public int end; // ルビの終了インデックス | |
public string ruby; // ルビ | |
public RubyPos(int start, int end, string ruby) { | |
this.start = start; | |
this.end = end; | |
this.ruby = ruby; | |
} | |
} | |
void Awake() { | |
text = GetComponent<Text>(); | |
rt = GetComponent<RectTransform>(); | |
text.text = "ルビが必要な例文章です."; | |
RubyPos[] rubyPos = new RubyPos[] { | |
new RubyPos(3, 4, "ひつよう"), | |
new RubyPos(6, 8, "れいぶんしょう") | |
}; | |
var generator = new TextGenerator(); | |
// テキストのレンダリング位置の計算 | |
generator.Populate(text.text, text.GetGenerationSettings(rt.sizeDelta)); | |
// 各文字のレンダリング位置を記録した文字配列の取得 | |
var charArray = generator.GetCharactersArray(); | |
foreach (var ruby in rubyPos) { | |
var start = charArray[ruby.start].cursorPos; | |
var end = charArray[ruby.end].cursorPos; | |
end.x += charArray[ruby.end].charWidth; | |
PlaceRuby(start.x + (end.x - start.x) / 2f, start.y, ruby.ruby); | |
} | |
} | |
// TextPrefab をインスタンス化して配置する | |
void PlaceRuby(float x, float y, string text) { | |
var o = GameObject.Instantiate(TextPrefab); | |
o.name = text; | |
o.transform.SetParent(this.transform); | |
var prt = o.GetComponent<RectTransform>(); | |
prt.localPosition = new Vector3(x, y, 0f); | |
o.GetComponent<Text>().text = text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment