UnityNovelProj Unity で VisualNovel を作成する
Property について
c# の property は getter/setter を簡潔にかけるものです。 外部からはただのメンバ変数に見えますが、内部からはそれら変数にロジックを追加できます。 https://ufcpp.net/study/csharp/oo_property.html#google_vignette
さらに 式形式メンバー を使うことで、メンバ変数のようだが特殊な処理をする、をスッキリかけます。 ただし、 getter だけです。 https://ufcpp.net/study/csharp/ap_ver6.html#sec-expression-bodied
private CanvasGroup canvasGroup => rootGameObject.GetComponent<CanvasGroup>();
Coroutine
coroutine は C# ではなく Unity 特有の機能です。 x frame や x 秒ごとに処理を実行し終わったら元のコルーチン処理を再開する、というものです。 https://docs.unity3d.com/ja/560/Manual/Coroutines.html
MonoBehaviour のメソッドとして StartCoroutine と StopCoroutine が実行されており、 IEnumerator を引数に取ります。 CSharp の IEnumerator と IEnumerable について調べる で IEnumerator を調べています。
public Coroutine StartCoroutine(IEnumerator routine)
{
if (routine == null)
throw new NullReferenceException("routine is null");
if (!MonoBehaviour.IsObjectMonoBehaviour((Object) this))
throw new ArgumentException("Coroutines can only be stopped on a MonoBehaviour");
return this.StartCoroutineManaged2(routine);
}
StartCoroutine の内部でフレーム・秒ごとに IEnuemator の MoveNext, … を実行しているのだとおもいます。
下記は 会話ボックスを Show, Hide するときに Coroutine をつかうサンプルです。
MonoBehaviour をもつ DialogueSystemController.instance (singleton) を利用して StartCoroutine でコルーチン処理を開始しています。 ここで IEnumerator の Fading を渡しています。
IEnuemator Fading が実際のコルーチン処理であり、1 フレームごとに CanvasGroup の alpha を targetAlpha に向かわせます。
public Coroutine Show()
{
if (isShowing) return null;
if (isHiding)
{
DialogueSystemController.instance.StopCoroutine(hidingCoroutine);
hidingCoroutine = null;
}
return showingCoroutine = DialogueSystemController.instance.StartCoroutine(Fading(1f));
}
public IEnumerator Fading(float targetAlpha)
{
var cg = canvasGroup;
while (!Mathf.Approximately(cg.alpha, targetAlpha))
{
cg.alpha = Mathf.MoveTowards(cg.alpha, targetAlpha, Time.deltaTime * DEFAULT_FADE_SPEED);
yield return null;
}
showingCoroutine = hidingCoroutine = null;
}
- コルーチンの実際の処理は
IEnumerator
インターフェースを返す関数である - Start,StopCoroutine は IEnumerator を受け取りそれらを実行するだけ
- 返り値は Unity が定義した Coroutine という型
であることに注意です。
url: https://github.com/ganyariya/gnovel/pull/9
title: "会話ボックス (DialogueContainer) を Show, Hide する by ganyariya · Pull Request #9 · ganyariya/gnovel"
description: "https://www.youtube.com/watch?v=e7yhzXfUbuE&list=PLGSox0FgA5B58Ki4t4VqAPDycEpmkBd0i&index=57Summary by CodeRabbit新機能ダイアログボックスのフェード表示/非表示に対応。演出中に一時的に隠す操作が可能になり、スムーズなトランジションで没入感が向上。..."
host: github.com
favicon: https://github.githubassets.com/favicons/favicon.svg
image: https://opengraph.githubassets.com/24d5efd61be969810effb5df4edef9e0567af34334c6d12f4181a57667373bab/ganyariya/gnovel/pull/9