Search Unity

Place a Plane to a Canvas in order to be in specific screen possition

Discussion in 'UGUI & TextMesh Pro' started by kordou, Mar 20, 2018.

  1. kordou

    kordou

    Joined:
    May 16, 2017
    Posts:
    13
    Hi there,
    I am trying to place an Plane Obj inside a canvas in order to be able to maitain a specific possiotion and size relative to screen for a mob app. I tried it a s child obj to a GUI object (image of button) but it dissapear from my screen.

    can someone help me on how i do that ?

    thanks in advanced
     
  2. Beks_Omega

    Beks_Omega

    Joined:
    Jun 7, 2017
    Posts:
    72
    Do you mean you want it to take up a specific percentage of the screen (e.g. always be 1/2 the height and 1/4 the width)? Or do you want it to always be 2cm by 3cm, independent of resolution?

    For now this might help ya.
     
  3. kordou

    kordou

    Joined:
    May 16, 2017
    Posts:
    13
    Hi, My problem is that i can do that for UI elements but not for GameObjects e.g. a plane.

    i want to do exactly that but for a game object.

    I thought one way is to add an GUI object e.g. a button, set the ancor points and then add as a child my Gameobject and se relative position to my parrent. Unfortunatelly that does not work
     
  4. Beks_Omega

    Beks_Omega

    Joined:
    Jun 7, 2017
    Posts:
    72
    Ok this won't work under every circumstance, because I don't know enough about manipulating matrices to make something that's perfect. But it does work!

    You set it up the same way you had done it already, adding the plane as a child of a RectTransform. Then you add this script to the plane & set the rectToMatch to be the parent.

    Code (CSharp):
    1. public class PlaneMatchScale : MonoBehaviour {
    2.  
    3.     public RectTransform rectToMatch;
    4.  
    5.     void Update () {
    6.         //we get the size based on the world corners instead of the
    7.         //size delta because we want to /sort of/ set the "global"
    8.         //scale of the plane
    9.         //size delta is relative to the RectTransform's anchors
    10.         Vector3[] corners = new Vector3[4];
    11.         rectToMatch.GetWorldCorners(corners);
    12.  
    13.         //distance from bottom left to top left
    14.         float worldHeight = Vector3.Distance(corners[0], corners[1]);
    15.         //distance from bottom left to bottom right
    16.         float worldWidth = Vector3.Distance(corners[0], corners[3]);
    17.  
    18.         //transform == the plane's transform
    19.         Vector3 scale = new Vector3(worldWidth / 10f, transform.localScale.y, worldHeight / 10f);
    20.         transform.localScale = scale;
    21.     }
    22. }
    The script matches the size of the plane to the size of the rectToMatch (the parent), even when the rectToMatch is set to size itself relative to the screen.

    This will only work if the scale of all of the plane's parents are set to Vector3.one, because we are setting the plane's local scale. If you are doing any manipulation of scale on the plane's parent objects this will be derpy, just a warning.

    Things I Used:
    GetWorldCorners
    Vector3.Distance
     
  5. kordou

    kordou

    Joined:
    May 16, 2017
    Posts:
    13
    HI, thnanks a lot the code :)
    but not working :( i am attaching a prefab of a canvas with an text and a simple cube. have i done something wrong ? as i can read from the script it should indeed working with local scale of parent equal to 1


    thnks for helping :)
     

    Attached Files:

  6. Beks_Omega

    Beks_Omega

    Joined:
    Jun 7, 2017
    Posts:
    72
    Planes are different than cubes. Planes are by default (at scale 1,1,1) 10 units by 10 units while cubes are 1 unity by 1 unit. Planes can also only be scaled along the x and z while depending on the orientation of a cube you could want it to be scaled along different axis.

    Changing this line:


    Vector3 scale = new Vector3(worldWidth / 10f, transform.localScale.y, worldHeight / 10f);


    To this line:


    Vector3 scale = new Vector3(worldWidth, worldHeight, transform.localScale.z);


    If that doesn't work you probably just need to play around with which axis you're scaling &/or rotate your cube.
     
  7. kordou

    kordou

    Joined:
    May 16, 2017
    Posts:
    13
    hi again. thnks for helping !!
    My goal is for a plane. I did try your first solution with a plane but nothing (with parent scale 1). If you change the cube with a plane you will see that it is not working for some weird reason. I will try the cube also with your second code3 and i will tell you!

    many thanks again!!!