Search Unity

Is the hierarchy data scriptable?

Discussion in 'Scripting' started by rickblacker, Jan 22, 2019.

  1. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Lets say I have an XML file, that XML file represents various objects that I want to be attached to the hierarchy. Is there a way that I can create a brand new Unity project, run a script that, when ran, would dynamically create empty game objects or better yet, create a prefab and populate it with the data from the XML file and save it to the hierarchy?

    Asking because we are working on a project where we have LOTS of objects that need to be created, and ideally, creating an XML file that could contain meta data about each prefab, then running a script to dynamically create and attach the prefab to the hierarchy would save a lot of time...

    Thanks!
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,706
    Yes. Some games even define their scenes entirely in an external format such as XML and assemble them into Unity scenes at runtime.

    I imagine you'd want to do this in a project that already has art assets and probably some kind of table that your XML file can use to match its data to art assets.

    Your programmer(s) are going to want to lean heavily on Unity's Scripting API, such as EditorSceneManager and PrefabUtility.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Everything @TonyLi says is true: you can make and interrelate and link GameObjects and Monobehaviors all day long in Unity.

    However, use caution: the more interesting object you want to make, the more you are going to have to reinvent the wheel, versus just using the extant Unity prefab and/or scene workflow.

    For instance, lets say you are making a bunch of combat hovercraft with engines and weapons on them.

    Ideally you want them all controlled by the same controller, supplied with parameters such as engine power, type of guns, etc.

    With the prefab system I would import the 3D object(s), attach and parent them in the editor, connect the important bits to the scripts (the engines might glow, or they emit particles, the weapons might each have a pivot point and be connected to a custom type of weapon controller (laser vs artillery), and the weapons might also have a recoil animation, plus there might be a particle that appears at its muzzle, which is another transform point), etc.

    When I am done I press play and see how the object looks and works. I never get it right the first time, and I go back and forth a dozen or more times tweaking positions and rotations and offsets. If I misconnected something, I can drag it instantly and fix it. When it's all perfect, as best I can say, I make a prefab and move on.

    With XML, you lose ALL of that rapid iteration, and you basically have to make parsers and logic to reconnect EVERYTHING I listed in the simple weapon-equipped vehicle I listed above.

    And if there's something wrong, well, go back and edit your XML... or is it a bug in your XML to GameObject maker? Hm, good question!

    Good luck. I would NOT select that path except for the most trivial of things with very few interoperating parts.
     
  4. You will need the ExecuteInEditMode and/or CustomInspector, Custom Editor Window. You can extend your editor quite much, you also can read up files and parse them so you can span both game objects and prefabs, however this last one in the latest Unity versions a little bit more complicated due to the new nested and override nature.
    Look for the "extending the unity editor" -like learn sections and tutorials.
     
  5. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Thanks all. I have not started this yet, but good knowing what the options are.