Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can I drag entities into the component's inspector?

Discussion in 'Project Tiny' started by UniversalCreative, May 20, 2019.

  1. UniversalCreative

    UniversalCreative

    Joined:
    Apr 4, 2019
    Posts:
    9
    I have an entity with a UICanvas component. It has entity children with UI Text components. I want to be able to change the text at runtime.

    I could find entity by name, but that makes me cringe.

    I want to make a new component, called TextUpdateSystem (or something). This takes in a TextUpdateComponent. As fields of the TextUpdateComponent are references to the entities with UI Text components, so that I can change their text during runtime.

    How can I drag entity references into the TextUpdateComponent? I have tried things like the EntityReference field, but there is no field to drag an entity into.

    (This may be going against the conventions of ECS? If so, what is the correct approach to edit text during runtime?)
     
  2. reallyhexln

    reallyhexln

    Joined:
    Jun 18, 2018
    Posts:
    69
    You need to attach your TextUpdateComponent to some entity before you will be able to fill their fields.

    Personally, I use component tags (component that has no fields) to identify my text fields.
    If I want to update text of some UserNameEntity field, I create component UserNameTag with no fields, attach that component to my text field UserNameEntity, and then reference it in my code:

    Code (JavaScript):
    1. this.world.forEach([game.UserNameTag, ut.Text.Text2DRenderer], (tag, renderer) => {
    2.     renderer.text = "New User Name";
    3. });
    Using of EntityReference's may be useful when you have a set of similar data that should be updated together.
    For example, you have component UserData with the following fields:
    - name (EntityReference type)
    - status (EntityReference type)

    You may create the following entity tree:
    UserPanelEntity
    |--> UserNameEntity
    |--> UserStatusEntity

    Next, you attach UserData component to UserPanelEntity.
    Finally, you select UserPanelEntity, drag UserNameEntity to name field of UserComponent, then drag UserStatusEntity to status field of UserComponent.

    Usage:

    Code (JavaScript):
    1. this.world.forEach([game.UserData], (user) =>
    2.     this.world.usingComponentData(user.name, [ut.Text.Text2DRenderer], (field) => {
    3.         field.text = "New User Name";
    4.     });
    5.  
    6.     this.world.usingComponentData(user.status, [ut.Text.Text2DRenderer], (field) => {
    7.         field.text = "New User Status";
    8.     });
    9. );
     
    UniversalCreative and Zionisias like this.