【问题标题】:Spawn several different prefabs each one in one different position with Unity Mapbox SDK使用 Unity Mapbox SDK 在一个不同的位置生成多个不同的预制件
【发布时间】:2018-05-01 11:11:02
【问题描述】:

我正在使用 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);
        }
    }
}

【问题讨论】:

  • 创建一个游戏对象数组,每次获取不同的索引并使用您放置的索引生成该对象。
  • 你想在没有任何输入的情况下生成不同的对象吗? (或)您尝试使用鼠标位置或单击等来生成对象。您的问题是哪一个?
  • @UgurTufekci 我想在游戏开始时生成,但是位置列表中有几个预制件的列表,按顺序为每个对象分配一个位置
  • 我将分享我的项目部分。我希望它会帮助你@Angelsm

标签: c# unity3d mapbox


【解决方案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()中调用这个方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多