• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ProMidgetWrestling · Aug 10, 2015 at 12:40 PM · uicanvasscalerecttransform

Editing RectTransform scale

I am trying to make a control room system, in which there are 5 monitors, with 1 main monitor and 4 side monitors, and I want to be able to click on the side monitors and they appear on the large main monitor. I attempted to do this by moving just the canvases, but the scale keeps distorting, and lossyScale.Set(); doesn't seem to change it. Any ideas would be appreciated, do you think moving the canvas is the best way to go or perhaps I should take a different approach?

My code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ScreenSwap : MonoBehaviour {
 
     public List<GameObject> screens = new List<GameObject>();
     Transform mainScreenHolder;
     GameObject currentMainScreen;
     Vector3[] screenPositions = new Vector3[5];
     Quaternion[] screenRotations = new Quaternion[5];
 
     // Use this for initialization
     void Start () {
         currentMainScreen = screens[0];
         mainScreenHolder = currentMainScreen.transform.parent;
         for(int i = 0; i< 5; i++)
         {
             screenRotations[i] = screens[i].transform.localRotation;
             screenPositions[i] = screens[i].transform.localPosition;
         }
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.GetMouseButtonDown(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if(Physics.Raycast(ray,out hit))
             {
                 if(hit.collider.gameObject.tag == "SideScreen")
                 {
                     GameObject screen = hit.collider.transform.GetChild(0).gameObject;
                     SwapScreen(screen, screens.IndexOf(screen));
                 }
             }
         }
     }
 
     void SwapScreen(GameObject sideScreen, int screenIndex)
     {
         RectTransform mainScreenCanvas = currentMainScreen.GetComponent<RectTransform>();
         RectTransform sideScreenCanvas = sideScreen.GetComponent<RectTransform>();
         currentMainScreen.transform.SetParent(sideScreen.transform.parent);
         sideScreen.transform.SetParent(mainScreenHolder);
         currentMainScreen.transform.localPosition = screenPositions[screenIndex];
         currentMainScreen.transform.localRotation = screenRotations[screenIndex];
         //mainScreenCanvas.lossyScale = new Vector3(0.001f,0.001f,0.001f);
         mainScreenCanvas.lossyScale.Set(0.001f,0.001f,0.001f);
         mainScreenCanvas.sizeDelta = new Vector2(950f,950f);
         mainScreenCanvas.lossyScale.Set(0.001f,0.001f,0.001f);
         sideScreen.transform.localPosition = screenPositions[0];
         sideScreen.transform.localRotation = screenRotations[0];
         //sideScreenCanvas.lossyScale = new Vector3(0.001f,0.001f,0.001f);
         sideScreenCanvas.lossyScale.Set(0.001f,0.001f,0.001f);
         sideScreenCanvas.sizeDelta = new Vector2(950f,950f);
         sideScreenCanvas.lossyScale.Set(0.001f,0.001f,0.001f);
         sideScreen.tag = "MainScreen";
         currentMainScreen.tag = "SideScreen";
         currentMainScreen = sideScreen;
     }
 }


Some screenshots:

alt text

alt text

screenie1.png (238.0 kB)
screenie2.png (256.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ProMidgetWrestling · Aug 10, 2015 at 01:45 PM

I found the solution myself to my own problem, since I can't seem to edit the scale of rectTransforms, I instead moved the screens by editing the GameObjects they were children of, and I could easily change their scales with transform.localscale = new Vector3(x,y,z);.

     public List<GameObject> screens = new List<GameObject>();
     GameObject currentMainScreen;
     Vector3 screenPosition;
     Quaternion screenRotation;
 
     // Use this for initialization
     void Start () {
         currentMainScreen = screens[0];
         screenPosition = currentMainScreen.transform.position;
         screenRotation = currentMainScreen.transform.rotation;
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.GetMouseButtonDown(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if(Physics.Raycast(ray,out hit))
             {
                 if(hit.collider.gameObject.tag == "SideScreen")
                 {
                     GameObject screen = hit.collider.gameObject;
                     SwapScreen(screen, screens.IndexOf(screen));
                 }
             }
         }
     }
 
     void SwapScreen(GameObject sideScreen, int screenIndex)
     {
         currentMainScreen.transform.position = sideScreen.transform.position;
         currentMainScreen.transform.rotation = sideScreen.transform.rotation;
         currentMainScreen.transform.localScale = new Vector3(1f,0.8f,0.1f);
         sideScreen.transform.position = screenPosition;
         sideScreen.transform.rotation = screenRotation;
         sideScreen.transform.localScale = new Vector3(2.5f,1.5f,0.1f);
         sideScreen.tag = "MainScreen";
         currentMainScreen.tag = "SideScreen";
         currentMainScreen = sideScreen;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by PremiereBoris · Jul 06, 2017 at 04:44 PM

For the future generations Googling this, you can't set LossyScale, it's read only. Use LocalScale.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Trying to place an UI Canvas Image next to another UI Image 1 Answer

Move a UI element with respect to canvas scaler 0 Answers

Snap UI Text inside canvas 1 Answer

RectTransform rounding? 0 Answers

Moving RectTransform over another RectTransform under GridLayout 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges