ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2023ǯ09·î25Æü(·î) 13:48:11ÍúÎò
¡¡°Ê²¼¤Ï¡¢¥³¥ë¡¼¥Á¥ó¤ò»ÈÍѤ·¤Æ¥²¡¼¥àÆâ¤Ç°ìÈÌŪ¤Ë¹Ô¤ï¤ì¤ë¥¿¥¹¥¯¤ò¼Â¹Ô¤¹¤ë¥µ¥ó¥×¥ë¥³¡¼¥É¤Ç¤¹¡£
¡¡¥×¥ì¥¤¥ä¡¼¥¥ã¥é¥¯¥¿¡¼¤¬¥À¥á¡¼¥¸¤ò¼õ¤±¤¿ºÝ¤ËÅÀÌǤ¹¤ë¥¨¥Õ¥§¥¯¥È¤ò¼ÂÁõ¤·¤Þ¤¹¡£
¡¡WaitForSeconds ¥¯¥é¥¹¤òÍøÍѤ·¤Æ¡¢°ú¿ô¤Ë»ØÄꤷ¤¿»þ´Ö¡¢½èÍý¤òÃæÃǤ·¤ÆºÆ³«¤µ¤»¤ëµ¡Ç½¤ò¼ÂÁõ¤·¤Æ¤¤¤Þ¤¹¡£
»²¹Í¥µ¥¤¥È
Unity ¸ø¼°¥¹¥¯¥ê¥×¥È¡¦¥ê¥Õ¥¡¥ì¥ó¥¹
WaitForSeconds
using UnityEngine; using System.Collections; public class PlayerHealth : MonoBehaviour { private Renderer playerRenderer; private bool isInvulnerable = false; void Start() { playerRenderer = GetComponent<Renderer>(); } void Update() { if (Input.GetKeyDown(KeyCode.Space) && !isInvulnerable) { StartCoroutine(TakeDamage()); } } IEnumerator TakeDamage() { isInvulnerable = true; for (int i = 0; i < 5; i++) { playerRenderer.enabled = false; yield return new WaitForSeconds(0.2f); playerRenderer.enabled = true; yield return new WaitForSeconds(0.2f); } isInvulnerable = false; } }
¡¡¤³¤Î¥³¡¼¥É¤Ï¡¢¥×¥ì¥¤¥ä¡¼¥¥ã¥é¥¯¥¿¡¼¤¬¥À¥á¡¼¥¸¤ò¼õ¤±¤ë¤È¡¢ÅÀÌǤ¹¤ë¥¨¥Õ¥§¥¯¥È¤ò5²ó·«¤êÊÖ¤·¤Þ¤¹¡£
¤Þ¤¿ isInvulnerable ¥Õ¥é¥°¤ò»ÈÍѤ·¤Æ¡¢Ï¢Â³¤·¤Æ¥À¥á¡¼¥¸¤ò¼õ¤±¤Ê¤¤¤è¤¦¤Ë¤·¤Þ¤¹¡£
¡¡¤³¤Î¥µ¥ó¥×¥ë¤Ç¤Ï¡¢»ØÄꤷ¤¿Éÿô¤´¤È¤ËŨ¤òÀ¸À®¤¹¤ëÎã¤ò¼¨¤·¤Þ¤¹¡£
using UnityEngine; using System.Collections; public class EnemySpawner : MonoBehaviour { public GameObject enemyPrefab; public float spawnInterval = 3.0f; void Start() { StartCoroutine(SpawnEnemies()); } IEnumerator SpawnEnemies() { while (true) { Instantiate(enemyPrefab, transform.position, Quaternion.identity); yield return new WaitForSeconds(spawnInterval); } } }
¡¡¤³¤Î¥³¡¼¥É¤Ç¤Ï¡¢enemyPrefab ÊÑ¿ô¤ËÀ¸À®¤·¤¿¤¤Å¨¤Î¥×¥ì¥Ï¥Ö¤ò³ä¤êÅö¤Æ¡¢spawnInterval ÊÑ¿ô¤ËÀ¸À®´Ö³Ö¤òÉÃñ°Ì¤Ç»ØÄꤷ¤Þ¤¹¡£
¥³¥ë¡¼¥Á¥ó SpawnEnemies ¤Ï¡¢»ØÄꤵ¤ì¤¿´Ö³Ö¤´¤È¤ËŨ¤òÀ¸À®¤·¤Þ¤¹¡£
¡¡¾åµ¤Î½èÍý¤Î¾ì¹ç¡¢¥²¡¼¥à¼Â¹ÔÃæ¤Ï¤º¤Ã¤ÈŨ¤¬À¸À®¤µ¤ì³¤±¤Þ¤¹¡£
¡¡Àµ¾ï¤ËÆ°ºî¤¹¤ë¤³¤È¤¬³Îǧ¤Ç¤¤¿¤é¡¢½èÍý¤ò½¤Àµ¤·¡¢»ØÄꤵ¤ì¤¿¿ô¤À¤±Å¨¤òÀ¸À®¤·¤¿¤é¡¢À¸À®¤òÄä»ß¤¹¤ë½èÍý¤òºî¤Ã¤Æ¤ß¤Þ¤·¤ç¤¦¡£
¡¡»ØÄꤷ¤¿¿ô¤À¤±Å¨¤òÀ¸À®¤·¤¿¤é½èÍý¤òÄä»ß¤µ¤»¤ë¤Ë¤Ï¡¢ÊÑ¿ô¤ò»ÈÍѤ·¤ÆÀ¸À®¤·¤¿Å¨¤Î¿ô¤ò¥«¥¦¥ó¥È¤·¡¢ÆÃÄê¤Î¾ò·ï¤¬Ëþ¤¿¤µ¤ì¤¿¤é¥³¥ë¡¼¥Á¥ó¤ò½ªÎ»¤¹¤ë¤³¤È¤Çɽ¸½¤Ç¤¤Þ¤¹¡£
¡¡°Ê²¼¤Ï¡¢»ØÄꤷ¤¿¿ô¤ÎŨ¤òÀ¸À®¤·¤¿¤é¥³¥ë¡¼¥Á¥ó¤ò½ªÎ»¤¹¤ë¥µ¥ó¥×¥ë¥³¡¼¥É¤Ç¤¹¡£
using UnityEngine; using System.Collections; public class EnemySpawner : MonoBehaviour { public GameObject enemyPrefab; public int maxEnemiesToSpawn = 10; // À¸À®¤¹¤ëºÇÂç¤ÎŨ¤Î¿ô public float spawnInterval = 3.0f; private int spawnedEnemiesCount = 0; // À¸À®¤µ¤ì¤¿Å¨¤Î¿ô void Start() { StartCoroutine(SpawnEnemies()); } IEnumerator SpawnEnemies() { while (spawnedEnemiesCount < maxEnemiesToSpawn) { Instantiate(enemyPrefab, transform.position, Quaternion.identity); spawnedEnemiesCount++; // À¸À®¤µ¤ì¤¿Å¨¤Î¿ô¤òÁý¤ä¤¹ yield return new WaitForSeconds(spawnInterval); } } }
¡¡¤³¤Î¥³¡¼¥É¤Ç¤Ï¡¢maxEnemiesToSpawn ¥Õ¥£¡¼¥ë¥É¤ÇÀ¸À®¤¹¤ëºÇÂç¤ÎŨ¤Î¿ô¤ò»ØÄꤷ¡¢spawnedEnemiesCount ÊÑ¿ô¤ÇÀ¸À®¤µ¤ì¤¿Å¨¤Î¿ô¤ò¥«¥¦¥ó¥È¤·¤Æ¤¤¤Þ¤¹¡£
¥³¥ë¡¼¥Á¥óÆâ¤ÇÀ¸À®¤·¤¿Å¨¤Î¿ô¤¬ maxEnemiesToSpawn ¤Ë㤷¤¿¾ì¹ç¡¢¥ë¡¼¥×¤òÈ´¤±¤Æ¥³¥ë¡¼¥Á¥ó¤¬½ªÎ»¤·¡¢Å¨¤ÎÀ¸À®¤¬Ää»ß¤·¤Þ¤¹¡£
¡¡Àè¤Û¤É¤Î¡Ú£²¡Û¤Î½èÍý¤ò±þÍѤ·¡¢»ØÄê»þ´Ö¤´¤È¤Ë¥¨¥Õ¥§¥¯¥È¤òÀ¸À®¤¹¤ëµ¡Ç½¤ò¼ÂÁõ¤·¤Þ¤¹¡£
¡¡¤³¤³¤Ç¤Ï²Ö²Ð¤Î¥¨¥Õ¥§¥¯¥È¤òÀ¸À®¤¹¤ë¥¤¥á¡¼¥¸¤Ç¡¢À¸À®¤µ¤ì¤ë°ÌÃ֤˥é¥ó¥À¥àÀ¤òÀߤ±¤Æ¡¢Æ±¤¸°ÌÃ֤˲ֲФ¬¾å¤¬¤é¤Ê¤¤¤è¤¦¤Ë¤·¤Æ¤¤¤Þ¤¹¡£
using UnityEngine; using System.Collections; public class FireworksController : MonoBehaviour { [SerializeField] private GameObject fireworksPrefab; [SerializeField] private int numberOfFireworks = 10; // À¸À®¤¹¤ë²Ö²Ð¤Î¿ô [SerializeField] private float spawnInterval = 5.0f; [SerializeField] private float minOffset = -1.0f; // ¥é¥ó¥À¥à¤Ê°ÌÃ֤κǾ®¥ª¥Õ¥»¥Ã¥È [SerializeField] private float maxOffset = 1.0f; // ¥é¥ó¥À¥à¤Ê°ÌÃ֤κÇÂ祪¥Õ¥»¥Ã¥È void Start() { StartCoroutine(SpawnFireworks()); } IEnumerator SpawnFireworks() { int fireworksCount = 0; // ¥í¡¼¥«¥ëÊÑ¿ô¤È¤·¤Æ fireworksCount ¤òÄêµÁ while (fireworksCount < numberOfFireworks) { Vector3 spawnPosition = transform.position + new Vector3(Random.Range(minOffset, maxOffset), 0f, Random.Range(minOffset, maxOffset)); Instantiate(fireworksPrefab, spawnPosition, Quaternion.identity); fireworksCount++; yield return new WaitForSeconds(spawnInterval); } } }
¡¡ÇÛÎó¤òÍøÍѤ¹¤ë¤³¤È¤Ë¤è¤ê¡¢²Ö²Ð¤Î¼ïÎà¤ò¥é¥ó¥À¥à¤ËÊѲ½¤µ¤»¤ë¤è¤¦¤Ë½¤Àµ¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£
¥¨¥Õ¥§¥¯¥È¤ò¥é¥ó¥À¥à¤ËÊѹ¹¤¹¤ë¤¿¤á¤Ë¡¢¥¨¥Õ¥§¥¯¥È¤Î¥×¥ì¥Ï¥Ö¤òÇÛÎó¤Ë³ÊǼ¤·¡¢¥é¥ó¥À¥à¤Ê¥¤¥ó¥Ç¥Ã¥¯¥¹¤òÁªÂò¤·¤ÆÀ¸À®¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¡¡°Ê²¼¤Ï¡¢½¤Àµ¤µ¤ì¤¿¥³¡¼¥É¤Ç¤¹¡£
using UnityEngine; using System.Collections; public class FireworksController : MonoBehaviour { [SerializeField] private GameObject[] fireworksPrefabs; // Ê£¿ô¤Î²Ö²Ð¤Î¥×¥ì¥Ï¥Ö¤ò³ÊǼ¤¹¤ëÇÛÎó [SerializeField] private int numberOfFireworks = 10; // À¸À®¤¹¤ë²Ö²Ð¤Î¿ô [SerializeField] private float spawnInterval = 5.0f; [SerializeField] private float minOffset = -1.0f; // ¥é¥ó¥À¥à¤Ê°ÌÃ֤κǾ®¥ª¥Õ¥»¥Ã¥È [SerializeField] private float maxOffset = 1.0f; // ¥é¥ó¥À¥à¤Ê°ÌÃ֤κÇÂ祪¥Õ¥»¥Ã¥È void Start() { StartCoroutine(SpawnFireworks()); } IEnumerator SpawnFireworks() { int fireworksCount = 0; // ¥í¡¼¥«¥ëÊÑ¿ô¤È¤·¤Æ fireworksCount ¤òÄêµÁ while (fireworksCount < numberOfFireworks) { // ¥é¥ó¥À¥à¤Ê²Ö²Ð¤Î¥×¥ì¥Ï¥Ö¤òÁªÂò int randomIndex = Random.Range(0, fireworksPrefabs.Length); GameObject selectedPrefab = fireworksPrefabs[randomIndex]; // ¥é¥ó¥À¥à¤Ê°ÌÃ֤˲ֲФòÀ¸À® Vector3 spawnPosition = transform.position + new Vector3(Random.Range(minOffset, maxOffset), 0f, Random.Range(minOffset, maxOffset)); Instantiate(selectedPrefab, spawnPosition, Quaternion.identity); fireworksCount++; yield return new WaitForSeconds(spawnInterval); } } }
¡¡¤³¤Î½¤Àµ¤µ¤ì¤¿¥³¡¼¥É¤Ç¤Ï¡¢fireworksPrefabs ÇÛÎó¤ËÊ£¿ô¤Î²Ö²Ð¤Î¥×¥ì¥Ï¥Ö¤ò³ÊǼ¤·¡¢¥é¥ó¥À¥à¤Ê¥¤¥ó¥Ç¥Ã¥¯¥¹¤òÁªÂò¤·¤Æ²Ö²Ð¤òÀ¸À®¤·¤Þ¤¹¡£
¡¡¤³¤ì¤Ë¤è¤ê¡¢²Ö²Ð¤Ë¸Â¤é¤º¡¢¥é¥ó¥À¥à¤Ê¥¨¥Õ¥§¥¯¥È¤¬À¸À®¤µ¤ì¤ë¤è¤¦¤Ë¤Ê¤ê¤Þ¤¹¡£
¡¡¤³¤ì¤òÀè¤Û¤É¤ÎŨ¤ÎÀ¸À®¤Ë±þÍѤ¹¤ì¤Ð¡¢¥é¥ó¥À¥à¤ÊŨ¤¬¡¢¥é¥ó¥À¥à¤Ê°ÌÃÖ¤«¤éÀ¸À®¤µ¤ì¤ëµ¡Ç½¤òºî¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡£
¡¡¥×¥í¥°¥é¥à¤Ï¤¹¤Ù¤Æ´ðÁäαþÍѤǤ¹¡£
¤¢¤Ê¤¿Æ¬¤ËÉ⤫¤ó¤À¥²¡¼¥àÆâ¤Î½èÍý¤ò¤É¤Î¤è¤¦¤Ë¤¹¤ì¤Ðɽ¸½¤Ç¤¤ë¤«¡¢¥¤¥á¡¼¥¸¤ò¹Í¤¨¤Æ¤ß¤Þ¤·¤ç¤¦¡£
¡¡¥Þ¥¦¥¹¥¯¥ê¥Ã¥¯¤¬¹Ô¤ï¤ì¤ë¤Þ¤ÇÂÔµ¡¤¹¤ë¥µ¥ó¥×¥ë¥³¡¼¥É¤Ç¤¹¡£
¡¡WaitForSeconds ¥¯¥é¥¹¤ÎÂå¤ï¤ê¤Ë WaitUntil ¥¯¥é¥¹¤òÍøÍѤ·¡¢ÆÃÄê¤Î¾ò·ï¤òËþ¤¿¤¹¤Þ¤Ç½èÍý¤òÃæÃǤ¹¤ëµ¡Ç½¤ò¼ÂÁõ¤·¤Æ¤¤¤Þ¤¹¡£
»²¹Í¥µ¥¤¥È
Unity ¸ø¼°¥¹¥¯¥ê¥×¥È¡¦¥ê¥Õ¥¡¥ì¥ó¥¹
WaitUntil
using UnityEngine; using System.Collections; public class MouseClickWait : MonoBehaviour { void Start() { StartCoroutine(WaitForMouseClick()); } IEnumerator WaitForMouseClick() { yield return new WaitUntil(() => Input.GetMouseButtonDown(0)); Debug.Log("¥Þ¥¦¥¹¥¯¥ê¥Ã¥¯¤¬¸¡½Ð¤µ¤ì¤Þ¤·¤¿¡£"); } }
¡¡¾åµ¤Î½èÍý¤ò²þÎɤ·¡¢¥Þ¥¦¥¹¥¯¥ê¥Ã¥¯¤·¤¿¤é¥·¡¼¥óÁ«°Ü¤¹¤ë½èÍý¤ò¹Í¤¨¤Æ¤ß¤Þ¤·¤ç¤¦¡£
Î㤨¤Ð¡¢¥¿¥¤¥È¥ë¥·¡¼¥ó¢ª¥á¥¤¥ó¥·¡¼¥ó¡¢¥á¥¤¥ó¥·¡¼¥ó¢ª¥ê¥¶¥ë¥È¥·¡¼¥ó¤È¤¤¤Ã¤¿Éôʬ¤ÇÍøÍѤ¹¤ë¥¤¥á¡¼¥¸¤Ç¤¹¡£
using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; public class MouseClickToStart : MonoBehaviour { [SerializeField] private string nextSceneName = "NextScene"; // ¼¡¤Î¥·¡¼¥ó¤Î̾Á° void Start() { StartCoroutine(WaitForMouseClick()); } IEnumerator WaitForMouseClick() { yield return new WaitUntil(() => Input.GetMouseButtonDown(0)); // ¥Þ¥¦¥¹¥¯¥ê¥Ã¥¯¤ò´¶ÃΤ·¤¿¤é»ØÄꤷ¤¿¥·¡¼¥ó¤ËÁ«°Ü¤¹¤ë SceneManager.LoadScene(nextSceneName); } }
¡¡¥Þ¥¦¥¹¥¯¥ê¥Ã¥¯¤ÎÉôʬ¤ò¥¡¼¥Ü¡¼¥É¤ËÊѹ¹¤¹¤ì¤Ð¡¢ÆÃÄê¤Î¥Ü¥¿¥ó¤Èɳ¤Å¤±¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡£
¡¡while ¥ë¡¼¥×¤È yield return ¥¹¥Æ¡¼¥È¥á¥ó¥È¤òÁȤ߹ç¤ï¤»¤Æ¡¢°ìÄê¤Î¾ò·ï¤òËþ¤¿¤¹¤Þ¤ÇÂÔµ¡¤¹¤ë¤¿¤á¤ÎÆȼ«¤ÎÂÔµ¡¥ë¡¼¥×¤òºîÀ®¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¡¡°Ê²¼¤Ï¡¢while ¥ë¡¼¥×¤ò»ÈÍѤ·¤Æ¾ò·ï¤¬Ëþ¤¿¤µ¤ì¤ë¤Þ¤ÇÂÔµ¡¤¹¤ë¥µ¥ó¥×¥ë¥³¡¼¥É¤Ç¤¹¡£
using System.Collections; using UnityEngine; public class Example : MonoBehaviour { bool conditionMet = false; // ÂÔµ¡¾ò·ï IEnumerator WaitForCondition() { while (!conditionMet) { // ¾ò·ï¤¬Ëþ¤¿¤µ¤ì¤ë¤Þ¤ÇÂÔµ¡ yield return null; } // ¾ò·ï¤¬Ëþ¤¿¤µ¤ì¤¿¸å¤Î½èÍý Debug.Log("¾ò·ï¤¬Ëþ¤¿¤µ¤ì¤Þ¤·¤¿¡ª"); } void Start() { StartCoroutine(WaitForCondition()); // ¤¢¤ë¾ò·ï¤¬Ëþ¤¿¤µ¤ì¤¿¤È²¾Äê StartCoroutine(SimulateConditionMet()); } IEnumerator SimulateConditionMet() { yield return new WaitForSeconds(3.0f); // 3Éøå¤Ë¾ò·ï¤òËþ¤¿¤¹ conditionMet = true; } }
¡¡¤³¤Î¥³¡¼¥É¤Ç¤Ï¡¢WaitForCondition ¥³¥ë¡¼¥Á¥ó¤¬ conditionMet ¥Õ¥é¥°¤¬ true ¤Ë¤Ê¤ë¤Þ¤ÇÂÔµ¡¤·¤Þ¤¹¡£
ÂÔµ¡Ãæ¤Ï yield return null ¤ò»ÈÍѤ·¤Æ¡¢¼¡¤Î¥Õ¥ì¡¼¥à¤Þ¤Ç¥³¥ë¡¼¥Á¥ó¤òÃæÃǤ·¤Þ¤¹¡£
¡¡SimulateConditionMet ¥³¥ë¡¼¥Á¥ó¤Ï3Éøå¤Ë conditionMet ¥Õ¥é¥°¤ò true ¤ËÀßÄꤷ¡¢ÂÔµ¡¤ò²ò½ü¤·¤Þ¤¹¡£
¡¡Ê£¿ô¤Î yield ¥¹¥Æ¡¼¥È¥á¥ó¥È¤òÍøÍѤ¹¤ë¤³¤È¤Ë¤è¤ê¡¢¼«Ê¬¼«¿È¤Î¾ò·ï¤Ë¹ç¤ï¤»¤¿¥«¥¹¥¿¥à¤ÎÂÔµ¡¥ë¡¼¥×¤òºîÀ®¤Ç¤¤Þ¤¹¡£
ɬÍפ˱þ¤¸¤Æ¡¢Â¾¤Î¾ò·ï¤ò´Æ»ë¤·¡¢ÆÃÄê¤Î¾ò·ï¤¬Ëþ¤¿¤µ¤ì¤¿¤é¥³¥ë¡¼¥Á¥ó¤ò³¹Ô¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¡¡¤Ê¤ª¡¢while ʸÆâ¤Ë yield ¥¹¥Æ¡¼¥È¥á¥ó¥È¤Î½èÍý¤òÆþ¤ì¤Ê¤¤¾ì¹ç¡¢Ìµ¸Â¥ë¡¼¥×¤¬È¯À¸¤·¤Æ¥¨¥é¡¼¤Ë¤Ê¤ê¤Þ¤¹¡£
¤³¤ì¤Ï¥³¥ë¡¼¥Á¥ó¤ÎÀ©¸æ¥Õ¥í¡¼¤¬Àµ¤·¤¯Æ°ºî¤·¤Ê¤¤¤¿¤á¤Ç¤¹¡£
¡¡¥³¥ë¡¼¥Á¥óÆâ¤Ç while ʸ¤òÍѤ¤¤ë¾ì¹ç¡¢yield¥¹¥Æ¡¼¥È¥á¥ó¥È¤ò»È¤ï¤Ê¤¤¤È¡¢¤½¤Î¥³¥ë¡¼¥Á¥ó¤¬´°Î»¤¹¤ë¤³¤È¤Ê¤¯¡¢Ìµ¸Â¤ËƱ¤¸¥Õ¥ì¡¼¥àÆâ¤Ç½èÍý¤¬¥ë¡¼¥×¤·Â³¤±¤Þ¤¹¡£
¤½¤Î¤¿¤á¡¢¥²¡¼¥à¤¬Ää»ß¤·¤Æ¤·¤Þ¤¤¤Þ¤¹¤Î¤Ç¡¢ÍøÍѤ¹¤ë¾ì¹ç¤Ë¤ÏÃí°Õ¤·¤Æ¤¯¤À¤µ¤¤¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- ¥×¥í¥°¥é¥ß¥ó¥°
¥³¥á¥ó¥È¤ò¤«¤¯