public void OnGUI()
    {
        needRepaint = false;
        int oldState = curState;

        textureAreaBottom = 0;
        isDirty           = false;

        if (restarted)
        {
            selGO   = null;
            control = null;
            OnSelectionChange();
            restarted = false;
        }

        // See if our window size has changed:
        if (wndRect != position)
        {
            WindowResized();
        }

        // See if we need to update our selection:
        if (Selection.activeGameObject != selGO)
        {
            OnSelectionChange();
        }

//#if UNITY_IPHONE && !(UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9)
        if (Selection.activeGameObject != null)
        {
            control = (IControl)Selection.activeGameObject.GetComponent("IControl");
        }
//#endif

        // Bailout if we don't have valid values:
        if (null == (MonoBehaviour)control)
        {
            PrintNoSelectMsg();
            return;
        }



        // Start keeping track of any changed values:
        BeginMonitorChanges();

        // Do the pre-state selection GUI, if any:
        height = control.DrawPreStateSelectGUI(curState, false);

        EndMonitorChanges();



        // Get the control's state names:
        stateNames = control.EnumStateElements();
        if (stateNames == null)
        {
            return;
        }

        // Cap our state to the number of states available:
        if (stateNames != null)
        {
            curState = Mathf.Min(curState, stateNames.Length - 1);
        }
        else
        {
            curState = 0;
        }

        // Choose the state we want to edit:
        curState = GUILayout.Toolbar(curState, stateNames);

        // Reset our selected transition element
        // if the state selection changed:
        if (curState != oldState)
        {
            curFromTrans    = 0;
            curTransElement = 0;
        }



        // Keep track of any changed values:
        BeginMonitorChanges();

        // Do the post-state selection GUI, if any:
        height += control.DrawPostStateSelectGUI(curState);

        EndMonitorChanges();


        // Adjust our texture selection rect:
        tempRect    = texRect;
        tempRect.y += height;



        if (control is IPackableControl)
        {
            ShowSpriteSettings();
        }
        else
        {
            stateInfo = control.GetStateElementInfo(curState);
        }

        transitions = stateInfo.transitions;



        if (!Application.isPlaying)
        {
#if UNITY_IPHONE && !(UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9)
            // Box off our script selection and transition fields area:
            GUILayout.BeginArea(new Rect(0, textureAreaBottom, position.width, position.height - textureAreaBottom));
            GUILayout.FlexibleSpace();
#endif
            //-----------------------------------------
            // Draw script selection:
            //-----------------------------------------
            BeginMonitorChanges();
            control.DrawPreTransitionUI(curState, this);
            EndMonitorChanges();


            //-----------------------------------------
            // Draw our state label stuff:
            //-----------------------------------------
            if (stateInfo.stateLabel != null)
            {
                BeginMonitorChanges();
                DoStateLabel();
                EndMonitorChanges();
            }


            //-----------------------------------------
            // Draw our transition stuff:
            //-----------------------------------------
            if (transitions != null)
            {
                if (transitions.list != null)
                {
                    if (transitions.list.Length > 0)
                    {
                        DoTransitionStuff();
                    }
                }
            }



#if UNITY_IPHONE && !(UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9)
            // End the boxed off area for our script selection and transition fields.
            GUILayout.EndArea();
#endif
        }


        GUILayout.BeginVertical();
        GUILayout.Space(10f);
        GUILayout.EndVertical();

        // Set dirty if anything changed:
        if (isDirty)
        {
            EditorUtility.SetDirty((MonoBehaviour)control);
        }

        if (needRepaint)
        {
            Repaint();
        }
    }