Search Unity

UI Image + Box Collider

Discussion in 'UGUI & TextMesh Pro' started by pastaHead2, Feb 6, 2015.

  1. pastaHead2

    pastaHead2

    Joined:
    Dec 24, 2014
    Posts:
    7
    Hello, I have an UI image "Spikes" on the top of screen (on any resolutions). I want to handle my character collision with these spikes, but I can't just add box collider to the UI Image object.
    What should I do? (It has to work on different resolutions like on iphones/ipads) Thanks!
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    be aware that Canvas Rendering is not inline with the full 3D rendering of the Unity engine. As such it doesn't play very well with physics.
    It will work but there is a certain amount of Lag to it (at least last time I tried doing a physic game I the UI for fun)

    As a simple test, just add a collider 2D and RigidBody 2D to a UI object, hit play and watch.
     
  3. pastaHead2

    pastaHead2

    Joined:
    Dec 24, 2014
    Posts:
    7
    Yeah, I realized that there's something wrong with UI and physics. Ok, So what should I use instead of physics? I have to check whether or not my character touches these spikes
     
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Don't draw the 2D elements in a canvas (in other words don't use UI if you need physics)
    Use the Ui for on screen elements and build the game as normal using the 2D system.
     
  5. pastaHead2

    pastaHead2

    Joined:
    Dec 24, 2014
    Posts:
    7
    But it is very convenient to place some sprites bounded to the screen side/corner via editor (not a code), especially if you work with a different resolutions. Is there a way to do it without UI elements?

    I used cocos2d-x and I could change, for example, some sprite's pivot to Y = 1, and then bound it(sprite) to the top of screen at a distance of % screen height
     
    Last edited: Feb 7, 2015
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Just means managing your sprite resolution and position manually, for example, check out these posts on Unity Answers:
    http://answers.unity3d.com/questions/687230/2d-scaling-sprites-by-screen-resolution.html
    http://answers.unity3d.com/questions/773525/keep-sprites-in-the-same-position-relative-to-scre.html

    Resolution management is a tricky thing. The new UI does a great job of it but isn't suited to full physics implementations.
    You might be able to get away with it for small things but until the lag/performance issues are sorted, it isn't best suited.
     
  7. ink13

    ink13

    Joined:
    Nov 14, 2013
    Posts:
    37
    Is this still the case? I'm having a look at it in unity 2017.3.1f1 and it seems reasonably performant. Have enough improvements been made along the way that we can get away with doing it?
     
  8. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    I'm trying to attach a 2D circle collider to a UI image, but I'm not sure it's working. There's nothing visible in the scene view, no outline like a collider normally shows. Is there anyway to see if the collider is attached to the image? I need to know when a button that's dragged around enters or leaves another button's location.
     
  9. max_payne

    max_payne

    Joined:
    May 24, 2016
    Posts:
    1
    You can add 2d collider to a UI image but it appears very small then you expect, for this click on edit collider and you will see its size ready to edit, or you can just increase radius by increasing radius property.
     
    atha11 likes this.