Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Entities 1.011 - Baking a player generated gameObject code

Discussion in 'Entity Component System' started by mk1987, Jul 31, 2023.

  1. mk1987

    mk1987

    Joined:
    Mar 18, 2019
    Posts:
    53
    Hi there,

    Im in the process of updating to unity 2022 and the latest version of entities. Feeling reasonably confident in that my mono code is working seemingly fine on the main scene and my fixed prefabs (weapons/ammo/engines etc.) generate nicely using the baker system in my entities subscene.

    In my game, all the main ships are procedurally generated using a monobehaviour 'builder' code from a simple load file, this means that players can make their own ships and the script will generate how it behaves and add in the various components and generate the meshes. As this wasnt hugely FPS sensitive i dont intend on converting it to ecs. The dots/ecs part is when you go to actually play the game and thus i had a conversion script that converted it pretty well in v0.51.

    I know how to go about creating a baker class for the ships, however im uncertain conceptually how to go about and structure things. My idea is the following.

    0) Initial State - Entity subscene is unloaded
    1) Ship Generations - Ships as GameObjects are generated in main scene.
    2 ) Add baker mono - Ships have baker monobehaviour component added to them in the gameobject, still in the main scene.
    3) Move ship models to subscene somehow
    4) Activate subscene somehow

    Stage 3/4 essentially is transferring those models with all the baker classes added into my entities sub scene and enable the entities subscene which i dont know how to do. I havnt found anything obvious that tells me how to do this having had a quick look at forums and docs (apologies if this is clearly covered somewhere).

    In editor mode, i can get it to work by dragging those ship models (with baker attached) into the entity subscene in play mode and it seems to convert it without issue. However im pretty sure that it wont work that way in an actual build (again correct me if im wrong, just my understanding of the documentation) and i dont know the code to do this programmatically anyway (ie. move a gameobject into a entity subscene).

    Thanks for any help you can give me :)

    (Ideal case - i would like the ships as prefabs as the game will have multiple instances of the same ship class, but thats not essential since my 0.51 runs really well anyway despite them not being prefabs. Pretty sure i cant do this)
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,975
    Baking is editor-only. It never happens at runtime. You'll need to rethink things if you were depending on runtime conversion.
     
  3. mk1987

    mk1987

    Joined:
    Mar 18, 2019
    Posts:
    53
    Thanks for the response, I had wondered if it could be done prior to starting a subscene as i had read a few forum post (possibly incorrectly) that implied that was case. I was using GameObjectConversionUtlity pre version 1.0 which is why i've been putting off moving to v1.0 for a while.

    The thing that is variable in my model is the Ship Armour Mesh and the number of Armour components as its shaped as per the players configuration (and a group of meshes is generated)..

    Is it still possible to create a RenderMesh at runtime? I was thinking i could create the armour into a Prefab with the relevant components and with a dummy mesh, then when creating the ships I would instantiate it that but change the MaterialMeshInfo reference ID to the newly created RenderMesh which EntitiesGraphicsSystem.RegisterMesh adds to the RendermeshArray at runtime. The collider is a separate mesh that sits on the main component and is simpler but again procedurally generated. If thats not possible to do, it would be great if you could let me know!

    I imagine for most people this is all they would need to do for this sort of thing. Im not working on this till next week now but when i get back to it and come to a working solution at build level I'll share what i came up with in case its useful for anyone else.
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,975
    Yes, although it is not a RenderMesh, but rather just a Mesh, and you don't need to touch RenderMeshArray as giving MaterialMeshInfo the absolute registered mesh ID will cause it to ignore the RenderMeshArray and just use the mesh directly.

    I won't comment on the collider as I don't use Unity Physics.
     
    mk1987 likes this.