Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Cambesa · Jul 12, 2015 at 09:35 PM · guiclass objectc#-and-js

How to use the Object class to pass a Vector2?

Hi all,

I'm working on a context menu to use within the Unity Editor(in a seperate window). I want to create a new window by pressing the Action button from the context menu and the window has to appear at the mouse position.

The problem is that I can't seem to pass the mouse position to the function which places the window. Here's part of the code I use:

 menu.AddItem(new GUIContent("Action") , false, CreateActionBox, mousePos);

And this is the function(Which is passed in the 3rd argument in the menu.AddItem function) it executes when clicking the Action button from the context menu.

 private void CreateActionBox(Object obj)
     {
         FieldWindow wind=new FieldWindow(1, new Rect(obj.x,obj.y,10f,10f));
         FieldWindows.Add(wind);
         Debug.Log("Window created");
     }

The mousePos variable is a Vector2 but the only type to pass in the fourth argument in the menu.AddItem function is Object. I can't find out how to make unity treat the Object type as a Vector2.

The unity documentation describes a JS way to pass a String to the function but I can't translate it to a right C# way, there is no C# example in the documentation. Here's the similar code from the documentation in JS:

 menu.AddItem (new GUIContent ("MenuItem1"), false, Callback, "item 1");

And:

 function Callback (obj:Object) {
         Debug.Log ("Selected: " + obj);
     }

I have also tried different approaches such as:

 private void CreateActionBox(Vector2 pos)

And casting the mouse position as Object but I didn't find a way that works.

I hope you can help me!

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 jctz · Jul 16, 2015 at 05:55 AM

did you try this?

 private void CreateActionBox(Object obj)
         {
             Vector2 pos = obj as Vector2;
             FieldWindow wind=new FieldWindow(1, new Rect(pos.x,pos.y,10f,10f));
             FieldWindows.Add(wind);
             Debug.Log("Window created");
         }

Comment
Add comment · Show 1 · 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 Cambesa · Jul 19, 2015 at 10:00 PM 0
Share

Hey, thanks for your reaction.

I just tried:

 private void CreateDialogueBox(Object obj)
     {
         Vector2 pos=obj as Vector2;
         FieldWindow wind=new FieldWindow(1,new Rect(pos.x,pos.y,10f,10f));
         FieldWindows.Add(wind);
         Debug.Log("Window created");
     }

But it results in the error:

  error CS0077: The `as' operator cannot be used with a non-nullable value type `UnityEngine.Vector2'

Trying similar aproaches such as:

 menu.AddItem(new GUIContent("Dialogue"),false,CreateDialogueBox,mousePos as Object);

Results in the error:

 error CS0039: Cannot convert type `UnityEngine.Vector2' to `UnityEngine.Object' via a built-in conversion

Replacing it with casts also results in errors stating that it can not convert Vector2 to Object and neither the other way around.

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

Enemy Health Bar problem Unity 2D. 1 Answer

[Solved] Built in GUI continuously Loads through different Options. 1 Answer

c# - void method from external class. 2 Answers

drawing a box with GUI 3 Answers

Why is my code duplicating the name of a button? styling gui 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