使用Unity Mapbox SDK在一个不同的位置生成几个不同的预制件

问题描述 投票:0回答:1

我在使用Mapbox SDK的Unity游戏中工作。我有这个脚本在地图中只产生一个多个位置的预制件。如何在一个不同的位置产生几个不同的预制件?

public class SpawnOnMap : MonoBehaviour
{
    [SerializeField]
    AbstractMap _map;

    [SerializeField]
    [Geocode]
    string[] _locationStrings;
    Vector2d[] _locations;

    [SerializeField]
    float _spawnScale = 100f;

    [SerializeField]
    GameObject _markerPrefab;

    List<GameObject> _spawnedObjects;

    void Start()
    {
        _locations = new Vector2d[_locationStrings.Length];
        _spawnedObjects = new List<GameObject>();
        for (int i = 0; i < _locationStrings.Length; i++)
        {
            var locationString = _locationStrings[i];
            _locations[i] = Conversions.StringToLatLon(locationString);
            var instance = Instantiate(_markerPrefab);
            instance.transform.localPosition = _map.GeoToWorldPosition(_locations[i], true);
            instance.transform.localScale = new Vector3(_spawnScale, _spawnScale, _spawnScale);
            _spawnedObjects.Add(instance);
        }
    }

    private void Update()
    {
        int count = _spawnedObjects.Count;
        for (int i = 0; i < count; i++)
        {
            var spawnedObject = _spawnedObjects[i];
            var location = _locations[i];
            spawnedObject.transform.localPosition = _map.GeoToWorldPosition(location, true);
            spawnedObject.transform.localScale = new Vector3(_spawnScale, _spawnScale, _spawnScale);
        }
    }
}
c# unity3d mapbox
1个回答
1
投票

我使用struct作为坐标:

public struct Point
    {
        /// <summary>
        /// The x position
        /// </summary>
        public int X { get; set; }

        /// <summary>
        /// The y position
        /// </summary>
        public int Y { get; set; }

        /// <summary>
        /// Sets the values of the struct
        /// </summary>
        /// <param name="x">initial x</param>
        /// <param name="y">initial y</param>
        public Point(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }
}

然后我在另一个类中使用了这个结构,blueSpawn和redSpawn是不同的点,你可以像这样使用不同的预制件:

另一堂课:

 private Point blueSpawn, redSpawn;
            //this the method for your question
             private void SpawnPortals()
                {
                    //Spawns the blue portal
                    blueSpawn = new Point(0, 0);
                    Instantiate(bluePortalPrefab, Tiles[BlueSpawn].GetComponent<TileScript>().WorldPosition, Quaternion.identity);
                    //GameObject tmp = (GameObject)Instantiate(bluePortalPrefab, Tiles[BlueSpawn].GetComponent<TileScript>().WorldPosition, Quaternion.identity);
                   //You can save like a "tmp" if you need. 

                    //Spawns the red portal
                    redSpawn = new Point(11, 6);
                    Instantiate(redPortalPrefab, Tiles[redSpawn].GetComponent<TileScript>().WorldPosition, Quaternion.identity);
                }

TileScript和Portal是另一个类,但您可以忽略它们。顺便说一下你可以在Start()中调用这个方法

© www.soinside.com 2019 - 2024. All rights reserved.