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

Framing/Scalign the camera correctly?

Discussion in 'Project Tiny' started by MatheusCohen, May 6, 2019.

  1. MatheusCohen

    MatheusCohen

    Joined:
    Aug 24, 2017
    Posts:
    57
    In normal unity, when i need to scale my game to any screen i get the bounds of all my objects, create a large bound and set my orthographic size based on that bound.

    how would i accomplish that in tiny? as i can't seem to find bounds in tiny so i imagine it's not implemented(yet?)

    also, if i would do the same for UI, how can i also achieve this? as the auto-scaling of the canvas is not helping me. As you can see in the image below, i need the 6 yellow rectangles to fit in the screen in any resolution and they are currently being cutoff in mobile.
    WhatsApp Image 2019-05-06 at 10.44.13.jpeg


    sorry for any grammar, english is not my native language :)
     
  2. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    131
  3. MatheusCohen

    MatheusCohen

    Joined:
    Aug 24, 2017
    Posts:
    57
    was trying it all morning...it was not working...i managed a workaround using it, so thnx anyway!

    btw, would you happen to know(or point out a thread as i couldn't find one) how to Drag a UI element(only found about non-ui element) :p
     
  4. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    131
    Haven't tried this myself, but I think you could just move the
    RectTransform.anchoredPosition
    to the current mouse location when mouse is down on the UI element (make sure to add the MouseInteraction component to the UI element for this to work).

    Something like this:
    Code (CSharp):
    1. this.world.forEach([ut.Entity, ut.UIControls.MouseInteraction, ut.UILayout.RectTransform], (entity, mouse, transform) => {
    2.     if (!mouse.down) { return; } //Don't do anything if mouse isn't down on the UI element
    3.  
    4.     //Get mouse position in pixels
    5.     let displayInfo = this.world.getConfigData(ut.Core2D.DisplayInfo);
    6.     let displaySize = new Vector2(displayInfo.width, displayInfo.height);
    7.     let inputPosition = ut.Runtime.Input.getInputPosition();
    8.  
    9.     //Convert to world-space units
    10.     let mousePosRaw = ut.Core2D.TransformService.windowToWorld(this.world, this.world.getEntityByName("Camera"), inputPosition, displaySize);
    11.  
    12.     //Convert to canvas/recttransform units
    13.     let canvasHeight = 400; //just in my case, look in the inspector to find out
    14.     let worldHeight = 21; //just in my case, look in the editor to find out
    15.     let worldToCanvasUnitsConversionRate = canvasHeight / worldHeight;
    16.     let mousePos = new Vector2(
    17.         mousePosRaw.x * worldToCanvasUnitsConversionRate,
    18.         mousePosRaw.y * worldToCanvasUnitsConversionRate
    19.     );
    20.  
    21.     //Set new position of UIElement
    22.     //(depends on anchoring of the rectTransform)
    23.     transform.anchoredPosition = mousePos;
    24. });
    (Note that this only works if the UIElement is not scaled or moved by parent entities; if the entity exists in parent UI elements with scaling or position changes, this code will result in the element following your mouse with an offset and/or multiplied by scale)
     
    Last edited: May 7, 2019
  5. MatheusCohen

    MatheusCohen

    Joined:
    Aug 24, 2017
    Posts:
    57
    isn't canvas already on screen-space units? i'll try the code anyway, i assume that with height you are referring to the ortho? so i'll just catch the ortho of my canvas and camera and double!

    Edit: worked great on pc, but on mobile portrait it doesn't work properly(has a negative offset) and mobile landscape doesn't work at all :p
     
    Last edited: May 7, 2019
  6. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    131
    RectTransform units are not the same as the units that "normal" entities use.
    You can see this because if you move a "normal" entity's
    ut.Core2D.TransformLocalPosition.position
    values by 10, the entity will move a whole lot more than when you move a UI entity's
    ut.UILayout.RectTransform.anchoredPosition
    values by the same amount.

    For the canvasHeight, you take
    referenceResolution.y
    from your UI Canvas Component on your "Canvas" entity.
    For the worldHeight, you take twice the
    halfVerticalSize
    from your Camera Component on the camera entity that renders your gameworld entities.
     
  7. MatheusCohen

    MatheusCohen

    Joined:
    Aug 24, 2017
    Posts:
    57
    i did exactly that for the height :)

    regarding the units...yes that's the same thing:
    UI - uses screen space
    Entity - uses world space

    so i fail to understand why you go from screen space(with the input) to world space back to screen space(for the UI)
    at least that's how it works on normal unity, maybe i'm talking s*** here :p
     
  8. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    131
    That's because the first screen-space is browser-window-screen space in pixels.
    The second screen-space is canvas-screen space.