TransWikia.com

Instantiate prefabs at particular vectors using text file (Unity3D game)

Game Development Asked by Kandel on November 2, 2021

I need to read a text file containing two pieces of information on each line, and use that information to instantiate a prefab at the correct location in a Unity3D game (c# script). The locations are either North, South, East, or West (each a particular vector).

For example, the first line of the text file might read:
landmark1, North

In a script, I need to instantiate landmark1 and assign it to case North vector.

The below script is what I’ve got so far. Basically, I can read from the text file but I don’t know how to assign that information to different cases. I’m guessing I need to use an array somehow but I’m not sure how to do that here.

Any help would be appreciated. Thanks!

using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using System.Linq;
using System.IO;
using System;

public class Landmarks : MonoBehaviour {

    //spawnable prefab gameobjects
    public GameObject castle;


    // Use this for initialization
    void Start()
    {

        StreamReader reader = new StreamReader(@"C:/Users/.../landmarks.txt"); //insert full path here
        String textContents = reader.ReadToEnd();

        // string[] dataArray = 

        for (int i = 0; i < (textContents.Length); i++)
        {

            //set case if first line = "castle, North" then instantiate object castle at vector 'north';

        }
    }
}

One Answer

Here's how I'd approach an issue like this.

  1. Define an enum for each set of special keywords you want to reference in your file. eg.

    [System.Serializable]
    public enum Landmark : int {
       Castle,
       Mountain,
       Forest,
       Temple
    }
    
    [System.Serializable]
    public enum Site : int {
       North,
       East,
       South,
       West
    }
  2. Associate these enumeration values with their corresponding prefabs / vectors inside your game.

    In this answer, I show a method to create a friendly, inspector-editable list with entries for each enumeration value, that automatically adjusts if you add / remove / re-order values in your enumeration.

  3. Use a structured data format for your input file - I'd recommend something standard and in common use like JSON or XML.

    There are existing deserialization libraries for these data formats that will do the text parsing for you. So instead of manipulating strings of text yourself, you can let the deserializer digest it into a structured format something like this:

    [System.Serializable]
    public struct LandmarkLocation {
        public Landmark landmark;
        public Site site;
    }
  4. Now you can iterate over a collection of structured entries like the one above returned by the deserializer, look up the corresponding prefab / location from your table, and perform the instantiation. It'll look something like this:

    LandmarkLocation[] locations = MyDeserializer.Deserialize(text);
    
    foreach(var location in locations) {
        Transform prefab = landmarkIndex.GetPrefab(location.landmark);
        Vector3 position = siteIndex.GetVector(location.site);
        Instantiate(prefab, position, Quaternion.identity);
    }

    The advantage of this style is clean, readable code, without a lot of messy string parsing fussing in the middle of your gameplay logic. It lets the battle-tested deserializer do what it's good at, with a clear contract established by the enum and struct types to ensure your data is clean and conforms to the expected structure. If it doesn't, the deserializer can handle detecting & generating the appropriate errors.

Answered by DMGregory on November 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP