Position To Rect

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
m (Text replace - "</javascript>" to "</syntaxhighlight>")
 
Line 15: Line 15:
 
// drawn as if by a camera with a pixelRect equivalent to (10,10,200,200)
 
// drawn as if by a camera with a pixelRect equivalent to (10,10,200,200)
 
GUI.DrawTexture(rect,texture); // draw the texture at the new rect
 
GUI.DrawTexture(rect,texture); // draw the texture at the new rect
}</javascript>
+
}</syntaxhighlight>
 
<syntaxhighlight lang="javascript">function PosToRect (pos : Vector2, bounds : Rect, inset : Rect) {
 
<syntaxhighlight lang="javascript">function PosToRect (pos : Vector2, bounds : Rect, inset : Rect) {
 
i = inset;
 
i = inset;
Line 24: Line 24:
 
rect.height = i.height;
 
rect.height = i.height;
 
return rect;
 
return rect;
};</javascript>
+
};</syntaxhighlight>
  
 
--[[User:DannyL|DannyL]] 09:07, 22 September 2008 (PDT)
 
--[[User:DannyL|DannyL]] 09:07, 22 September 2008 (PDT)

Latest revision as of 20:52, 10 January 2012

[edit] Description

A helper function to convert a GUITexture position to the equivalent Rect.

[edit] Usage

PosToRect (position : Vector2, bounds : Rect, pixelInset : Rect) : Rect;

position - the guiTexture's transform.position x and y components as a Vector2.

bounds - the equivalent of a custom pixelRect for rendering the guiTexture into. Uses GUI coordinates.

pixelInset - the pixelInset (Rect) of the guiTexture.

function OnGUI () {
	var rect = PosToRect(Vector2(pos.x,pos.y),Rect(10,10,200,200),Rect(-10,-10,20,20));
	// set rect to the GUI Rect of a guiTexture at position pos with a pixelInset of (-10,-10,20,20)
	// drawn as if by a camera with a pixelRect equivalent to (10,10,200,200)
	GUI.DrawTexture(rect,texture); // draw the texture at the new rect
}
function PosToRect (pos : Vector2, bounds : Rect, inset : Rect) {
	i = inset;
	rect = new Rect(0,0,0,0);
	rect.x = bounds.x + (bounds.width * pos.x) + i.x;
	rect.y = bounds.y + (bounds.height * pos.y) + i.y;
	rect.width = i.width;
	rect.height = i.height;
	return rect;
};

--DannyL 09:07, 22 September 2008 (PDT)

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox