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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

[Solved]: 3D objects in Canvas/Panel

Discussion in 'UGUI & TextMesh Pro' started by sygaki786, Jan 1, 2015.

  1. sygaki786

    sygaki786

    Joined:
    Jan 26, 2014
    Posts:
    142
    I am working on a 3d puzzle game. I would like to make use of the device independent resolution features of the new UI. Can I place my 3d pieces in one of the new UI containers such as the Canvas or Panel? Is this possible? Would that take care of resizing the 3d stuff for different resolutions so I don't have to bother adjusting my perspective camera's position/fov for different sizes.

    Thanks
    Syed
     
  2. cybervaldez

    cybervaldez

    Joined:
    Aug 16, 2014
    Posts:
    87
    I tried playing around and couldn't get this to work. But i'm very interested as well if this is possible :)
     
  3. cybervaldez

    cybervaldez

    Joined:
    Aug 16, 2014
    Posts:
    87
    Hi, I managed to make it work, just attach a "Rect Transform" to your game object inside the canvas and it behaves similar to a UI gameobject :)

    You can easily try that out but make sure you scale the game object big enough and set the canvas to use camera overlay(just to test it out)
     
    Last edited: Jan 3, 2015
  4. sygaki786

    sygaki786

    Joined:
    Jan 26, 2014
    Posts:
    142
    Thanks!!! I did try it out. I had to scale it at least 200 times to fit the outer Tile. Having to scale defeats the purpose of using the UI as a container. I was hoping to use the containers full stretch anchor to auto-size the child cube. The only reason I was trying this out was that when you right click on a UI element like a Panel etc there is a option to add 3d objects. I guess if this is not possible then they should remove that option from the menu.
     
    Last edited: Jan 4, 2015
  5. cybervaldez

    cybervaldez

    Joined:
    Aug 16, 2014
    Posts:
    87
  6. Sebioff

    Sebioff

    Joined:
    Dec 22, 2013
    Posts:
    210
    If you got Unity Pro you can alternatively render your object with a separate camera into a RenderTexture, then display that in your UI with a RawImage component.
     
    bartm4n and cybervaldez like this.
  7. cybervaldez

    cybervaldez

    Joined:
    Aug 16, 2014
    Posts:
    87
    Hi you're right i'd like to see how to create this possibility as well where the cube's vectors will correspond to the rect transform's width and height. Hopefully someone can teach us that :)
     
  8. sygaki786

    sygaki786

    Joined:
    Jan 26, 2014
    Posts:
    142
    It would seem that they would need a 3d container transform to anchor in the z direction too. Which is why I am surprised they gave an option to add 3d objects to the UI (in the menu) which is 2d with the ability to be positioned in 3d.
     
  9. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    you could use a render to texture? and render the scene on a texture inside a ui panel
     
    sygaki786 likes this.
  10. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    408
    It's not going to happen automatically, the anchors are only really functional for RectTransforms. While you can attach a mesh renderer to a RectTransform, it's not actually a Rect and won't scale with one.

    You could maybe write some clever code that scales the vertices of the mesh based on the RectTransform, but I wouldn't recommend it. There's just better ways of going about things.
     
    cybervaldez likes this.
  11. sygaki786

    sygaki786

    Joined:
    Jan 26, 2014
    Posts:
    142
    Finally got around to working on the game again. I followed the advice above to Render to Texture. Works like a charm!! Don't have to bother with camera positioning, World Space etc. Render to Texture to a RawImage within a Panel. Now you can anchor the panel to the canvas. You can also anchor the Raw Image to the Panel. You don't have to bother about resizing/coding for different resolutions. The anchoring takes care of it all. There is an example of RenderTexture in the new UI demo in the asset store (free).

    Thanks,
    Syed
     
  12. bartm4n

    bartm4n

    Joined:
    Jun 20, 2013
    Posts:
    57
    Yep. This solved all of my problems too when Unity 5 Personal was released with all features.
     
  13. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    syed, when it came to rendering the model to a texture, did you manually place it so that it fit withing your camera's view or did you find an algorithm to automatically find the ideal scale?
     
  14. sygaki786

    sygaki786

    Joined:
    Jan 26, 2014
    Posts:
    142
    That is a minor issue I was having now and I think I resolved it. So my secondary camera renders the object to texture and it gets rendered onto the panel. However, the camera not only captures the object but there is some empty space of the scene view around it which gets rendered too. I could play around with the camera settings in the editor to show only the object, but if the resolution changes then again you get the empty space around the object. I tried putting the object in its own layer and the camera in the same layer with the same culling mask. I then set the clear flags to Solid color. Now I don't see the empty space any more, no matter what resolution. This works but is there a better way of doing this - I mean focusing the camera to focus only on the gameobject and not the scene behind it, regardless of resolution? If somebody responds here that is fine, otherwise I will open a separate thread for it.

    Syed
     
  15. arun123gulshan

    arun123gulshan

    Joined:
    May 14, 2022
    Posts:
    1
    I am facing the same issue I Want to scale a 3d object according to the size of canvas can anyone help me.