Giter VIP home page Giter VIP logo

typingsystem's Introduction

概要

TypingSystemはタイピングゲームの開発をサポートするライブラリです。
平仮名、アルファベット、数字、記号を含んだ文字列をゼロアロケーションでローマ字で入力可能な文字列に変換します。
ライブラリの挙動を確認したい方は、WebGLデモを参照してください。

image

特徴

  • 入力済みの文字に応じたタイピング情報を生成します。
  • インスタンス生成時のバッファ確保によりゼロアロケーションで動作します。
  • 変換対象の文字列は平仮名、アルファベット、数字、記号の混合に対応。
  • TextMeshProに文字列をセットするときにString生成を回避できる拡張メソッド、バッファを提供。

セットアップ

要件

  • C# 9.0以上
  • TextMeshPro (TextMeshPro拡張を削除すれば導入しなくても動作します)

インストール

最新リリースからUnityPackageをダウンロード

使い方

TypingSystem

初期化

TypingSystem typingSystem = new TypingSystem();

//平仮名、アルファベット、数字、記号でテキストをセット
typingSystem.SetTypingKanaText("たいぴんぐもじれつ");

入力チェック

サンプルでは、InputSystemのonTextInputイベントでキーボードからの入力をchar型で取得しています。

//入力されたchar型文字をセットされた文字列に対して有効かどうかチェックする
if (typingSystem.CheckInputChar('t'))
{     
}

タイピング情報

//セットされたタイピング文字列を現在の入力状況を考慮したローマ字に変換したもの
ReadOnlySpan<char> fullRomajiPatternSpan = typingSystem.FullRomajiPatternSpan;
Debug.Log(fullRomajiPatternSpan.ToString());

//タイピング文字列セット後の有効な入力
ReadOnlySpan<char> validInputSpan = typingSystem.ValidInputSpan;
Debug.Log(validInputSpan.ToString());

//入力可能なローマ字パターン
foreach (ReadOnlySpan<char> pattern in typingSystem.GetPermittedRomajiPatterns())
{
      Debug.Log(pattern.ToString());
}

//セットされたタイピング文字列を入力し終えたか
bool isComplete = typingSystem.IsComplete;

//入力済みのセットされたタイピング文字列の長さ
int inputedKanaLength = typingSystem.InputedKanaLength;

カスタムバッファ

  • StructBuffer

    Spanでの要素の追加とビューの取得に使う
//char[64]が内部で確保される。
StructBuffer<char> buffer = new StructBuffer<char>(64);

ReadOnlySpan<char> validInputSpan = typingSystem.ValidInputSpan;

//Spanをそのまま追加できる
buffer.Add(validInputSpan);
//char単体も追加できる
buffer.Add('!');

//追加された要素の配列のビューを取得できる (ArraySegment, ReadOnlySpan, Memory)
ArraySegment<char> segment = buffer.Segment;
ReadOnlySpan<char> span = buffer.Span;
ReadOnlyMemory<char> memory = buffer.Memory;

buffer.Clear();
  • LimitedQueue

    事前に取得したキャパシティを超えたときに古い要素を削除して詰めるキュー
    サンプルでは入力履歴で使用
//char[32]が内部で確保される
LimitedQueue<char> buffer = new LimitedQueue<char>(32);

//キューに追加
//キューが上限に達した場合は古い要素を捨てて詰める
buffer.Enqueue('a');

//追加された要素の配列のビューを取得できる (ArraySegment, ReadOnlySpan, Memory)
ArraySegment<char> segment = buffer.Segment;
ReadOnlySpan<char> span = buffer.Span;
ReadOnlyMemory<char> memory = buffer.Memory;

buffer.Clear();

TextMeshProにはchar配列で文字列をセットできるSetCharArray関数が用意されているので、String生成を避けることができます。

//SpanをStructBufferに書き込んでArraySegmentで文字列をセットする拡張メソッド
public static void SetCharSpan(this TMP_Text tmp, in ReadOnlySpan<char> text, StructBuffer<char> buffer)

//ArraySegmentで文字列をセットする拡張メソッド
public static void SetCharArraySegment(this TMP_Text tmp, in ArraySegment<char> segment)
  • RichColorTag TextMeshProのリッチテキストカラータグを生成する構造体
//初期化 (内部でカラータグを生成)
RichTextTag richTextTag = new RichTextTag(Color.Red);
//引数で渡された文字列の先頭から指定された文字数までタグで囲んでバッファに書き込む
public void WriteTaggedTextToBuffer(in ReadOnlySpan<char> text, int charLength, StructBuffer<char> buffer)

typingsystem's People

Contributors

rinqer0203 avatar

Stargazers

くり太郎 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.