0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unity6.5でMathmaticsパッケージがビルドインになり、いくつかの未実装だった「暗黙の型変換演算子」も追加された

0
Posted at

概要

Unity 6.5でMathematicsパッケージが、Unity本体に含まれるビルトインパッケージになりました。

Unity Mathematics now a built-in module

には、次のような記載があります。

The Unity Mathematics package is now a built-in module of the core Editor and Engine. Its APIs are available to user code by default in the Unity.Mathematics namespace, with no package installation required. Accordingly, the Unity Mathematics API documentation is now part of the core Scripting API reference under the Unity.Mathematics namespace, and the user manual is integrated with the core user manual. Implicit conversion operators are available for converting between UnityEngine math types (such as Vector3, Matrix4x4) and their Unity.Mathematics equivalents (float3, float4x4). For more information, refer to Programming with math.

Unity 6.5から「Mathematicsパッケージのリファレンス」が、公式スクリプトリファレンスの下に追加されました。今までは、Mathematicsパッケージのページにリファレンスありました。

なぜか、Unity 6.5のビルドインパッケージのリストには載っていません?謎?(2026/06/19時点)

Mathematicsパッケージとは?

Mathematicsパッケージは、float3quaternionfloat4x4 などの数学型と、math クラスによる数学関数を提供するライブラリです。Burst-compilableでSIMD(単一命令複数データ)に対応したAPI群が用意されています。

UnityEngineではよく Vector3 を使いますが、Mathematicsでは float3 を使います。float3は構造体の名前であることに注意してください。(.NETの命名セオリーとは違う命名をしていますね。)

using Unity.Mathematics;

/* 略 */
float3 a = new float3(1f, 0f, 0f);
float3 b = new float3(0f, 1f, 0f);

float distance = math.distance(a, b);

float3以外にも、quaternionやint2など、Mathematicsでは専用の構造体が多数提供されています。

ビルドインパッケージとは?

Unityのビルドインパッケージは、Unity Editor本体に組み込まれている機能群を、Package Manager上で有効化・無効化できる形に分離したものです。

Mathematicsパッケージは、Unity 6.5でビルドインになりました。

ただ、以前もBurstパッケージや2D Commonパッケージが、Mathematicsパッケージに依存していたため、自動的にプロジェクトが依存していた可能性もあります。

Mathematicsの暗黙の型変換演算子の追加

UnityEngine.Vector3 <=> Unity.Mathematics.float3
UnityEngine.Quaternion <=> Unity.Mathematics.quaternion

などの型の間には、暗黙の型変換演算子が定義されています。

using UnityEngine;
using Unity.Mathematics;

/* 略 */
Vector3 vector3 = new float3(1.0f, 1.0f, 1.0f);
quaternion quaternion = Quaternion.identity;

Unity 6.5で、次の型の「暗黙的な型変換」が実装されました。

UnityEngine.Vector2Int <=> Unity.Mathematics.int2
UnityEngine.Vector3Int <=> Unity.Mathematics.int3

該当リリースノートはこちら

Scripting: Added: Added implicit conversion between int2 <-> VectorInt2.
Scripting: Added: Added implicit conversion between int3 <-> VectorInt3.

まとめ

次のようなページも追加されています。

こちらのページには、既存のUnityEngine.MathfとMathematicsパッケージの使い分けに関して、次のような説明があります。

In general, for performance reasons the following is recommended:

  • In code compiled just-in-time (JIT) with the Mono scripting backend: use UnityEngine math APIs rather than Unity.Mathematics.
  • In code compiled ahead-of-time (AOT) with Burst: use Unity Mathematics by default and only use the UnityEngine math APIs when necessary.

適切な場面・状況で、Mathematicsパッケージを活用していきたいですね!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?