コード例 #1
0
    // process the message locally. The method name can be customized to match the message type
    // make it easier to figure out where messages are processed
    bool HandleMyCustomMessage(BaseMessage msg)
    {
        MyCustomMessage castMsg = msg as MyCustomMessage;

        Debug.Log(string.Format("Got the message! {0}, {1}", castMsg._intValue, castMsg._floatValue));
        return(true);
    }
コード例 #2
0
        /// <summary>
        /// Raised when we get the custom message
        /// </summary>
        /// <param name="rMessage">Customized message</param>
        public void OnCustomMessageReceived(IMessage rMessage)
        {
            int lMaxHealth     = ((MyCustomMessage)rMessage).MaxHealth;
            int lCurrentHealth = ((MyCustomMessage)rMessage).CurrentHealth;

            mCustomMessageResult = lCurrentHealth + " of " + lMaxHealth + " health";

            MyCustomMessage.Release(rMessage);
        }
コード例 #3
0
        /// <summary>
        /// Place the buttons and send messages when clicked
        /// </summary>
        void OnGUI()
        {
            // Show the buttons
            if (GUI.Button(mClear, "Clear"))
            {
                // Send the message to everyone listening
                MessageDispatcher.SendMessage("EVERYONE");
            }

            if (GUI.Button(mSpheresBlue, "Spheres Blue"))
            {
                // Send the message, but only the listeners filtering on "Sphere" will react
                MessageDispatcher.SendMessage("FILTER", "Sphere");
            }

            if (GUI.Button(mCubesBlue, "Cubes Blue"))
            {
                // Send the message, but only the listeners filtering on "Cube" will react
                MessageDispatcher.SendMessage("FILTER", "Cube");
            }

            if (GUI.Button(mLeadSphereRed, "Lead Sphere Red"))
            {
                // Send the message to a specific sphere (by name)
                MessageDispatcher.SendMessage(gameObject, "Sphere_1", "TARGET", null, EnumMessageDelay.IMMEDIATE);
            }

            if (GUI.Button(mLeadCubeRed, "Lead Cube Red"))
            {
                // Send the message to a specific cube (by name)
                MessageDispatcher.SendMessage(gameObject, "Cube_1", "TARGET", null, EnumMessageDelay.IMMEDIATE);
            }

            if (GUI.Button(mAllYellow, "All Yellow"))
            {
                // Send a custom message to everyone
                Message lMessage = new Message();
                lMessage.Type   = "EVERYONE";
                lMessage.Sender = this;
                lMessage.Data   = Color.yellow;
                lMessage.Delay  = EnumMessageDelay.IMMEDIATE;
                MessageDispatcher.SendMessage(lMessage);
            }

            if (GUI.Button(mAllGreen, "All Green"))
            {
                // Send a custom message to everyone
                Message lMessage = new Message();
                lMessage.Type   = "EVERYONE";
                lMessage.Sender = this;
                lMessage.Data   = Color.green;
                lMessage.Delay  = EnumMessageDelay.IMMEDIATE;
                MessageDispatcher.SendMessage(lMessage);
            }

            if (GUI.Button(mDelayBlue, "Delay - All Blue"))
            {
                // Send a custom message to everyone (either approach works)

                //Message lMessage = new Message();
                //lMessage.Type = "EVERYONE";
                //lMessage.Sender = this;
                //lMessage.Data = Color.blue;
                //lMessage.Delay = 1.0f;
                //MessageDispatcher.SendMessage(lMessage);

                MessageDispatcher.SendMessage(null, "", "EVERYONE", Color.blue, 1f);
            }

            if (GUI.Button(mCustomMessage, "Delay - Custom Msg"))
            {
                mCustomMessageResult = "";

                // Send a custom message to everyone
                MyCustomMessage lMessage = MyCustomMessage.Allocate();
                lMessage.Type          = "CUSTOM";
                lMessage.MaxHealth     = 100;
                lMessage.CurrentHealth = 50;
                lMessage.Sender        = this;
                lMessage.Data          = Color.blue;
                lMessage.Delay         = 1.0f;
                MessageDispatcher.SendMessage(lMessage);
            }

            if (GUI.Button(mBadMessage, "Unhandled Message"))
            {
                // Send a custom message to everyone
                Message lMessage = new Message();
                lMessage.Type   = "UNHANDLED";
                lMessage.Sender = this;
                MessageDispatcher.SendMessage(lMessage);
            }

            if (GUI.Button(mTest, "Perf. Test"))
            {
                mCustomMessageResult = "";

                mPhase      = 0;
                mPhase1Time = 0;
                mPhase2Time = 0;
                mIsTesting  = true;
                mInstances  = 500;
                mCounter    = 1;
            }

            // Update the testing
            if (mIsTesting)
            {
                if (mInstances > 0)
                {
                    if (mPhase == 0)
                    {
                        // Send a custom message to update the counter
                        mProfiler.Start();
                        MessageDispatcher.SendMessage("COUNTER");
                        mProfiler.Stop();

                        if (mCounter > 10)
                        {
                            mPhase1Time += mProfiler.ElapsedTime;
                        }

                        // Stop if we've finished the test
                        if (mCounter >= mInstances)
                        {
                            mPhase   = 1;
                            mCounter = 0;
                        }
                    }
                    else if (mPhase == 1)
                    {
                        // Send a custom message to update the counter
                        mProfiler.Start();
                        gameObject.SendMessage("UpdateCounter");
                        mProfiler.Stop();

                        if (mCounter > 10)
                        {
                            mPhase2Time += mProfiler.ElapsedTime;
                        }

                        // Stop if we've finished the test
                        if (mCounter >= mInstances)
                        {
                            mPhase   = 2;
                            mCounter = 0;
                        }
                    }
                    else
                    {
                        // Send a custom message to update the counter
                        mProfiler.Start();
                        UpdateCounter();
                        mPhase3Time += mProfiler.Stop();

                        if (mCounter > 10)
                        {
                            mPhase3Time += mProfiler.ElapsedTime;
                        }

                        // Stop if we've finished the test
                        if (mCounter >= mInstances)
                        {
                            mInstances = 0;
                        }
                    }
                }
            }

            // Show the label
            string lText = "Message Dispatcher";

            if (mCustomMessageResult.Length > 0)
            {
                lText += "\r\n" + mCustomMessageResult;
            }

            if (mIsTesting)
            {
                lText += "\r\n" + "" + mCounter + " msgs => MD: " + (mPhase1Time * mTicksPerMS).ToString("0") + " ticks   SMess: " + (mPhase2Time * mTicksPerMS).ToString("0") + " ticks   Direct: " + (mPhase3Time * mTicksPerMS).ToString("0") + " ticks";

                if (mInstances == 0)
                {
                    lText += "\r\n" + " MD: " + mPhase1Time.ToString("0.0000") + " ms   SMess: " + mPhase2Time.ToString("0.0000") + " ms   Direct: " + mPhase3Time.ToString("0.0000") + " ms";
                }
            }

            GUIStyle lStyle = new GUIStyle();

            lStyle.alignment        = TextAnchor.MiddleCenter;
            lStyle.normal.textColor = Color.grey;
            lStyle.fontSize         = 24;

            GUI.color        = Color.white;
            GUI.contentColor = Color.white;

            GUI.Label(mPerformance, lText, lStyle);
        }