DelayedMessage

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "Category: MonoBehaviour Category: C Sharp Author: Hayden Scott-Baron (Dock) ==Description== A simple script to call a message on an object after a while. ==Usage== P...")
 
(C# - DelayedMessage.cs)
 
Line 14: Line 14:
 
==C# - DelayedMessage.cs==
 
==C# - DelayedMessage.cs==
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
/*
 
FadeObjectInOut.cs
 
Hayden Scott-Baron (Dock) - http://starfruitgames.com
 
6 Dec 2012
 
 
This allows you to easily fade an object and its children.
 
If an object is already partially faded it will continue from there.
 
If you choose a different speed, it will use the new speed.
 
 
NOTE: Requires materials with a shader that allows transparency through color. 
 
*/
 
 
using UnityEngine;
 
using System.Collections;
 
 
public class FadeObjectInOut : MonoBehaviour
 
{
 
 
/*
 
/*
 
DelayedMessage.cs
 
DelayedMessage.cs
Line 132: Line 115:
  
 
}
 
}
 +
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 12:11, 7 March 2013

Author: Hayden Scott-Baron (Dock)

Contents

[edit] Description

A simple script to call a message on an object after a while.

[edit] Usage

Place this script on the gameobject you wish to have call a function later on. Useful for Destroying an old object, for example, or for allowing objects to repeatedly fade in/out using the FadeObjectInOut script.

[edit] Technical Discussion

Set the initial time to negative to reduce the initial wait.

[edit] C# - DelayedMessage.cs

/*
	DelayedMessage.cs
 	Hayden Scott-Baron (Dock) - http://starfruitgames.com
 	7 Mar 2013 
 
	This uses 'SendMessage' on an object after a delay. 
 	You can send a value such as a string, int or float
 	You may also set this to repeat a message on an object. 
*/
 
using UnityEngine;
using System.Collections;
 
 
public class DelayedMessage : MonoBehaviour 
{
 
 
	public enum MessageType
	{
		No_Values,
		Send_Int,
		Send_String,
		Send_Float,
	}
	public MessageType typeOfMessage = MessageType.No_Values;  
 
	public float delayTime = 1.0f; 
	public string message = ""; 
 
	public int valueInt; 
	public float valueFloat; 
	public string valueString; 
 
	public SendMessageOptions messageOptions = SendMessageOptions.DontRequireReceiver; 
 
	public float initialDelay = 0.0f; 	
	public bool repeatMessage = false; 
 
 
	private bool startNow = true; 
	private float timer = 0.0f; 
 
	IEnumerator MessageSequence ()
	{
		timer += delayTime; 
 
		while (timer > 0.0f)
		{
			yield return null; 
			timer -= Time.deltaTime; 
		}
 
		SendMessageNow (); 
 
		if (repeatMessage)
			startNow = true; 
	}
 
	void SendMessageNow()
	{
		switch (typeOfMessage)
		{
		case MessageType.No_Values:
			SendMessage (message, messageOptions); 
			break; 
 
		case MessageType.Send_Float:
			SendMessage (message, valueFloat, messageOptions); 
			break; 
 
		case MessageType.Send_Int:
			SendMessage (message, valueInt, messageOptions); 
			break; 
 
		case MessageType.Send_String:
			SendMessage (message, valueString, messageOptions); 
			break; 
		}
	}
 
	void Awake ()
	{
		if (message == "")
			Debug.LogError ("Warning! No message on 'Delayed Message' object!", gameObject); 
 
		timer += initialDelay;
		startNow = true; 
	}
 
	void Update ()
	{
		if (startNow)
		{
			startNow = false; 
			StartCoroutine ( "MessageSequence" ); 
		}
	}
 
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox