ユニティ-マップとグリッドの動的描画(2D)



Unity Dynamic Drawing Maps



周囲のニンジンマップに基づく
ゲーム開発では、多くの場合、マップとグリッドスタイルを動的に描画する必要があります。通常、forループなどを使用して描画します。
まず、私たちの考えを試してみましょう。グリッドを描画しているので、最初にいくつか質問があります。描き方は?絵はどこにありますか?絵の大きさはどれくらいですか...これらはすべて問題ですか?次に、それを段階的に解決します
画像
まず、マップとそれを操作するためのグリッドの幅と高さを取得する必要があります。作者はここに背景ボードを持っていて、カメラ全体のレンダリングで覆われているので、カメラ全体の幅と高さを直接計算し、次のステップは比率を使用して計算することです。 )、Vector3は実際には構造タイプのクラスであり、右上隅はVector(1、1、1)です。
画像
さらに下に行くと、ビューポート座標全体がワールド座標系Camera.main.ViewportToWorldPoint(leftDown)に変換されます。
この場合、ビューポート座標から世界座標系APIが使用され、3D数学が設計されます。注意すべきことの1つは、Unityがメインカメラをマークすることです カメラのタグをカメラに設定する必要があります 、作成者がここにnullポインタを持っているため。 。 。
これらのデータを使用して、マップとグリッドの幅と高さを取得できます。
画像
OK、これらすべてが完了したら、内部の直接データを取得するのと同じです。次のステップは線を引くことです。この種の関数OnDrawGizmos()は次のとおりです。これはすべてのフレームで呼び出されます。
画像
次のステップは、行と列の数を描画することです
:最初に、グリッドとマップのサイズ情報を取得してから、0から始まりで終わるforループを使用します。 行の総数 、注意すべき点は、行の総数を含めることです。含まれていない場合、最終行の行は表示できません。ループの内側は、ループの開始と終了です。 2つのVector3を使用して、開始点と終了点を記録します。
開始位置のX軸=-マップ幅/ 2
y軸=-マップの高さ/ 2 + y(循環変数)*グリッドの高さ

終了位置x軸=マップ幅/ 2
y軸=-マップ幅/ 2 +行の総数*グリッドの高さ

次に、Gizmos.DrawLine(startPos、endPos)を使用して2つのパラメーターを渡し、線の描画を開始します。



列の数 :0から始まるforループを引き続き使用します。 最大値は列の総数です 、セルフインクリメントを実行するたびに、列の総数を含める必要があることに注意してください。含まれていない場合、最後の列の行は表示できません
開始位置のx軸=-マップ幅/ 2 +グリッド幅* x(ループ変数)
y軸=マップの高さ/ 2
終了位置x軸=-マップ幅/ 2 + x(ループ変数)*グリッド幅
y軸=-マップの高さ/ 2
次に、Gizmos.DrawLine()を使用して線の描画を開始します。

OK、これらすべてが完了すると、マッププレハブが生成されます。まず、マップとグリッドの幅と高さを取得します。次に、forループをネストします。行数は最大行数、列数は最大列数です。次に、グリッドオブジェクトが生成され、プロパティが設定されます。プロパティの設定に問題があります。つまり、すべてのプレハブを1つに直接設定すると、ゲームオブジェクトの下にオブジェクト全体が積み上げられますが、これは明らかに望ましい効果ではないため、を返すメソッドが必要です。新しいポジション
プレハブの開始位置のx軸は次のとおりです。x(ループ内の行数)*グリッドの幅-マップの幅/ 2+グリッドの幅/ 2
y軸は次のとおりです。y(ループ内の行数)*グリッドマップの高さ/ 2+グリッドの高さ/ 2



次に、Awake()メソッドで呼び出します。

//========================== // - FileName: MapMaker.cs // - Created: true. // - CreateTime: 2020/03/16 14:27:09 // - Email: root@xxxxx // - Region: China WUHAN //-Description: spelling of the map grid //========================== using System.Collections using System.Collections.Generic using UnityEngine public class MapMaker : MonoBehaviour { //Switch attributes, line drawing switch public bool drawLine //Map width private float mapWidth //high private float mapHeight //Grid width private float gridWidth //Grid height private float gridHeight //Rows public const int yRow = 8 //Number of columns public const int xColumn = 12 //Adding point of the tower public GameObject gridGo //Not a singleton in the game, but a singleton in the tool private static MapMaker _instance public static MapMaker Instance { get { return _instance } } private void Awake() { _instance = this InitMap() } //Initialize the map public void InitMap() { CalculateSize() //Generate grid for (int x = 0 x < xColumn x++) { for (int y = 0 y < yRow y++) { //Generate grid object GameObject itemGo = Instantiate(gridGo, transform.position, transform.rotation) //Set attributes itemGo.transform.position = CorretPositon(x * gridWidth, y * gridHeight) itemGo.transform.SetParent(transform) } } } //Correct the starting position of the prefab public Vector3 CorretPositon(float x,float y) { return new Vector3(x - mapWidth / 2 + gridWidth / 2, y- mapHeight / 2 + gridHeight / 2) } //Calculate the width and height of the map grid private void CalculateSize() { //Lower left corner Vector3 leftDown = new Vector3(0, 0) //Upper right corner Vector3 rightUp = new Vector3(1, 1) //Viewport coordinates to world coordinates, world coordinates in the lower left corner Vector3 posOne = Camera.main.ViewportToWorldPoint(leftDown) //Upper right corner Vector3 posTwo = Camera.main.ViewportToWorldPoint(rightUp) //Map width mapWidth = posTwo.x - posOne.x Debug.Log('The width of the map' + mapWidth) //Map height mapHeight = posTwo.y - posOne.y //The width of the grid gridWidth = mapWidth / xColumn //Grid height gridHeight = mapHeight / yRow } //Draw grid private void OnDrawGizmos() { Debug.Log('Start drawing grid') //Draw a line if (drawLine) { //Calculate the size of the grid CalculateSize() //The color of the grid Gizmos.color = Color.green //Draw the number of lines The value here should be equal to the number of lines for (int y = 0 y <= yRow y++) { //starting point Vector3 startPos = new Vector3(-mapWidth / 2, -mapHeight / 2 + y * gridHeight) //End point coordinates Vector3 endPos = new Vector3(mapWidth / 2, -mapHeight / 2 + y * gridHeight) //Draw a line Gizmos.DrawLine(startPos, endPos) } //Draw a column for (int x = 0 x <= xColumn x++) { Vector3 startPos = new Vector3(-mapWidth / 2 + gridWidth * x, mapHeight / 2) Vector3 endPos = new Vector3(-mapWidth / 2 + x * gridWidth, -mapHeight / 2) Gizmos.DrawLine(startPos, endPos) } } else { return } } }

最後に効果を見てください
画像