
Github に UnityStructShortcutExtensions というライブラリを公開しました。
URL
https://github.com/Taka414/UnityStructShortcutExtensions
このライブラリは Unity の Transform 等のゲームオブジェクトがもつ構造体 (positionなど) の操作のショートカットを提供します。
例えばゲームオブジェクトを移動したい時に position を操作する処理を以下のようにショートカットできます。
// ゲームオブジェクトのXを移動したい時 // When you want to move the X of a game object Vector3 position = transform.localPosition; position.x += 1; transform.localPosition = position // ↓ transform.AddLocalPos(1); // 画像の透明度を変更したい時 var sr = GetComponent<SpriteRenderer>(); Color c = sr.color; c.a = 0; sr.color = c; // ↓ sr.SetColorA(0);
transform.Translate を使用すればオブジェクトの移動はできると思いますが、作者がだいたいモバイル向けに 2D アプリの実装を良くしているので 3D環境の事はあまり考えずに、transform.Translate を使うよりこういった操作のほうが作業しやすいという考えで機能を提供しています。
色を変えるメソッドは地味にコード量が減るかもしれません。
サポートする型/Supported types
| 型/Type | プロパティ/Property | 説明/Description |
|---|---|---|
| Transform | position | 広範囲なサポート/Extensive support |
| localPosition | 広範囲なサポート/Extensive support | |
| localEulerAngles | 広範囲なサポート/Extensive support | |
| localScale | 広範囲なサポート/Extensive support | |
| RectTransformExtensions | sizeDelta | 部分的なサポート/Partial support |
| rect | 部分的なサポート/Partial support | |
| anchoredPosition | 部分的なサポート/Partial support | |
| pivot | 部分的なサポート/Partial support | |
| offsetMax | 部分的なサポート/Partial support | |
| offsetMin | 部分的なサポート/Partial support | |
| Graphic | color | 必要最小限のサポート/Minimal support required |
| SpriteRenderer | color | 必要最小限のサポート/Minimal support required |
よろしくお願いします。