赞
踩
using UnityEngine; public class CoroutineExample : MonoBehaviour { void Start() { StartCoroutine(MyCoroutine()); } IEnumerator MyCoroutine() { Debug.Log("Coroutine started"); yield return new WaitForSeconds(2f); Debug.Log("Coroutine resumed after 2 seconds"); } }
using UnityEngine; public class CameraController : MonoBehaviour { public Transform target; public float smoothSpeed = 2f; void Update() { StartCoroutine(MoveCameraSmoothly(target.position, smoothSpeed)); } IEnumerator MoveCameraSmoothly(Vector3 targetPosition, float speed) { while (Vector3.Distance(transform.position, targetPosition) > 0.1f) { transform.position = Vector3.Lerp(transform.position, targetPosition, speed * Time.deltaTime); yield return null; } Debug.Log("Camera reached the target position"); } }
using UnityEngine; using UnityEngine.Events; public class EventExample : MonoBehaviour { // Declare an event public UnityEvent myEvent; void Start() { // Subscribe a method to the event myEvent.AddListener(MyEventHandler); // Invoke the event myEvent.Invoke(); } void MyEventHandler() { Debug.Log("Event handled!"); } }
using UnityEngine; using UnityEngine.Events; public class PlayerHealth : MonoBehaviour { public int maxHealth = 100; private int currentHealth; // Declare a health change event public UnityEvent onHealthChanged; void Start() { currentHealth = maxHealth; // Subscribe the UpdateUI method to the health change event onHealthChanged.AddListener(UpdateUI); } void TakeDamage(int damage) { currentHealth -= damage; // Invoke the health change event onHealthChanged.Invoke(); } void UpdateUI() { Debug.Log("Player health updated. Current health: " + currentHealth); } }
using UnityEngine; public class DelegateExample : MonoBehaviour { // Declare a delegate public delegate void MyDelegate(); // Declare an instance of the delegate public MyDelegate myDelegate; void Start() { // Assign a method to the delegate myDelegate = MyDelegateMethod; // Invoke the delegate myDelegate(); } void MyDelegateMethod() { Debug.Log("Delegate method invoked!"); } }
using UnityEngine; public class CustomEventSystem : MonoBehaviour { // Declare a delegate for the event public delegate void GameEventHandler(); // Declare an event using the delegate public static event GameEventHandler OnGameEvent; void Start() { // Subscribe methods to the event OnGameEvent += PlaySound; OnGameEvent += SpawnParticles; // Invoke the event OnGameEvent?.Invoke(); } void PlaySound() { Debug.Log("Sound played!"); } void SpawnParticles() { Debug.Log("Particles spawned!"); } }
using UnityEngine; using UnityEngine.Events; using System; public class GameTimerSystem : MonoBehaviour { // Unity event for notifying components when the game starts public UnityEvent onStartGame; // Unity event for notifying components when the game ends public UnityEvent onEndGame; // Custom delegate for dynamic callbacks during the game public delegate void GameUpdateDelegate(float timeRemaining); public static event GameUpdateDelegate onGameUpdate; private float gameDuration = 60f; // Game duration in seconds void Start() { StartCoroutine(StartGame()); } IEnumerator StartGame() { Debug.Log("Game starting in 3 seconds..."); // Wait for 3 seconds before starting the game yield return new WaitForSeconds(3f); Debug.Log("Game started!"); // Notify components that the game has started onStartGame?.Invoke(); // Run the game loop StartCoroutine(GameLoop()); } IEnumerator GameLoop() { float currentTime = gameDuration; while (currentTime > 0) { // Update the game timer onGameUpdate?.Invoke(currentTime); // Wait for one second yield return new WaitForSeconds(1f); // Decrement the timer currentTime--; // Simulate events happening during the game (for example, spawning enemies) if (currentTime == gameDuration - 10) { Debug.Log("10 seconds remaining - spawning enemies!"); SpawnEnemies(); } } // Game over Debug.Log("Game over!"); // Notify components that the game has ended onEndGame?.Invoke(); } void SpawnEnemies() { Debug.Log("Enemies spawned!"); } // Subscribe to the game update delegate void OnEnable() { onGameUpdate += DisplayTimeRemaining; } // Unsubscribe from the game update delegate void OnDisable() { onGameUpdate -= DisplayTimeRemaining; } // Method to be called when the game updates void DisplayTimeRemaining(float timeRemaining) { Debug.Log("Time remaining: " + timeRemaining + " seconds"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。