Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Canvas Scaler settings 0 Answers

Scaling Canvas (or panel) and then trying to move a gameobject (child) attached to it 1 Answer

Ui elements not scaling correctly 0 Answers

Scaling RectTransform to fit screen size in code 1 Answer

Good way to scale 3D GameObjects in a Canvas (Screen Space - Camera)? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges