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?

More than 5 years have passed since last update.

Unity雑記(素人のチラ裏)

Last updated at Posted at 2019-07-13

Unity開発

初めてunityを触る。
再生を押すと画面に表示された青い画面......。

これから長く辛い(予定)Unityの開発がはじまる.......。

デバッグツールが欲しい

まず画面を押した時に押した軌跡を表示したかった。


public class Player : MonoBehaviour
{
    List<Vector2> g_moveTowards = new List<Vector2>();

    // カウント
    int g_i = 1;

    // 実際に線を記述する
	void Update ()
    {
        if (g_moveTowards.Count > g_i)
        {
            // タップしている間、線を書く
            Draws(g_moveTowards[g_i - 1], g_moveTowards[g_i]);
            g_i++;
        }
    }

    // 入力を受け付ける
    private void FixedUpdate()
    {
        // 押している間
        if (Input.GetMouseButton(0))
        {
            // タップ地点を格納する
            Vector2 m_posi = Input.mousePosition;
            g_moveTowards.Add(transrate(m_posi));
        }

        // 離した時
        if (Input.GetMouseButtonUp(0))
        {
            // 線を消す
            ResetHard();
        }
    }

    /// <summary>
    /// タップ地点をカメラからの座標に変換する
    /// </summary>
    /// <returns>The transrate.</returns>
    /// <param name="position">Position.</param>
    private Vector2 transrate(Vector2 position)
    {
        Vector2 m_tapPoint = Camera.main.ScreenToWorldPoint(position);
        return m_tapPoint;
    }

    /// <summary>
    /// 線を描く
    /// poolがあればそこから出す
    /// </summary>
    /// <param name="p1">始点</param>
    /// <param name="p2">終点</param>
    private void Draws(Vector2 p1,Vector2 p2)
    {
        if (IsChild(p1,p2))
        {
            // プールがなければ作る
            GameObject line = new GameObject();
            line.AddComponent<LineRenderer>();
            LineRenderer lineren = line.GetComponent<LineRenderer>();

            // 線の幅を決める
            lineren.startWidth = 0.1f;
            lineren.endWidth = 0.1f;

            // 頂点の数を決める
            lineren.positionCount = 2;
            lineren.SetPosition(0, p1);
            lineren.SetPosition(1, p2);

            // このオブジェクトを親に
            line.transform.parent = this.transform;
        }
    }

    /// <summary>
    /// 書いた線を見えなくする
    /// </summary>
    private void ResetHard()
    {
        // 線を見えなくする
        foreach (Transform line in this.transform)
        {
            line.gameObject.SetActive(false);
        }
        g_moveTowards.Clear();
        g_i = 1;
    }

    /// <summary>
    /// オブジェクトプール
    /// </summary>
    /// <returns><c>true</c>, if child was ised, <c>false</c> otherwise.</returns>
    /// <param name="p1">P1.</param>
    /// <param name="p2">P2.</param>
    private bool IsChild(Vector2 p1, Vector2 p2)
    {
        foreach (Transform line in this.transform)
        {
            if (!line.gameObject.activeSelf)
            {
                line.gameObject.SetActive(true);
                LineRenderer lineren = line.GetComponent<LineRenderer>();
                lineren.positionCount = 2;
                lineren.SetPosition(0, p1);
                lineren.SetPosition(1, p2);
                return false;
            }
        }
        return true;
    }
}

参考画像
スクリーンショット 2019-07-13 18.12.38.png

作った。qiitaを駆け回り生まれしキメラ.......。調子に乗ってObjectPoolなる概念も取り入れた。
効率に関しては不明。(なぜピンク?)

疑問点

グローバルで宣言した

// カウント
int g_i = 1;

こいつが非常に気になる

また記事にハイライトがなく見辛い

学んだこと

・GameObjectの生成
・オブジェクトプール
・親子関係
・入力受付(クリック)

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?