- Home /
get the mouse cursor position x and y and make gui follow on trigger
basically i want to make it so if i click GUI.Button it trigger it on and the GUI.Button x and y will follow cursor on the screen tell i click GUI.Button again triggering it off i know how make the trigger and set the x and y but i don't know how to get the cursor position on the screen so it change x and y of GUI.Button to the cursor x and y coordinates on the screen
i need the code in C# other wise wont work with my other codes please help
Answer by gameplay4all · Dec 26, 2014 at 11:48 PM
Warning I don't use c# this is JavaScript translated to C# by a C#-noob ;)
You should make some variables that would regulate wheter or not the button is following, and the x,y position of the button:
public bool buttonFollow;
public Vector2 buttonPos;
And then make the button toggle the buttonFollow
variable:
if( GUI.Button(Rect(buttonPos.x, buttonPos.y, 100, 100), "Your Button Text")){
buttonFollow = !buttonFollow;
}
And then to make the button actually change its position:
if( buttonFollow){
buttonPos = Event.current.mousePosition;
}
Please try to understand what these lines of code do and mean :) Don't just copy-paste them and accept that they work, you won't learn anything that way ;)
But you must of course do as you like.
Good luck!
NullReferenceException: Object reference not set to an instance of an object dragdrop.Update () (at Assets/network/codes/dragdrop.cs:21)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class dragdrop : $$anonymous$$onoBehaviour
{
//public int x = 0;
public Texture icon;
public string text = "";
//public int y = 0;
public bool m1 = false;
public bool m2 = false;
//public GameObject $$anonymous$$ousePosition;
public float w = 0f;
public Vector3 buttonPos;
void Update()
{
buttonPos = Event.current.mousePosition;
if (m1)
{
}
}
void OnGUI()
{
if(GUI.Button (new Rect (buttonPos.x, buttonPos.y, 50, 50), ""))
{
m1 = !m1;
}
//GUI.DrawTexture (new Rect (x - 15, y - 15, 75, 75), icon);
}
}
Hey there,
Events are a part of the immediate mode GUI. They do not exist outside of any function call OnGUI().
To put it simple.
Input static calss is used in any function except OnGUI
Events static class is used in only OnGUI functions.
thankyou it works now
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class dragdrop : $$anonymous$$onoBehaviour
{
//public int x = 0;
public Texture icon;
public string text = "";
//public int y = 0;
public bool m1 = false;
public bool m2 = false;
//public GameObject $$anonymous$$ousePosition;
public float w = 0f;
public Vector2 buttonPos;
void Update()
{
}
void OnGUI()
{
if (m1)
{
buttonPos = Event.current.mousePosition;
}
if(GUI.Button (new Rect (buttonPos.x - 25, buttonPos.y - 25, 50, 50), ""))
{
m1 = !m1;
}
GUI.DrawTexture (new Rect (buttonPos.x - 35, buttonPos.y - 35, 75, 75), icon);
}
}
to ever1 i updated my code to drop into another gui slot if its in the slot vector area x and y heres updated code hope it helps you all
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class dragdrop : $$anonymous$$onoBehaviour
{
public float basesize = 50f;
//public int x = 0;
public Texture icon;
public string text = "";
//public int y = 0;
public bool m1 = false;
public bool m2 = false;
//public GameObject $$anonymous$$ousePosition;
public float w = 0f;
public Vector2 buttonPos;
public Vector2 s1;
void Update()
{
if (buttonPos.x >= s1.x && buttonPos.x <= s1.x + basesize && buttonPos.y >= s1.y && buttonPos.y <= s1.y + basesize)
{
m2 = true;
}
else
{
m2 = false;
}
if (m2)
{
buttonPos.x = s1.x + (basesize / 2);
buttonPos.y = s1.y + (basesize / 2);
}
}
void OnGUI()
{
GUI.Box (new Rect (s1.x, s1.y, basesize, basesize), "");
if (m1)
{
buttonPos = Event.current.mousePosition;
}
if(GUI.Button (new Rect (buttonPos.x - (basesize / 2), buttonPos.y - (basesize / 2), basesize, basesize), ""))
{
m1 = !m1;
}
GUI.DrawTexture (new Rect (buttonPos.x - (basesize / 2), buttonPos.y - (basesize / 2), basesize, basesize), icon);
}
}