/// <summary> /// Pulls an object from the pool. /// </summary> /// <returns></returns> public static Message Allocate() { // Grab the next available object Message lInstance = sPool.Allocate(); // Reset the sent flags. We do this so messages are flagged as 'completed' // by default. lInstance.IsSent = false; lInstance.IsHandled = false; // For this type, guarentee we have something // to hand back tot he caller if (lInstance == null) { lInstance = new Message(); } return lInstance; }
/// <summary> /// Returns an element back to the pool. /// </summary> /// <param name="rEdge"></param> public static void Release(Message rInstance) { if (rInstance == null) { return; } // Reset the sent flags. We do this so messages are flagged as 'completed' // and removed by default. rInstance.IsSent = true; rInstance.IsHandled = true; // Make it available to others. sPool.Release(rInstance); }
/// <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 - Next Update")) { // Send a custom message to everyone Message lMessage = new Message(); lMessage.Type = "EVERYONE"; lMessage.Sender = this; lMessage.Data = Color.green; lMessage.Delay = EnumMessageDelay.NEXT_UPDATE; 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); }