Search Unity

Creating all game objects dynamically

Discussion in 'Scripting' started by omer_gulzar, Jan 2, 2019.

  1. omer_gulzar

    omer_gulzar

    Joined:
    Jan 2, 2019
    Posts:
    2
    I am working on a project where a json file needs to be read and depending on the json contents, game objects of different types needs to be created on a timeline. Each game object can have transforms and activation tracks.

    An example json

    Code (JavaScript):
    1. { objects: [
    2.                  { texture:"image1.png", position: "100,100", size: "100,100", start: "2.0", end: "8.0"},
    3.                  { texture:"image2.png", position: "200,100", size: "100,100", start: "4.0", end: "6.0"},
    4.                  { texture:"image3.png", position: "100,200", size: "100,100", start: "2.0", end: "4.0"},
    5.                  { texture:"image4.png", position: "300,300", size: "100,100", start: "6.0", end: "8.0"}
    6.                 ]
    7. }
    Can all of this be done through script ?
     
    Last edited: Jan 2, 2019
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yes, I recommend you read up on composition and scriptableobjects for real world examples. Also use code tags in forum posts, and post in the right part of the forum. I've moved it to scripting.
     
    omer_gulzar likes this.
  3. omer_gulzar

    omer_gulzar

    Joined:
    Jan 2, 2019
    Posts:
    2
    Thanks hippocoder
     
    hippocoder likes this.
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    (I don't know anything Unity can't do, it's used for pretty much everything. It would probably suck at trying to send a ship to Mars, actually.)
     
  5. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,982
    Just to chime in here, Im a senior hololens developer at my dayjob and in our research Im pretty sure I remember seeing that the NASA team used unity for a thing or two with the mars rover hololens thing (probably just mock up stage) but dont quote me on that as I cant find the source now that im searching for it again I cant find it (which is usually the case with any good source I find - I really need to start keeping a doc of this stuff). Although you can find their hololens + unity feasibility study still here: https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/20170004397.pdf

    So unity really is for all things I Guess!:D

    Edit: actually from reading this it looks like unity was used for the initial version of the curiosity rover hololens application :D
     
    Last edited: Jan 4, 2019
    omer_gulzar and hippocoder like this.
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    You can use any C# json parser, in Unity, to get the data. Your items are just 2D squares? Instantiate is fine for that. You can change textures while running (simple - just Search "unity change textures). You can _find_ textures with some sort of Resource.GetAsset, but it might be easier to pre-load them into a "texture holder" script and to search that by name. Figuring how to create items at the times will take some coding. To not make them twice or more, you'll need to either mark them, or move them into a different list. You can easily destroy items while running.

    For anything larger like this, all of the parts make it seem harder than it is. But it's not as it they all need to be done at once, before you can test any of them.
     
    omer_gulzar and hippocoder like this.