-AUTEUR : artemisart
-DESCRIPTION : Cette fois si un script tout simple, mais tellement utile si on se sert de OnGUI ! C'est juste un Rect, avec en plus une position relative à l'écran et un point de pivot.
-UTILISATION : Ben ça s'utilise comme un Rect normal (le cast est implicite) mais faut de préférence déclarer la variable au début du script (en membre de la classe et pas new Rect(...) dans une fonction GUI) pour la modifier depuis l'inspector.
Un exemple concret, pour voir un peu à quoi ça sert :
On veut afficher une box de 100*20, 10 pixels au dessus du bas de l'écran et centrée en x :
En GUI classique ça donnerais un truc du genre... GUI.Box (new Rect (Screen.width / 2 - 100 / 2, Screen.height - 10 - 20, 100, 20), "truc"); (-> on peut légèrement simplifier, mais c'est encore moins compréhensible ^^).
Bon bref, pas très digeste, et encore là c'est pas compliqué.
Avec ScreenRect :
Au début du script : public ScreenRect rect;
En OnGUI : GUI.Box (rect, "truc");
Dans l'inspector :
- position : Bottom (c'est la position du pivot par rapport à l'écran)
- pivot : Bottom (c'est la position du pivot par rapport au Rect, là on le mets en bas pour facilement rajouter les 10 pixels d'offset)
- x : 0 (pas de décalage en x)
- y : -10 (décalage vertical vers le bas, en négatif vers le haut).
- width : 100
- height : 20
-SCRIPT :
Code : Tout sélectionner
using UnityEngine;
[System.Serializable]
public class ScreenRect
{
public enum Position
{
TopLeft,
Top,
TopRight,
Left,
Center,
Right,
BottomLeft,
Bottom,
BottomRight
}
public Position position;
public Position pivot;
public int x;
public int y;
public int width;
public int height;
public ScreenRect (int x, int y, int width, int height)
{
position = Position.TopLeft;
pivot = Position.TopLeft;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public static implicit operator Rect (ScreenRect sr)
{
Vector2 pos = sr.CalcPosition ();
Vector2 piv = sr.CalcPivot ();
return new Rect (pos.x + piv.x + sr.x, pos.y + piv.y + sr.y, sr.width, sr.height);
}
private Vector2 CalcPosition ()
{
Vector2 pos = Vector2.zero;
switch (position) // set x
{
case Position.Top:
case Position.Center:
case Position.Bottom:
pos.x = Screen.width / 2;
break;
case Position.TopRight:
case Position.Right:
case Position.BottomRight:
pos.x = Screen.width;
break;
}
switch (position) // set y
{
case Position.Left:
case Position.Center:
case Position.Right:
pos.y = Screen.height / 2;
break;
case Position.BottomLeft:
case Position.Bottom:
case Position.BottomRight:
pos.y = Screen.height;
break;
}
return pos;
}
private Vector2 CalcPivot ()
{
Vector2 piv = Vector2.zero;
switch (pivot) // set x
{
case Position.Top:
case Position.Center:
case Position.Bottom:
piv.x = -width / 2;
break;
case Position.TopRight:
case Position.Right:
case Position.BottomRight:
piv.x = -width;
break;
}
switch (pivot) // set y
{
case Position.Left:
case Position.Center:
case Position.Right:
piv.y = -height / 2;
break;
case Position.BottomLeft:
case Position.Bottom:
case Position.BottomRight:
piv.y = -height;
break;
}
return piv;
}
}