void CheckComponents()
        {
            if (StateWindowDisplay == null)
            {
                StateWindowDisplay = new vStateWindowDisplay();
            }

            if (!fsmSkin)
            {
                fsmSkin = (GUISkin)Resources.Load("GUISkins/EditorSkins/NodeEditorSkin");
            }
            try
            {
                var selectedObject = lastValidSelection ? lastValidSelection : Selection.activeGameObject;
                if (Selection.activeGameObject != null && selectedObject && selectedObject != Selection.activeGameObject)
                {
                    selectedObject = Selection.activeGameObject;
                }
                if (selectedObject)
                {
                    if ((lastValidSelection == null || lastValidSelection != selectedObject || target == null) && selectedObject.GetComponent <vIFSMBehaviourController>() != null)
                    {
                        target             = selectedObject.GetComponent <vIFSMBehaviourController>() as MonoBehaviour;
                        lastValidSelection = selectedObject;
                    }
                }
            }
            catch { }
        }
        private void OnGUI()
        {
            CheckComponents();

            viewRect               = new Rect(0, 0, position.width, position.height);
            debugLineStyle         = new GUIStyle(fsmSkin.box);
            this.minSize           = new Vector2(200, 100);
            debugWindowRect.y      = viewRect.y + 20;
            debugWindowRect.x      = position.width - Mathf.Clamp((500 - fsmViewOffset), 200f, position.width);
            debugWindowRect.width  = Mathf.Clamp((500 - fsmViewOffset), 200f, position.width);
            debugWindowRect.height = viewRect.height - 20;

            nodeDebugRect.y      = viewRect.y + 20;
            nodeDebugRect.height = viewRect.height - 20;
            nodeDebugRect.width  = (target != null && target is vIFSMBehaviourController && (target as vIFSMBehaviourController).debugMode) ? position.width - debugWindowRect.width : position.width;

            dragFSMSize.x      = debugWindowRect.x - 2.5f;
            dragFSMSize.y      = viewRect.y + 20;
            dragFSMSize.width  = 5;
            dragFSMSize.height = viewRect.height;

            DrawBackground(viewRect);
            ///State debug window
            GUILayout.BeginArea(nodeDebugRect);
            {
                nodeDebugScroll = GUILayout.BeginScrollView(nodeDebugScroll);
                {
                    if (StateWindowDisplay != null)
                    {
                        if (target != null && target is vIFSMBehaviourController)
                        {
                            StateWindowDisplay.Draw(target as vIFSMBehaviourController, fsmSkin);
                        }
                    }
                }
                GUILayout.EndScrollView();
            }
            GUILayout.EndArea();
            ///Debug message window
            if ((target != null && target is vIFSMBehaviourController && (target as vIFSMBehaviourController).debugMode))
            {
                GUILayout.BeginArea(debugWindowRect, fsmSkin.GetStyle("ToolBar"));
                {
                    try
                    {
                        if ((target as vIFSMBehaviourController).debugList != null)
                        {
                            debugWindowScroll = EditorGUILayout.BeginScrollView(debugWindowScroll, false, false, GUIStyle.none, GUI.skin.verticalScrollbar, GUIStyle.none);
                            {
                                GUILayout.BeginVertical(GUILayout.ExpandHeight(true));
                                debugLineStyle.richText  = true; debugLineStyle.alignment = TextAnchor.MiddleLeft; debugLineStyle.fontSize = 10;
                                debugLineStyle.fontStyle = FontStyle.Normal;
                                for (int i = 0; i < (target as vIFSMBehaviourController).debugList.Count; i++)
                                {
                                    try
                                    {
                                        var height = debugLineStyle.CalcHeight(new GUIContent((target as vIFSMBehaviourController).debugList[i].message), debugWindowRect.width);

                                        if (GUILayout.Button(" " + (target as vIFSMBehaviourController).debugList[i].message, debugLineStyle, GUILayout.Height(height + 10), GUILayout.Width(debugWindowRect.width)))
                                        {
                                            if ((target as vIFSMBehaviourController).debugList[i].sender != null)
                                            {
                                                try
                                                {
                                                    Selection.activeObject = (target as vIFSMBehaviourController).debugList[i].sender;
                                                }
                                                catch { }
                                            }
                                        }
                                    }
                                    catch { }
                                }
                                GUILayout.EndVertical();
                            }
                            EditorGUILayout.EndScrollView();
                        }
                    }
                    catch
                    {
                    }
                }
                GUILayout.EndArea();
            }

            if (StateWindowDisplay == null)
            {
                StateWindowDisplay = new vStateWindowDisplay();
            }
            //Tool bar
            GUILayout.BeginHorizontal(fsmSkin.GetStyle("ToolBar"));

            if (lastValidSelection)
            {
                if (GUILayout.Button(!Application.isPlaying ? "Display of " + lastValidSelection.name + " only Work in Play mode" : lastValidSelection.name, fsmSkin.GetStyle("ToolBar"), GUILayout.Height(20), GUILayout.Width(position.width - debugWindowRect.width)))
                {
                    Selection.activeObject = lastValidSelection;
                }
                GUILayout.Box("Debug Message Window", fsmSkin.GetStyle("ToolBar"), GUILayout.Height(20), GUILayout.Width(debugWindowRect.width));
                GUILayout.Space(-20);
                (target as vIFSMBehaviourController).debugMode = GUILayout.Toggle((target as vIFSMBehaviourController).debugMode, "", fsmSkin.GetStyle("ShowHideToggle"), GUILayout.Width(18), GUILayout.Height(18));
            }
            else
            {
                if (!Application.isPlaying)
                {
                    GUILayout.Box("Select a FSMBeviourController in Play mode to Display FSM Info", fsmSkin.GetStyle("ToolBar"), GUILayout.Height(20), GUILayout.Width(position.width - debugWindowRect.width));
                }
                else
                {
                    GUILayout.Box("Select a FSMBeviourController to Display FSM Info", fsmSkin.GetStyle("ToolBar"), GUILayout.Height(20), GUILayout.Width(position.width - debugWindowRect.width));
                }
            }

            GUILayout.EndHorizontal();
            ProcessEvents(Event.current);
            Repaint();
        }