Topic: Batch Object Placer

Hi there

I would love to read in a CSV of coordinates and batch place a bunch of the objects there (without having to use the Object Placer to do each one individually). I have had a bit of a dig around in the code, but ended up getting a bit confused.

Is there any example script that could get me started on this?

Thanks for your help

Stuart

Re: Batch Object Placer

Hello.

Example:

using System.Collections.Generic;
using InfinityCode.RealWorldTerrain;
using UnityEditor;
using UnityEngine;

public class BatchPlacer: ScriptableWizard
{
    public GameObject prefab;
    public RealWorldTerrainContainer container;
    private List<Vector2> coords;

    [MenuItem("Window/Batch Placer")]
    static void CreateWizard()
    {
        BatchPlacer wizard = DisplayWizard("Batch Placer", typeof(BatchPlacer)) as BatchPlacer;
        wizard.container = FindObjectOfType<RealWorldTerrainContainer>();
    }

    void OnWizardCreate()
    {
        LoadCSVData();
        PlaceObjects();
    }

    private void PlaceObjects()
    {
        foreach (Vector2 coord in coords)
        {
            GameObject go = Instantiate(prefab);
            Vector3 pos;
            container.GetWorldPosition(coord, out pos);
            go.transform.position = pos;
        }
    }

    private void LoadCSVData()
    {
        // Here you load your CSV data

        GenerateDummyData(); // Generate dummy data
    }

    private void GenerateDummyData()
    {
        float tx = (float)container.leftLongitude;
        float ty = (float)container.topLatitude;
        float bx = (float)container.rightLongitude;
        float by = (float)container.bottomLatitude;

        int count = 20;
        coords = new List<Vector2>();
        for (int i = 0; i < count; i++)
        {
            coords.Add(new Vector2(Random.Range(tx, bx), Random.Range(by, ty)));
        }
    }
}
Kind Regards,
Infinity Code Team

Re: Batch Object Placer

That is awesome - thank you so much for the prompt reply! I'll get going and see what I can do.

Thanks again for such a great asset!