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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Text in World space with collision

Discussion in 'UGUI & TextMesh Pro' started by ZannaU, Apr 29, 2015.

  1. ZannaU

    ZannaU

    Joined:
    Mar 24, 2013
    Posts:
    17
    Hi,

    I would like to create some elements in my product that contains text but also have collisions.
    These are the requirements:
    1- Elements have a background image
    2- Elements contain text which needs to be dynamic to support localization.
    3- Elements will resize based on the text with option to decide on vertical or horizontal resize.
    4- Elements have 2d Colliders

    Requirements 1 to 3 are already satisfied by the current UI system so it would be great to simply being able to add a 2D box collider to it. I tried to create a canvas in world coordinates but unfortunately, due to the very small scale on the canvas, I get the following error when i add a 2D collider to the element.
    "The Collider did not create any collision shapes as they all failed verification"
    Is there a workaround to this problem?

    Alternatively I could create an element with a sprite in the world with collision and find a different way to add text to it. This would require quite a bit of extra work to re-create the resize logic, the image sliced borders etc... Do you guys have any suggestion in this direction? Maybe the old OnGUI?

    Thanks for your help
     
  2. ZannaU

    ZannaU

    Joined:
    Mar 24, 2013
    Posts:
    17
    Sharing my solution in case someone else faces the same problem.

    I used the new UI for the graphical part. Then I created a script that run only in the editor and dynamically create new game objects with a BoxCollider2D that match the UI elements size and position every time the scene is updated.
    I kept the new collision objects outside the canvas hierarchy so they are not affected by the scale issue.

    Works well so far and it was pretty fast to implement.
     
  3. GordonWedge

    GordonWedge

    Joined:
    Jul 6, 2013
    Posts:
    23
    Hi, nice solution, can you share some code, i never created editor only scripts.
    thanks :)