コード例 #1
0
    /// <summary>
    /// this method contains all timer example code. NOTE: timer code
    /// does not have to be located in 'OnGUI'. it can exist anywhere
    /// </summary>
    void OnGUI()
    {
        // make the status text fade back to invisible if made yellow
        m_StatusColor = Color.Lerp(m_StatusColor, new Color(1, 1, 0, 0), Time.deltaTime * 0.5f);

        // draw the header text
        GUI.color = Color.white;
        GUILayout.Space(50);
        GUILayout.BeginHorizontal();
        GUILayout.Space(50);
        GUILayout.Label("SCHEDULING EXAMPLE\n    - Each of these examples will schedule some functionality in one (1)\n      second using different options.\n    - Please study the source code in 'Examples/Scheduling/Scheduling.cs' ...");
        GUILayout.EndHorizontal();

        // create an area for all the example buttons
        GUILayout.BeginArea(new Rect(100, 150, 400, 600));
        GUI.color = Color.white;
        GUILayout.Label("Methods, Arguments, Delegates, Iterations, Intervals & Canceling");

        // --- Example 1 ---
        if (DoButton("A simple method"))
        {
            vp_Timer.In(1.0f, DoMethod);
        }

        // --- Example 2 ---
        if (DoButton("A method with a single argument"))
        {
            vp_Timer.In(1.0f, DoMethodWithSingleArgument, 242);
        }

        // --- Example 3 ---
        if (DoButton("A method with multiple arguments"))
        {
            object[] arg = new object[3];
            arg[0] = "December";
            arg[1] = 31;
            arg[2] = 2012;
            vp_Timer.In(1.0f, DoMethodWithMultipleArguments, arg);

            // TIP: you can also create the object array in-line like this:
            //vp_Timer.In(1.0f, DoMethodWithMultipleArguments, new object[] { "December", 31, 2012 });
        }

        // --- Example 4 ---
        if (DoButton("A delegate"))
        {
            vp_Timer.In(1.0f, delegate()
            {
                SmackCube();
            });
        }

        // --- Example 5 ---
        if (DoButton("A delegate with a single argument"))
        {
            vp_Timer.In(1.0f, delegate(object o)
            {
                int i = (int)o;
                SmackCube();
                SetStatus("A delegate with a single argument ... \"" + i + "\"");
            }, 242);
        }

        // --- Example 6 ---
        if (DoButton("A delegate with multiple arguments"))
        {
            vp_Timer.In(1.0f, delegate(object o)
            {
                object[] argument = (object[])o;                                // 'unpack' object into an array
                string month      = (string)argument[0];                        // convert the first argument to a string
                int day           = (int)argument[1];                           // convert the second argument to an integer
                int year          = (int)argument[2];                           // convert the third argument to an integer

                SmackCube();
                SetStatus("A delegate with multiple arguments ... \"" + month + " " + day + ", " + year + "\"");
            }, new object[] { "December", 31, 2012 });
        }

        // --- Example 7 ---
        if (DoButton("5 iterations of a method"))
        {
            vp_Timer.In(1.0f, SmackCube, 5);
        }

        // --- Example 8 ---
        if (DoButton("5 iterations of a method, with 0.2 sec intervals"))
        {
            vp_Timer.In(1.0f, SmackCube, 5, 0.2f);
        }

        // --- Example 9 ---
        if (DoButton("5 iterations of a delegate, canceled after 3 seconds"))
        {
            vp_Timer.Handle timer = new vp_Timer.Handle();
            vp_Timer.In(0.0f, delegate() { SmackCube(); }, 5, 1,
                        timer);
            vp_Timer.In(3, delegate() { timer.Cancel(); });
        }

        GUILayout.Label("\nMethod & object accessibility:");

        // --- Example 10 ---
        if (DoButton("Running a method from a non-monobehaviour class", false))
        {
            vp_Timer.In(1, delegate()
            {
                NonMonoBehaviour test = new NonMonoBehaviour();
                test.Test();
            });
        }

        // --- Example 11 ---
        if (DoButton("Running a method from a specific external gameobject", false))
        {
            vp_Timer.In(1, delegate()
            {
                TestComponent tc = (TestComponent)GameObject.Find("ExternalGameObject").GetComponent("TestComponent");
                tc.Test("Hello World!");
            });
        }

        // --- Example 12 ---
        if (DoButton("Running a method from the first component of a certain type\nin current transform or any of its children", false))
        {
            vp_Timer.In(1, delegate()
            {
                TestComponent tc = transform.root.GetComponentInChildren <TestComponent>();
                tc.Test("Hello World!");
            });
        }

        // --- Example 13 ---
        if (DoButton("Running a method from the first component of a certain type\nin the whole Hierarchy", false))
        {
            vp_Timer.In(1, delegate()
            {
                TestComponent tc = (TestComponent)FindObjectOfType(typeof(TestComponent));
                tc.Test("Hello World!");
            });
        }

        GUILayout.EndArea();

        GUI.color = m_StatusColor;

        GUILayout.BeginArea(new Rect(Screen.width - 255, 205, 240, 600));
        GUILayout.Label(m_StatusString);
        GUILayout.EndArea();
    }
コード例 #2
0
    /// <summary>
    /// this method contains all timer example code. NOTE: timer code
    /// does not have to be located in 'OnGUI'. it can exist anywhere
    /// </summary>
    void OnGUI()
    {
        // make the status text fade back to invisible if made yellow
        m_StatusColor = Color.Lerp(m_StatusColor, new Color(1, 1, 0, 0), Time.deltaTime * 0.5f);

        // draw the header text
        GUI.color = Color.white;
        GUILayout.Space(50);
        GUILayout.BeginHorizontal();
        GUILayout.Space(50);
        GUILayout.Label("SCHEDULING EXAMPLE\n    - Each of these examples will schedule some functionality in one (1)\n      second using different options.\n    - Please study the source code in 'Examples/Scheduling/Scheduling.cs' ...");
        GUILayout.EndHorizontal();

        // create an area for all the example buttons
        GUILayout.BeginArea(new Rect(100, 150, 400, 600));
        GUI.color = Color.white;
        GUILayout.Label("Methods, Arguments, Delegates, Iterations, Intervals & Canceling");

        // --- Example 1 ---
        if (DoButton("A simple method"))
        {
            vp_Timer.In(1.0f, DoMethod);
        }

        // --- Example 2 ---
        if (DoButton("A method with a single argument"))
        {
            vp_Timer.In(1.0f, DoMethodWithSingleArgument, 242);
        }

        // --- Example 3 ---
        if (DoButton("A method with multiple arguments"))
        {

            object[] arg = new object[3];
            arg[0] = "December";
            arg[1] = 31;
            arg[2] = 2012;
            vp_Timer.In(1.0f, DoMethodWithMultipleArguments, arg);

            // TIP: you can also create the object array in-line like this:
            //vp_Timer.In(1.0f, DoMethodWithMultipleArguments, new object[] { "December", 31, 2012 });

        }

        // --- Example 4 ---
        if (DoButton("A delegate"))
        {
            vp_Timer.In(1.0f, delegate()
            {
                SmackCube();
            });
        }

        // --- Example 5 ---
        if (DoButton("A delegate with a single argument"))
        {
            vp_Timer.In(1.0f, delegate(object o)
            {
                int i = (int)o;
                SmackCube();
                SetStatus("A delegate with a single argument ... \"" + i + "\"");
            }, 242);
        }

        // --- Example 6 ---
        if (DoButton("A delegate with multiple arguments"))
        {
            vp_Timer.In(1.0f, delegate(object o)
            {

                object[] argument = (object[])o;		// 'unpack' object into an array
                string month = (string)argument[0];		// convert the first argument to a string
                int day = (int)argument[1];				// convert the second argument to an integer
                int year = (int)argument[2];			// convert the third argument to an integer

                SmackCube();
                SetStatus("A delegate with multiple arguments ... \"" + month + " " + day + ", " + year + "\"");

            }, new object[] { "December", 31, 2012 });
        }

        // --- Example 7 ---
        if (DoButton("5 iterations of a method"))
        {
            vp_Timer.In(1.0f, SmackCube, 5);
        }

        // --- Example 8 ---
        if (DoButton("5 iterations of a method, with 0.2 sec intervals"))
        {
            vp_Timer.In(1.0f, SmackCube, 5, 0.2f);
        }

        // --- Example 9 ---
        if (DoButton("5 iterations of a delegate, canceled after 3 seconds"))
        {
            vp_Timer.Handle timer = new vp_Timer.Handle();
            vp_Timer.In(0.0f, delegate() { SmackCube(); }, 5, 1,
                timer);
            vp_Timer.In(3, delegate() { timer.Cancel(); });
        }

        GUILayout.Label("\nMethod & object accessibility:");

        // --- Example 10 ---
        if (DoButton("Running a method from a non-monobehaviour class", false))
        {
            vp_Timer.In(1, delegate()
            {
                NonMonoBehaviour test = new NonMonoBehaviour();
                test.Test();
            });

        }

        // --- Example 11 ---
        if (DoButton("Running a method from a specific external gameobject", false))
        {
            vp_Timer.In(1, delegate()
            {
                TestComponent tc = (TestComponent)GameObject.Find("ExternalGameObject").GetComponent("TestComponent");
                tc.Test("Hello World!");
            });
        }

        // --- Example 12 ---
        if (DoButton("Running a method from the first component of a certain type\nin current transform or any of its children", false))
        {
            vp_Timer.In(1, delegate()
            {
                TestComponent tc = transform.root.GetComponentInChildren<TestComponent>();
                tc.Test("Hello World!");
            });
        }

        // --- Example 13 ---
        if (DoButton("Running a method from the first component of a certain type\nin the whole Hierarchy", false))
        {
            vp_Timer.In(1, delegate()
            {
                TestComponent tc = (TestComponent)FindObjectOfType(typeof(TestComponent));
                tc.Test("Hello World!");
            });
        }

        GUILayout.EndArea();

        GUI.color = m_StatusColor;

        GUILayout.BeginArea(new Rect(Screen.width - 255, 205, 240, 600));
        GUILayout.Label(m_StatusString);
        GUILayout.EndArea();
    }