The Unity and C# scripts scale together, the respective GameObjects change, the spacing stays the same

eank007 注册会员
2023-02-26 13:10

You can zoom the entire map by adjusting the position of the generated tiles and obstacles, as well as the location of the map center. There are some implementation issues that need to be resolved.

First, you need to apply a change in the scale value tilesize to the creation of tiles and obstacles in the GenerateMap function. In your code, you've already used outinePercent * tilesize to scale for each tile and obstacle generation, but here outinePercent controls stroke, not size, so tilesize will be used instead.

Then, you need to work out the conversion relationship between the tile and obstacle positions before and after scaling so that you can move them to the correct position. You can calculate the position of each tile and obstacle before scaling as you generate them, and then move them to the position after scaling according to the scaling scale. This ensures that the distance between them remains the same.

Finally, you need to calculate the size of the scaled map and the location of the center int. You can calculate the zoom size based on the zoom scale and the original size of the map, and then apply it to the map. Also, you need to calculate the new center position based on the ratio between the size before and after scaling.

Here is a modified code example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapGenerator : MonoBehaviour
    public float tilesize = 1;
    public Transform Quad;
    public int seed;
    public Transform mapPoint;
    public Vector2 mapSize;
    public Transform tilePresf;
    public Transform obsPresf;
    [Range(0, 1)]
    public float obsPercent;
    List<Coord> coordsList;
    [Range(0, 1)]
    public float outinePercent;

    Queue coordQueue;
    Coord mapCenter;
    public Vector2 mapMaxSize;

    public void GenerateMap()
        coordsList = new List();
        if (mapPoint != null)
            mapPoint = new GameObject().transform;
            mapPoint.parent = transform;
        mapCenter = new Coord((int)mapSize.x / 2, (int)mapSize.y / 2);
        for (int x = 0; x < mapSize.x; x++)
            for (int y = 0; y < mapSize.y; y++)
                Transform newTile = Instantiate(tilePresf);
                float newX = (-mapSize.x / 2 + 0.5f + x) * tilesize;
                float newY = 0;
                float newZ = (-mapSize.y / 2 + y + 0.5f) * tilesize;
                newTile.position = new Vector3(newX, newY, newZ);
                newTile.localScale *= tilesize;
                newTile.parent = mapPoint;

About the Author

Question Info

Publish Time
2023-02-26 13:10
Update Time
2023-02-26 13:10