Events
Geprüft mit Version: 4.1
-
Schwierigkeitsgrad: Fortgeschrittene Anfänger
How to create a dynamic "broadcast" system using Events.


Events
Fortgeschrittene Anfänger Scripting
EventManager
Code snippet
using UnityEngine;
using System.Collections;
public class EventManager : MonoBehaviour
{
public delegate void ClickAction();
public static event ClickAction OnClicked;
void OnGUI()
{
if(GUI.Button(new Rect(Screen.width / 2 - 50, 5, 100, 30), "Click"))
{
if(OnClicked != null)
OnClicked();
}
}
}
//Javascript doesn't have a built-in "event". Instead,
//you will need to use C# to gain this functionality.
//Other scripts written in Javascript can still subscribe
//to events created in C#.
import UnityEngine
import System.Collections
public class EventManager(MonoBehaviour):
public callable ClickAction() as void
public static event OnClicked as ClickAction
private def OnGUI():
if GUI.Button(Rect(((Screen.width / 2) - 50), 5, 100, 30), 'Click'):
OnClicked()
TeleportScript
Code snippet
using UnityEngine;
using System.Collections;
public class TeleportScript : MonoBehaviour
{
void OnEnable()
{
EventManager.OnClicked += Teleport;
}
void OnDisable()
{
EventManager.OnClicked -= Teleport;
}
void Teleport()
{
Vector3 pos = transform.position;
pos.y = Random.Range(1.0f, 3.0f);
transform.position = pos;
}
}
#pragma strict
//Scripts written in Javascript can subscribe to
//events made in C#. The key is that the C# scripts
//must be compiled before the Javascripts. In the are
//compiled at the same time, they won't be able to
//"see" each other.
function OnEnable()
{
EventManager.OnClicked += Teleport;
}
function OnDisable()
{
EventManager.OnClicked -= Teleport;
}
function Teleport()
{
var pos : Vector3 = transform.position;
pos.y = Random.Range(1.0f, 3.0f);
transform.position = pos;
}
import UnityEngine
import System.Collections
public class TeleportScript(MonoBehaviour):
private def OnEnable():
EventManager.OnClicked += Teleport
private def OnDisable():
EventManager.OnClicked -= Teleport
private def Teleport():
pos as Vector3 = transform.position
pos.y = Random.Range(1.0F, 3.0F)
transform.position = pos
TurnColorScript
Code snippet
using UnityEngine;
using System.Collections;
public class TurnColorScript : MonoBehaviour
{
void OnEnable()
{
EventManager.OnClicked += TurnColor;
}
void OnDisable()
{
EventManager.OnClicked -= TurnColor;
}
void TurnColor()
{
Color col = new Color(Random.value, Random.value, Random.value);
renderer.material.color = col;
}
}
#pragma strict
//Scripts written in Javascript can subscribe to
//events made in C#. The key is that the C# scripts
//must be compiled before the Javascripts. In the are
//compiled at the same time, they won't be able to
//"see" each other.
function OnEnable()
{
EventManager.OnClicked += TurnColor;
}
function OnDisable()
{
EventManager.OnClicked -= TurnColor;
}
function TurnColor()
{
var col : Color = new Color(Random.value, Random.value, Random.value);
renderer.material.color = col;
}
import UnityEngine
import System.Collections
public class TurnColorScript(MonoBehaviour):
private def OnEnable():
EventManager.OnClicked += TurnColor
private def OnDisable():
EventManager.OnClicked -= TurnColor
private def TurnColor():
col = Color(Random.value, Random.value, Random.value)
renderer.material.color = col
Ähnliche Tutorials
- Delegates (Lektion)