Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to assign the destination world while converting a GameObject to an entity

Discussion in 'NetCode for ECS' started by sc03275sdo, Jun 13, 2022.

  1. sc03275sdo

    sc03275sdo

    Joined:
    Dec 26, 2021
    Posts:
    12
    The version of NetCode is 0.50.1.
    I want to change the content of an unityEngine.UI.Text in a SystemBase script.
    I'v tried three methods but all failed.

    There were two scenes named "Netcodes" and "SharedData",the SharedData Scene is the subScene of Netcodes Scene.
    In all the methods I created an emptyObject with an IComponentData Class which contains a public filed of type Text.I wanted to drag the Text into the filed on the inspector.

    In the 1st method,I add a "convert to entity" script to the emptyObject.I put both the emptyObject and the canvas in the Netcodes Scene.I Draged the Text to the field Success but when played the game ,the Text cannot be changed because the emptyObject had been converted to an entity in the Default World while the systemBase were updating in ClientWorld and ServerWorld.

    In the 2nd method,to convert the emptyObject into clientWorld and ServerWorld,Iput the emptyObject into the subScene "SharedData".However ,the public field of the IComponentData Class show Scene mismatch and I couldnot drag the Text into the filed again.

    In the 3rd method,I put both the Canvas and the emptyObject into the Scene "SharedData".This Time they were both in the clientworld and serverworld.But Unfortunately the Text had been convert into an Entitty too,I really dont know how to change the content of an text entitty.

    A possible solution is to convert the emptyObject to an entity into the assigned world.,But I didnt find a way.
    Does anyone know how to assign the destination world for an gameObject while converting?
    Or is there any other solutions to change the UI meta's value through systembase scripts?
     
  2. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    882
    You should use the ConvertToClientServerEntity for that purpose (if you want to a conversion outside a subscene) if you really want to go that way.

    Otherwise another viable alternatives are:
    - do exactly the contrary: update the text from a gameobject by getting some data populated by a system base (as class c# singleton, or entity or whatever you want)
    - Create a normal MononoBeaviour with the the text references, add that to the canvas (for example) and the retrieve it from the canvas GameObject from system for example

    For the 2n point: you can't use cross-scene/sub-scene references unfortunately.

    For the 3rd option: I don't recommend to put the canvas in subscene. It will not work.
    At best, you can load the canvas from another gameobject scene at runtime and then bind any reference as necessary.
    Also, UI.Text are not converted to entities at all (even if you put them on a subscene). We support some hybrid components, like MeshRenderer, TextMesh etc, but there is not UI.Text entity.
     
    sc03275sdo likes this.
  3. sc03275sdo

    sc03275sdo

    Joined:
    Dec 26, 2021
    Posts:
    12
    Its really helpful.I replaced the script "convert to entity" by "convert to client server entity" as you told me.Now I can update the content of the text as I hoped. Thank you very much!