- Home /
DragWindowPosition Doesn't Work
My DragWindowPosition doesn't work. I was watching a tutorial: https://www.youtube.com/watch?v=W68rCj6baRk and it worked up until when I added the DragWindowPosition. When I played my game, the window was draggable but my Inventory buttons and Close button disappeared. It think this is a problem with the position or size of my GUI but I don't know how to fix it. My script:
START:
#pragma strict
//Private Variables
private var InventoryOn = false;
private var scrollBarChopGrid : Vector2 = Vector2.zero;
private var GridValue : float = -1;
//GUI Pos/Size
var NamePosition : Vector2 = new Vector2(30,30);
var NameSize : Vector2 = new Vector2(250,250);
var GridPosition : Vector2 = new Vector2(10,5);
var GridSize : Vector2 = new Vector2(323,410);
var ClosePosition : Vector2 = new Vector2(312,5);
var CloseSize : Vector2 = new Vector2(35,35);
var ScrollPosition : Vector2 = new Vector2(0,95);
var ScrollSize : Vector2 = new Vector2(355,500);
var WindowPosition : Vector2 = new Vector2(0,0);
var WindowSize : Vector2 = new Vector2(360,360);
var DragWindowPosition : Rect = Rect(0,0,WindowSize.x,WindowSize.y);
//Textures
var InventoryWindow : Texture;
var CloseIcon : Texture;
var Grids : Texture [];
function Update ()
{
//On or Off
if (Input.GetKeyUp ("i"))
{
if (InventoryOn == false)
{
InventoryOn = true;
}
else if (InventoryOn == true)
{
InventoryOn = false;
scrollBarChopGrid.y = 0;
}
}
//Window Drag Fix
if (Input.GetKeyDown("left shift"))
{
if (Input.GetKeyDown("i"))
{
DragWindowPosition.x = 10;
DragWindowPosition.y = 10;
}
}
}
function OnGUI()
{
if (InventoryOn ==true)
{
DragWindowPosition = GUI.Window(0, DragWindowPosition, DoMyWindow, "");
}
}
function DoMyWindow(WindowID : int)
{
if (InventoryOn == true)
{
GUI.BeginGroup(new Rect(WindowPosition.x, WindowPosition.y, WindowSize.x, WindowSize.y), InventoryWindow);
//Name
GUI.Label(Rect(NamePosition.x, NamePosition.y, NameSize.x, NameSize.y), "Your Inventory");
//Close Button
if (GUI.Button(Rect(ClosePosition.x, ClosePosition.y, CloseSize .x, CloseSize .y), CloseIcon))
{
InventoryOn = false;
}
//Scroll Bar
scrollBarChopGrid = GUI.BeginScrollView(Rect (ScrollPosition.x, ScrollPosition.y, ScrollSize.x, ScrollSize.y), scrollBarChopGrid, Rect(0,0,0,420));
//Grid
GridValue = GUI.SelectionGrid(Rect(GridPosition.x, GridPosition.y, GridSize.x, GridSize.y), GridValue, Grids, 5);
GUI.EndScrollView();
//Dragable Window
GUI.DragWindow (Rect (WindowPosition.x, WindowPosition.y, 10000, 40));
GUI.EndGroup();
}
}
END
Another thing wrong with the Inventory is that my Inventory Buttons go underneath the texture of the Inventory halfway through. This problem is only fixable, though, if I fix the first problem with the DragWindowPosition. I tried to explain this the best way I could but if you have any questions then please ask.
Please reformat your code by selecting all your code then press the code button,
I'll try resizing the DragWindowPosition in the script and see if it works
Answer by slayer29179 · May 26, 2013 at 03:28 PM
I just tried your code and it worked fine, straight away.
Take the script off your object.
Re-assign it.
Make sure you have assigned the Grids images, background image and close button image. :) hope this helps!
$$anonymous$$y thing is pretty fine now-it looks correct...but not the scroll bar. The scroll bar is not there? I've tried to resize it, position it but it just doesn't show up. Any ideas?
Errr...I dunno. What's a group lol-I'm just very new to scripting. Whenever I reposition the "ScrollPosition" variable it repositions all of the grids ins$$anonymous$$d. I wondered if the script had errors where I put ins$$anonymous$$d "GridPosition" ins$$anonymous$$d of "ScrollPosition" but I didn't have any.
The group is the BeginGroup line which makes a big group around everything you put in between that and GUI.EndGroup (kinda like a balloon with water inside)
The reason you put your grids inside of your scroll bar is to allow it to move with the scroll bar. When you move your grids position you are moving them inside the scroll bar. When you move the scroll bar you are moving both the grids and the scroll bar :)