Unity_Lesson

Func の説明

Func は、C# のデリゲートの一種で、引数を受け取り、結果を返すメソッドを表現するための型です。主に次のような場面で使用されます。

  1. 関数を変数として扱いたい: メソッドを変数として持ち、必要に応じて呼び出すことができます。
  2. メソッドを引数に渡す: 他のメソッドに処理を渡したり、コールバックとして使用したりすることができます。

Unityでの Func の使用例

以下に、Unityのシンプルなスクリプトで Func を使用した例を示します。この例では、プレイヤーのスコアを計算するための Func を使っています。

using UnityEngine;

public class ScoreManager : MonoBehaviour
{
    // スコアを計算するためのFunc
    private Func<int, int, int> scoreCalculator;

    void Start()
    {
        // スコアを加算するFuncを定義
        scoreCalculator = (currentScore, pointsToAdd) => currentScore + pointsToAdd;

        // 初期スコア
        int initialScore = 10;

        // ポイントを追加
        int newScore = AddPoints(initialScore, 5);

        // 結果を表示
        Debug.Log("New Score: " + newScore); // 出力: New Score: 15
    }

    // スコアを加算するメソッド
    public int AddPoints(int currentScore, int pointsToAdd)
    {
        return scoreCalculator(currentScore, pointsToAdd);
    }
}

コードの説明

他の例

次に、Unityでのコールバックとしての Func の使用例を示します。この例では、オブジェクトの移動を行うメソッドに Func を渡します。

using UnityEngine;

public class Mover : MonoBehaviour
{
    // 移動するためのFunc
    private Func<Vector3, Vector3> movementCalculator;

    void Start()
    {
        // 移動ベクトルを計算するFuncを定義
        movementCalculator = (input) => input.normalized * 5f;

        // 移動を実行
        Move(new Vector3(1, 0, 1));
    }

    public void Move(Vector3 direction)
    {
        // movementCalculatorを使用して新しい位置を計算
        Vector3 movement = movementCalculator(direction);
        transform.position += movement * Time.deltaTime;

        // 移動後の位置を表示
        Debug.Log("New Position: " + transform.position);
    }
}

まとめ