"Unity3d" implements background processing in editor mode with inheriting Editorupdater class

Source: Internet
Author: User

The Ongui function of the Editorwindow class will only run when the window focus is on the editor window. If you want the focus not to be on the editor window, it can be updated in real time, and you can do the following:

OnDestroy OnDestroy is called when the Editorwindow is closed.
onfocus Called when the window gets keyboard focus.
Ongui Implement your own editor GUI here.
Onhierarchychange Called whenever the scene hierarchy has changed.
Oninspectorupdate Oninspectorupdate is called at Ten frames per second to give the inspector a chance to update.
OnLostFocus Called when the window loses keyboard focus.
Onprojectchange Called whenever the project has changed.
Onselectionchange Called whenever the selection has changed.
Update Called multiple times per second on all visible windows.

However, if the editor window is pasted onto a large window, it selects the window with its peers, thus hiding the editor window so that the Ongui function cannot be called. Therefore, in order to achieve more efficient background processing, we can inherit a editorupdate class that we write.

usingSystem;usingSystem.Collections;usingSystem.Reflection;usingUnityeditor;usingUnityengine; [Initializeonload] Public classEditormonobehaviour {StaticEditormonobehaviour () {varType = Types.gettype ("unityeditor.editorassemblies","UnityEditor.dll"); varmethod = Type. GetMethod ("subclassesof", bindingflags.static| bindingflags.nonpublic| BindingFlags.Instance,NULL,Newtype[]{typeof(Type)},NULL); varE = method. Invoke (NULL,New Object[] {typeof(Editormonobehaviour)}) asIEnumerable; foreach(Type Editormonobehaviourclassinche) {method= EditorMonoBehaviourClass.BaseType.GetMethod ("Oneditormonobehaviour", BindingFlags.NonPublic |bindingflags.instance); if(Method! =NULL) {method. Invoke (System.Activator.CreateInstance (editormonobehaviourclass),New Object[0]); }        }    }    Private voidOneditormonobehaviour () {editorapplication.update+=Update; Editorapplication.hierarchywindowchanged+=onhierarchywindowchanged; Editorapplication.hierarchywindowitemongui+=Hierarchywindowitemongui; Editorapplication.projectwindowchanged+=onprojectwindowchanged; Editorapplication.projectwindowitemongui+=Projectwindowitemongui; Editorapplication.modifierkeyschanged+=onmodifierkeyschanged; editorapplication.callbackfunction function= () + =Onglobaleventhandler (event.current); FieldInfo Info=typeof(editorapplication). GetField ("Globaleventhandler", BindingFlags.Static | BindingFlags.Instance |bindingflags.nonpublic); Editorapplication.callbackfunction functions= (editorapplication.callbackfunction) info. GetValue (NULL); function+=function; Info. SetValue (NULL, (Object) functions); Editorapplication.searchchanged+=onsearchchanged; Editorapplication.playmodestatechanged+ = () = {            if(editorapplication.ispaused) {onplaymodestatechanged (playmodestate.paused); }                if(editorapplication.isplaying) {onplaymodestatechanged (playmodestate.playing); }            if(Editorapplication.isplayingorwillchangeplaymode) {onplaymodestatechanged (Playmodestate.pla            Yingorwillchangeplaymode);        }        };    Start (); }     Public Virtual voidStart () {} Public Virtual voidUpdate () {} Public Virtual voidonhierarchywindowchanged () {} Public Virtual voidHierarchywindowitemongui (intinstanceid,rect Selectionrect) {}     Public Virtual voidonprojectwindowchanged () {} Public Virtual voidProjectwindowitemongui (stringguid,rect Selectionrect) {}     Public Virtual voidonmodifierkeyschanged () {} Public Virtual voidOnglobaleventhandler (Event e) {} Public Virtual voidonsearchchanged () {} Public Virtual voidonplaymodestatechanged (Playmodestate playmodestate) {} Public enumplaymodestate {Playing, Paused, Stop, Playingorwillchangeplaymode}}

This class is then inherited in another script, and methods such as start and update are overloaded, and the background logic in these methods can be updated in the background.

using Unityengine; using System.Collections;  Public class updatertest:editormonobehaviour{    publicoverridevoid  Update ()    {            }}

"Unity3d" implements background processing in editor mode with inheriting Editorupdater class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.