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
3
Question by Dayman · May 08, 2013 at 11:33 PM · editoronguicustom inspectorexecuteineditmode

OnGUI() never being called in ExecuteInEditMode script

I'm trying to do something that I thought would be pretty simple, but is turning out to be very frustrating. I have a system of pathfinding waypoints, and I would like to make it easier to make connections between the nodes with a simple point and click interface. As in, you click the first node, click a button that says "add connection," then click the node you want to connect to, all in the editor.

First I tried to do this with a custom inspector for the waypoint class. This seemed to be working, except that it could never handle the part where you click on a second waypoint, because as soon as you click in the scene the object becomes deselected and the inspector script stops running.

To get around that issue, I made it so the custom editor just has a button which switches the Waypoint instance into "making a new connection" mode. So I had to make the Waypoint an ExecuteInEditMode script, and in OnGUI it looks for mouseclicks using Event.current.isMouse and Event.current.button then casts a from camera.current to the mouse.

However, a new problem has come up that I just can't figure out at all--the OnGUI() method is NEVER being called while in edit mode. It gets called in play mode, and other functions (like Update()) get called in edit mode, but never OnGUI(), under any circumstance. I understand that OnGUI() only fires when it receives an event in edit mode, but I can't figure out any way to get it to fire even once, no matter what I do.

Here's the code for the class, but most of it is kind of irrelevant because the problem is that the very first Debug.Log("OnGUI Firing") in the OnGUI method is never happening, much less any of the rest of the method. What am I doing wrong here?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [ExecuteInEditMode]
 public class AIWaypoint : MonoBehaviour {
 
     public List<AIWaypoint> edges;
 
     public bool addEdgeMode = false;
 
 
     void OnEnable () {
         if (edges == null)
         {
             edges = new List<AIWaypoint>();
         }
         
     }
 
     void OnGUI()
     {
         Debug.Log("OnGUI firing");
         if (addEdgeMode)
         {
             if (Event.current.isMouse && Event.current.button == 0)
             {
                 Ray clickRay = Camera.current.ScreenPointToRay(Event.current.mousePosition);
                 RaycastHit hit;
                 if (Physics.Raycast(clickRay, out hit, 100, (1 << 31)))
                 {
                     AIWaypoint hitWP = hit.collider.gameObject.GetComponent(typeof(AIWaypoint)) as AIWaypoint;
                     if (hitWP != null)
                     {
                         AddEdge(hitWP);
                         addEdgeMode = false;
                     }
                 }
             }
         }
     }
 }
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
Best Answer

Answer by Bunny83 · May 08, 2013 at 11:43 PM

Are you sure that your GameView is visible in the editor and you actually focused it? ExecuteInEditMode just pretends to be in playmode. There are no constant update intervals, only when something changed. OnGUI is an event processing function (even at runtime). Make sure your GameView is visible. Of course you won't get any events in OnGUI when you're in the SceneView since that doesn't belong to the runtime at all.

You really should go the custom inspector way ;) Here's my GUI crash course. The important part is the section about the Event class and the Use function. Also see OnSceneGUI which mentions the Use function as well.

Also you should set the Tools.viewTool to ViewTool.None so it doesn't interfer with the built-in tools.

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 Dayman · May 09, 2013 at 12:39 AM 0
Share

Perfect, I was doing a couple of things wrong, and your answer helped me figure out what they were. I switched over to doing it entirely in a custom inspector (way better!), using the OnSceneGUI method, rather than OnGUI.

I tried adding Tool.viewTool = ViewTool.None to my method, but it doesn't seem to actually change the tool in use in the editor. All the same, manually changing to the pan tool fixes my focus-losing problem, so I'm good there, too.

Thanks again for the help.

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using ExecuteInEditMode to create platforms in the editor 1 Answer

Unity Editor, detect Save or Rebuild Event 1 Answer

OnGUI elements in editor scene view 1 Answer

Mixing custom editors with custom property drawers, possible ? 1 Answer

Problem with ExecuteInEditMode 0 Answers


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