0

i'm trying to scale a GUI.TextArea.

the code block works in OnGui function. But when i change screen size , the textArea won't be on correct position and size.

first screen size

second screen size

is there any solution like Canvas.ScaleMode.ScaleToFit or should i recreate the GUI.TextArea?

Edit: I use button,some image and text in canvas and easily scale them with canvasScaler script. But the text object doesn't contain multiple line text so i create textarea with gui for display multiple line text.

Fattie
  • 27,874
  • 70
  • 431
  • 719
yatagac
  • 19
  • 1
  • 8

2 Answers2

4

The UI system in unity is very easy to use...

1) click add canvas .. select "scale with screen size"

Important!!!!!!!!!!!!!

It is critical to select "scale with screen size".

(Note that this is in the "Canvas Scaler" section - it is NOT in the "Canvas" section.)

enter image description here

(2017 - note they recently changed the wording from "space" to "size"...)

Unity accidentally set the WRONG DEFAULT OPTION on that menu.

Until they fix the problem, it is essential to remember to select "scale with screen size".

The other options are irrelevant and almost never used.

2) click "Add Button", or add any element you want: panel. scroll view, slider, etc.

You're completely done.

It's that easy.

For a given item: to achieve what you say about POSITION, just do this ...

enter image description here

select the option relevant to you, probably middle one.

In your script have

  public Text hello

That will create a slot (labelled "hello") in your Inspector panel for that script.

be sure to drag the physical .Text unit (ie, underneath "Canvas") to that slot in the Inspector. To set the text, in code, is trivial:

    hello.text = "some text here"; 

it's that simple. You can set the size, shape, position, etc of the type in the editor, what you see is what you get.

Be sure to set the following items ...

enter image description here

... as you need them. Cheers.

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • yes , i'm using canvas for scaling button and some image and text. but text.text contains a few characters. how can display long text like in picture? – yatagac Mar 28 '16 at 18:12
  • how can i use text area with canvas? if i add text in canvas , i cannot display my all text – yatagac Mar 28 '16 at 19:31
  • extremely simple! in script have `public Text hello` . be sure to drag to it your Text item. when you want to set the text, it is so easy .......... `hello.text = "some text here";` – Fattie Mar 28 '16 at 19:41
  • yes , i know this but hello.text doesn't contain multiple lines. my message is message="Dear Sir , i need "+missionAmount+" kg grain. If you accept, i will pay " +mission1Reward+ " florins. But i must receive supply in "+missionDuration+" months otherwise you will owe me "+penaltyMoney+" florins!"; so i need textarea. – yatagac Mar 28 '16 at 19:43
  • ***YES, IT DOES CONTAIN MULTIPLE LINES. IT DOES EVERYTHING FOR YOU. SIMPLY TRY IT. JUST TRY IT IN THE EDITOR. SIMPLY SET THE WIDTH OF THE TEXT ARE. SET IT TO "WRAP"*** – Fattie Mar 28 '16 at 19:45
-1

You should try to use new GUI / Canvases, but if you're determined to use the older immediate-mode GUI for some reason, you can do this:

public static Vector3 GUIScale{
    get{
        float normalWidth = 1024; //Whatever design resolution you want
        float normalHeight = 768;
        return new Vector3(Screen.width / normalWidth, Screen.height / normalHeight, 1);
    }
}


public static Matrix4x4 AdjustedMatrix{
    get {
        return Matrix4x4.TRS(Vector3.zero, Quaternion.identity, GUIScale);
    }
}

void OnGUI(){
    GUI.matrix = AdjustedMatrix;
    (... GUI drawing code ...)
}

Which will make the GUI always take up the same amount of the screen. It will work, but it might look pretty bad at higher resolutions and different screen aspect ratios than the "normal" one you design for. You'll also have to use the "normal" resolution numbers instead of Screen.width and Screen.height when drawing things.

Gabriel Sibley
  • 368
  • 2
  • 6