- Home /
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:
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;
}
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.
Your answer
Follow this Question
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