Jump to content

Calling OnGUI() from non-attributed class (no KSPAddon tag)


Recommended Posts

Alright, with KSP 1.1 disabling the RenderingManager access, I'm trying to move my windows to OnGUI and running into an issue.

How do I hook OnGUI from any old class in my project? That means a class that's not tagged with KSPAddon or anything similar.

One thing the Unity documentation did tell me is that OnGUI only works in a class that inherits MonoBehavoir and that is enabled.

So, here's the code giving me issues:

ourWin = new MainGUIWindow(EditorLogic.SortedShipList, winTop, winLeft);
ourWin.enabled = true; //new line added in KSP 1.1

In KSP 1.0.5, the main MainGUIWindow class did not inherit anything and hooked into RenderingManager in it's constructor method, this worked great.

In KSP 1.1, I changed MainGUIWindow to inherit from MonoBehavior, changed my RenderingManager method to OnGUI() and added the second line in the code snippet above to enable the class so the OnGUI() method gets called.

However, the ourWin.enabled line is now throwing a NullRef error and I am now stumped.

So, how do I convert the MainGUIWindow class to a MonoBehavior class so I can have it call OnGUI the same way a class tagged KSPAddOn does?

D

Link to comment
Share on other sites

Tagging a monobehaviour with KSPAddon does nothing else except creating a new GameObject (new GameObject("nice name")) and adding the flagged type to it, using gameObject.AddComponent<myType>(). Creating monobehaviours with the standard constructor doesn't work in Unity. :)
Therefore you can't pass variables to it, like the constructor does it.

In your case, I would add an OnGUI method to your KSPAddon, and call the draw methods you previously registered in RenderingManager from there. That way you don't need to extend from MonoBehavoir and you have minimal code changes, as opposed to MonoBehaviour, where you have to respect the lifecycle (no constructor, everything in Awake() and so on).

 

Link to comment
Share on other sites

That does appear to be what is going on.

I'm making the new instance of the class as per the code in the first post but not attaching it to an object, so Unity is immediately sending it to garbage collection, so the class has already been destroyed by the next line causing the NullRef on the .enabled call I'm trying to make.

Going to switch to the nested OnGUI calls as you suggest, that sounds like it will work.

D.

Link to comment
Share on other sites

MonoBehaviours are not meant to be created using "new" keyword.

They are meant to be instantiated via calling "AddComponent" on a relevant GameObject, this is how Unity works

Link to comment
Share on other sites

  • 3 weeks later...

  

uff, if i understand needs right, here are some code example. i used this when i moved   from 4.2.6 to 5.2.4. just a note.

 

    public class ClassName
    {
        protected OnGUIObject onGUIObject;

        public NiceName(blabla)
        {
             *****
             GameObject onGUIHolder = new GameObject();
             onGUIObject = onGUIHolder.AddComponent<OnGUIObject>(); 
             onGUIHolder.transform.parent = part.transform;
        }
               
        public virtual void Activate()
        {
                onGUIObject.updateGUIFunction += OnGUI;
        }

        public virtual void Deactivate()
        {
            onGUIObject.updateGUIFunction -= OnGUI;
        }
        
        void OnGUI()
        {
            *****
        }       
}
    public delegate void UpdateGUIFunction();

    public class OnGUIObject : MonoBehaviour
    {
        public UpdateGUIFunction updateGUIFunction;

        void OnGUI()
        {
            if (updateGUIFunction != null)
            {
                updateGUIFunction();
            }
        }
    }

 

Edited by DennyTX
Link to comment
Share on other sites

@DennyTX That is what I ended up doing.

The answer to my question in the first post is that Unity only calls OnGUI() in classes that inherit from monobehavior, so you either have to tag your class as a monobehavior (which I can't in this specific case), or call your own GUI method via a call in a OnGUI of a monobehavior class such as the KSPAddon.Flight class for your mod as per your code snippet.

D.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...