GUIUtils

From Unify Community Wiki
Revision as of 16:36, 6 March 2012 by Messu (Talk | contribs)

Jump to: navigation, search

Contents

What is it?

It's a GUI helper written in UnityScript ("UnityJS") for creating rectangles with size and position absolute/relative to any side of the screen.

This plugin was created for Unity 3.4

How to use GUIUtils ?

Mainly you just put a Vector4 in the function doing what you want (which point of the rectangle your working with) Vector4ToRect_XYWH where X = (L)eft/(C)enter/(R)ight et Y = (T)op/(M)iddle/(B)ottom.

The first two coordinates are the position of the point of the rectangle you want to place. If coordinates are positive, the position is calculated from Top/Left side of the screen, else the position is calculated from Bottom/Right side of the screen.

If coordinates/sizes have an absolute value higher than 1, then it is calculated with pixels, else it is calculated in Screen %.

For example, using Vector4ToRect_CM( Vector4( 0.5, -0.3, 1.0, 200 ) ) create a Rectange with its center at 50% of the screen from left side of the screen (horizontally centered), at 30% of the screen from bottom side of the screen, with a width of 100% of the screen and a height of 200 pixels.

Note : I used Vector4 so the variables are grouped together in the Inspector and you dont have to declare 4 different attributes.

Who do I contact for help / questions?

Feel free to email me if you upgrade/clean/adapt/translate this code.

Indiana MAGNIEZ

The code

The plugin script

/*
 
GUIUtils.js
A GUI helper for the Unity Game Engine
 
Created by Indiana MAGNIEZ
 
*/
 
class GUIUtils 
{
	/**
	 * @Name : CreateRectangleFromLimits
	 * @Arguments :
	 * - xMin:float : Left coordinate of the rectangle
	 * - yMin:float : Top coordinate of the rectangle
	 * - xMax:float : Right coordinate of the rectangle
	 * - yMax:float : Bottom coordinate of the rectangle
	 * @Description :
	 * 		This function return a rectangle whose xMin, xMax, yMin and yMax are these given in argument.
	 * @Return : Rect
	 **/
	public static function CreateRectangleFromLimits(xMin:float, xMax:float, yMin:float, yMax:float):Rect {
		var temp:float;
		if ( xMin > xMax ) {
			temp = xMin;
			xMin = xMax;
			xMax = temp;
		}
		if ( yMin > yMax ) {
			temp = yMin;
			yMin = yMax;
			yMax = temp;
		}
		// Rect (left, top, width, height)
		return new Rect(xMin, yMin, xMax - xMin, yMax - yMin);
	}
 
	private static function ConvertCoordInPixels(coord:Vector4):Vector4
	{
		var xPos:float = coord[0];
		var yPos:float = coord[1];
		var xSize:float = coord[2];
		var ySize:float = coord[3];
 
		// On multiplie les valeurs relatives par la taille de l'écran
		if( Mathf.Abs(xPos) <= 1 )
		{
			xPos = xPos * Screen.width;
		}
		if( Mathf.Abs(xSize) <= 1 )
		{
			xSize = Mathf.Abs(xSize) * Screen.width;
		}
		if( Mathf.Abs(yPos) <= 1 )
		{
			yPos = yPos * Screen.height;
		}
		if( Mathf.Abs(ySize) <= 1 )
		{
			ySize = Mathf.Abs(ySize) * Screen.height;
		}
 
		// On applique le positionnement par rapport au côté opposé pour les positions négatives
		if( xPos < 0 )
		{
			xPos = Screen.width + xPos; // La valeur est déjà négative, pas la peine de mettre un signe moins.
		}
		if( yPos < 0 )
		{
			yPos = Screen.height + yPos; // La valeur est déjà négative, pas la peine de mettre un signe moins.
		}
 
		return Vector4(xPos,yPos,xSize,ySize);
	}
 
	// Liste des fonctions de conversions pour GUI dynamique Vector4ToRect_XYWH où X = (L)eft/(C)enter/(R)ight et Y = (T)op/(M)iddle/(B)ottom
 
	/**
	 * Nom : Vector4ToRect_LTHW (LeftTop)
	 * Description :
	 * 		Cette fonction permet de créer un rectangle utilisable en GUI à partir de quatre composantes
	 * @param coord:Vector4
	 * 		Vecteur dont les composantes sont la position par rapport au bord gauche, la position par rapport au bord haut, la largeur et la hauteur du coin haut gauche du rectangle créé
	 * 		Pour les quatre composantes, si la valeur absolue est comprise entre 0 et 1, la position est relative à la taille de l'écran, sinon, elle est en pixels.
	 * 		Pour les deux premières composantes, une valeur négative se basera sur le bord opposé (bas au lieu de haut, droit au lieu de gauche).
	 * @return Rect
	 * 		Le rectangle qui occupe la place correspondant aux quatre composantes.
	 **/
	public static function Vector4ToRect_LTWH(coord:Vector4):Rect
	{
		coord = ConvertCoordInPixels(coord);
 
		var xPos:float = coord[0];
		var yPos:float = coord[1];
		var xSize:float = coord[2];
		var ySize:float = coord[3];
		return Rect(xPos,yPos,xSize,ySize);
	}
 
	/**
	 * Nom : Vector4ToRect_CMHW (CenterMiddle)
	 * Description :
	 * 		Cette fonction permet de créer un rectangle utilisable en GUI à partir de quatre composantes
	 * @param coord:Vector4
	 * 		Vecteur dont les composantes sont la position par rapport au bord gauche, la position par rapport au bord haut, la largeur et la hauteur du centre du rectangle créé
	 * 		Pour les quatre composantes, si la valeur absolue est comprise entre 0 et 1, la position est relative à la taille de l'écran, sinon, elle est en pixels.
	 * 		Pour les deux premières composantes, une valeur négative se basera sur le bord opposé (bas au lieu de haut, droit au lieu de gauche).
	 * @return Rect
	 * 		Le rectangle qui occupe la place correspondant aux quatre composantes.
	 **/
	public static function Vector4ToRect_CMWH(coord:Vector4):Rect
	{
		coord = ConvertCoordInPixels(coord);
 
		var xPos:float = coord[0];
		var yPos:float = coord[1];
		var xSize:float = coord[2];
		var ySize:float = coord[3];
 
		// On place le rectangle dont la position est TOUJOURS celle du coin en haut à gauche correctement par rapport aux valeurs du vecteur
		xPos = xPos - xSize/2;
		yPos = yPos - ySize/2;
		return Rect(xPos,yPos,xSize,ySize);
	}
 
	public static function Vector4ToRect_CTWH(coord:Vector4):Rect
	{
		coord = ConvertCoordInPixels(coord);
		// On place le rectangle dont la position est TOUJOURS celle du coin en haut à gauche correctement par rapport aux valeurs du vecteur
		return Rect(
			coord[0] - coord[2]/2,
			coord[1],
			coord[2],
			coord[3]
			);
	}
 
	public static function Vector4ToRect_RTWH(coord:Vector4):Rect
	{
		coord = ConvertCoordInPixels(coord);
		// On place le rectangle dont la position est TOUJOURS celle du coin en haut à gauche correctement par rapport aux valeurs du vecteur
		return Rect(
			coord[0] - coord[2],
			coord[1],
			coord[2],
			coord[3]
			);
	}
 
	public static function Vector4ToRect_LMWH(coord:Vector4):Rect
	{
		coord = ConvertCoordInPixels(coord);
		// On place le rectangle dont la position est TOUJOURS celle du coin en haut à gauche correctement par rapport aux valeurs du vecteur
		return Rect(
			coord[0],
			coord[1] - coord[3]/2,
			coord[2],
			coord[3]
			);
	}
 
	public static function Vector4ToRect_RMWH(coord:Vector4):Rect
	{
		coord = ConvertCoordInPixels(coord);
		// On place le rectangle dont la position est TOUJOURS celle du coin en haut à gauche correctement par rapport aux valeurs du vecteur
		return Rect(
			coord[0] - coord[2],
			coord[1] - coord[3]/2,
			coord[2],
			coord[3]
			);
	}
 
	public static function Vector4ToRect_LBWH(coord:Vector4):Rect
	{
		coord = ConvertCoordInPixels(coord);
		// On place le rectangle dont la position est TOUJOURS celle du coin en haut à gauche correctement par rapport aux valeurs du vecteur
		return Rect(
			coord[0],
			coord[1] - coord[3],
			coord[2],
			coord[3]
			);
	}
 
	public static function Vector4ToRect_CBWH(coord:Vector4):Rect
	{
		coord = ConvertCoordInPixels(coord);
		// On place le rectangle dont la position est TOUJOURS celle du coin en haut à gauche correctement par rapport aux valeurs du vecteur
		return Rect(
			coord[0] - coord[2]/2,
			coord[1] - coord[3],
			coord[2],
			coord[3]
			);
	}
 
	public static function Vector4ToRect_RBWH(coord:Vector4):Rect
	{
		coord = ConvertCoordInPixels(coord);
		// On place le rectangle dont la position est TOUJOURS celle du coin en haut à gauche correctement par rapport aux valeurs du vecteur
		return Rect(
			coord[0] - coord[2],
			coord[1] - coord[3],
			coord[2],
			coord[3]
			);
	}
}

The testing script

You can use this script adding it on an object in your scene after addition of GUIUtils.js and JSONUtils.js to your project (ideally in Plugins).

This testing script is using one of my others Utils scripts : GUIUtils.

var size:int = 100;
var temoin:boolean = false;
 
function OnGUI()
{
	if( temoin )
	{
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
 
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
 
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
		GUI.Box( Rect(size,size,size,size) , "size" );
	}
	else
	{
		GUI.Box( GUIUtils.Vector4ToRect_LTWH( Vector4( 1.0/6.0 , 1.0/6.0 , size , size ) ), "LT" );
		GUI.Box( GUIUtils.Vector4ToRect_CMWH( Vector4( 1.0/6.0 , 1.0/6.0 , size , size ) ), "CM" );
		GUI.Box( GUIUtils.Vector4ToRect_CTWH( Vector4( 3.0/6.0 , 1.0/6.0 , size , size ) ), "CT" );
		GUI.Box( GUIUtils.Vector4ToRect_CMWH( Vector4( 3.0/6.0 , 1.0/6.0 , size , size ) ), "CM" );
		GUI.Box( GUIUtils.Vector4ToRect_RTWH( Vector4( -1.0/6.0 , 1.0/6.0 , size , size ) ), "RT" );
		GUI.Box( GUIUtils.Vector4ToRect_CMWH( Vector4( -1.0/6.0 , 1.0/6.0 , size , size ) ), "CM" );
 
		GUI.Box( GUIUtils.Vector4ToRect_LMWH( Vector4( 1.0/6.0 , 3.0/6.0 , size , size ) ), "LM" );
		GUI.Box( GUIUtils.Vector4ToRect_CMWH( Vector4( 1.0/6.0 , 3.0/6.0 , size , size ) ), "CM" );
		GUI.Box( GUIUtils.Vector4ToRect_CMWH( Vector4( 3.0/6.0 , 3.0/6.0 , size , size ) ), "CM" );
		GUI.Box( GUIUtils.Vector4ToRect_RMWH( Vector4( 5.0/6.0 , 3.0/6.0 , size , size ) ), "RM" );
		GUI.Box( GUIUtils.Vector4ToRect_CMWH( Vector4( 5.0/6.0 , 3.0/6.0 , size , size ) ), "CM" );
 
		GUI.Box( GUIUtils.Vector4ToRect_LBWH( Vector4( 1.0/6.0 , 5.0/6.0 , size , size ) ), "LB" );
		GUI.Box( GUIUtils.Vector4ToRect_CMWH( Vector4( 1.0/6.0 , 5.0/6.0 , size , size ) ), "CM" );
		GUI.Box( GUIUtils.Vector4ToRect_CBWH( Vector4( 3.0/6.0 , 5.0/6.0 , size , size ) ), "CB" );
		GUI.Box( GUIUtils.Vector4ToRect_CMWH( Vector4( 3.0/6.0 , 5.0/6.0 , size , size ) ), "CM" );
		GUI.Box( GUIUtils.Vector4ToRect_RBWH( Vector4( 5.0/6.0 , 5.0/6.0 , size , size ) ), "RB" );
		GUI.Box( GUIUtils.Vector4ToRect_CMWH( Vector4( 5.0/6.0 , 5.0/6.0 , size , size ) ), "CM" );
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox