Position To Rect

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
Line 10: Line 10:
 
'''pixelInset''' - the pixelInset (Rect) of the guiTexture.
 
'''pixelInset''' - the pixelInset (Rect) of the guiTexture.
  
<javascript>function OnGUI () {
+
<syntaxhighlight lang="javascript">function OnGUI () {
 
var rect = PosToRect(Vector2(pos.x,pos.y),Rect(10,10,200,200),Rect(-10,-10,20,20));
 
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)
 
// set rect to the GUI Rect of a guiTexture at position pos with a pixelInset of (-10,-10,20,20)
Line 16: Line 16:
 
GUI.DrawTexture(rect,texture); // draw the texture at the new rect
 
GUI.DrawTexture(rect,texture); // draw the texture at the new rect
 
}</javascript>
 
}</javascript>
<javascript>function PosToRect (pos : Vector2, bounds : Rect, inset : Rect) {
+
<syntaxhighlight lang="javascript">function PosToRect (pos : Vector2, bounds : Rect, inset : Rect) {
 
i = inset;
 
i = inset;
 
rect = new Rect(0,0,0,0);
 
rect = new Rect(0,0,0,0);

Revision as of 20:48, 10 January 2012

Description

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

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
}</javascript>
<syntaxhighlight lang="javascript">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;
};</javascript>
 
--[[User:DannyL|DannyL]] 09:07, 22 September 2008 (PDT)
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox