Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by darkal · Dec 26, 2014 at 10:57 PM · guipositioncursor

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darkal · Jan 02, 2015 at 02:40 AM 0
Share

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);
 
     }
 }
avatar image darkal darkal · Jan 02, 2015 at 02:16 AM 0
Share

i tried vector2 and vector3 nether helped

avatar image BMayne · Jan 02, 2015 at 03:26 AM 0
Share

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.

avatar image darkal · Jan 02, 2015 at 04:09 AM 0
Share

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);
 
     }
 }
avatar image darkal · Jan 03, 2015 at 08:35 AM 0
Share

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);
 
 
 
     }
 }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Locking cursor without changing position 1 Answer

Setting the mouse position to specific coordinates 5 Answers

GUI always changes Position with different resolutions. 2 Answers

How to set the position of a guitext using transform? 1 Answer

One Last GUI Question 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges