ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2020ǯ11·î07Æü(ÅÚ) 16:10:56ÍúÎò
¥Ï¥¤¥¹¥³¥¢¤Î¹¹¿·¤òÌܻؤ·¤Æ¥¯¥ê¥¢¤·¤Æ¤¤¤¯¤è¤¦¤Ë¤·¤Þ¤¹¡£
Main¥·¡¼¥ó¤ò³«¤¤Þ¤¹¡£
¥Ò¥¨¥é¥ë¥¡¼Æâ¤Ë¤Æcreate -> Create Empty¤òÁªÂò¤·¡¢¶õ¤Î¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤òºîÀ®¤·¤Þ¤¹¡£
̾Á°¤ò¡ÖStageManager¡×¤ËÊѹ¹¤·¤Þ¤¹¡£
¥Ò¥¨¥é¥ë¥¡¼¤Î¡ÖScoreCanavas¡×Æâ¤Ë¤Æ create -> UI -> Text ¤òÁªÂò¤·¤Æ¿·¤·¤¤Text¥ª¥Ö¥¸¥§¥¯¥È¤òºîÀ®¤·¤Þ¤¹¡£
̾Á°¤ò¡ÖLevelText¡×¤ËÊѹ¹¤·¤Þ¤¹¡£
¡ÖLevelText¡×¤ò¥À¥Ö¥ë¥¯¥ê¥Ã¥¯¤·¤ÆÇÛÃÖ¤µ¤ì¤¿°ÌÃ֤ޤǰÜÆ°¤·¡¢°ÌÃÖ¤ò¥²¡¼¥à¥·¡¼¥ó¾å¤ÇÄ´À°¤·¤Þ¤¹¡£
¤½¤Î¸å¡ÖLevelText¡×¤òDuplicate¡ÊÊ£À½¡Ë¤·¤Æ̾Á°¤ò¡ÖLevelLabel¡×¤ËÊѹ¹¤·¤Þ¤¹¡£
TextÆâ¤Ë¡ÖLevel : ¡×¤È½ñ¤¹þ¤ó¤Ç¡¢°ÌÃÖ¤òLevelText¤Î°ÌÃ֤˹ç¤ï¤»¤ÆÄ´À°¤ò¤·¤Þ¤¹¡£
¤¤¤º¤ì¤âAnchorµ¡Ç½¤òÍøÍѤ·¤Æ²èÌÌ¥µ¥¤¥º¤Ê¤É¤Ë¤è¤Ã¤Æ¥ì¥¤¥¢¥¦¥È¤¬Êѹ¹¤Ë¤Ê¤Ã¤¿¤È¤¤Ë¤â¡¢Àµ¾ï¤Ê°ÌÃÖ¤Ëɽ¼¨¤µ¤ì¤ë¤è¤¦¤Ë¤·¤Þ¤¹¡£
Assets -> Script¥Õ¥©¥ë¥ÀÆâ¤Ç±¦¥¯¥ê¥Ã¥¯¤ò¤·¤Æ create -> C# scrpt¤òÁªÂò¤·¤Þ¤¹¡£
̾Á°¤ò¡ÖLevelManager¡×¤ËÊѹ¹¤·¤Þ¤¹¡£
²¼µ¤Î¥½¡¼¥¹¥³¡¼¥É¤ò½ñ¤¹þ¤ß¤Þ¤¹¡£
LevelManager.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class LevelManager : MonoBehaviour { [SerializeField] Text levelText; // ¥ì¥Ù¥ë¤Îɽ¼¨ÍÑ¡£¥¤¥ó¥¹¥Ú¥¯¥¿¤Ç»ØÄê²Äǽ [SerializeField] StartShot startShot; // ¥Ü¡¼¥ë¤ÎÀ©¸æÍÑ public static int level; // ¥¹¥Æ¡¼¥¸¤ò¥¯¥ê¥¢¤·¤¿²ó¿ô¡£¥·¡¼¥ó¤ò¤Þ¤¿¤¤¤Ç¤â°ú¤·Ñ¤°¤è¤¦¤Ëpublic ¤«¤Ä static¤Ë¤¹¤ë ¡¡¡¡ public static bool isStart; // ¥²¡¼¥à³«»Ï¤«¤É¤¦¤«¤òȽÃǤ¹¤ë¥Õ¥é¥°¡£false¤Ê¤é³«»ÏÁ°¡£true¤Ê¤é¥²¡¼¥àÃæ¤ÈȽÃǤ¹¤ë¡¡¡¡ void Start () { ¡¡¡¡ ¡¡ if(!isStart) // isStart¤Î¥Õ¥é¥°¤ò³Îǧ¤·¡¢False¤Ç¤¢¤ì¤Ð¼Â¹Ô¤µ¤ì¤ë¡£¡Ö!isStart¡×¤Ï¡ÖisStart == false¡×¤ÈƱµÁ¡£ { level = 0; // ¥ì¥Ù¥ë¤ò0¤Ë¤¹¤ë isStart = true; // ¥²¡¼¥à³«»Ï¤Î¥Õ¥é¥°¤òΩ¤Æ¤ë¡£¥¿¥¤¥È¥ë²èÌ̤ØÌá¤ë¤È¥ê¥»¥Ã¥È¤µ¤ì¤ë¤è¤¦¤ËResetFlags.cs¤Ç´ÉÍý¤¹¤ë } levelText.text = level.ToString();¡¡¡¡¡¡ // level¤òLevelText¤Ëɽ¼¨ } public void LevelUp() // ³°Éô¡ÊGameMaster.cs¡Ë¤è¤ê¸Æ¤Ð¤ì¤ë¥á¥½¥Ã¥É { startShot.BallDestroy();¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ // ¥Ü¡¼¥ë¤òÀ©¸æ¤¹¤ë¥á¥½¥Ã¥É¤Î¸Æ¤Ó½Ð¤· level ++ ; // level¤ò²Ã»»¤¹¤ë StartCoroutine("NextLevel"); // ¥³¥ë¡¼¥Á¥ó¤Î¸Æ½Ð¤· } IEnumerator NextLevel() // ¥³¥ë¡¼¥Á¥ó { Debug.Log("Please press key"); ¡¡¡¡ while(!Input.GetKey("space")) yield return null;¡¡¡¡¡¡//¡¡¥¹¥Ú¡¼¥¹¥¡¼¤ÎÆþÎÏÂÔ¤Á SceneManager.LoadScene("Main"); // ¥¹¥Ú¡¼¥¹¥¡¼¤¬²¡¤µ¤ì¤¿¤éMain¥·¡¼¥ó¤ò¸Æ¤Ó½Ð¤¹ Debug.Log("Done!"); } }
¤³¤Î¥¹¥¯¥ê¥×¥È¤òMaster¥ª¥Ö¥¸¥§¥¯¥È¤Ë¥¢¥¿¥Ã¥Á¤·¤Þ¤¹¡£
Master¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Î¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤ò³Îǧ¤·¤Æ¤¯¤À¤µ¤¤¡£
LevelManager ¥¹¥¯¥ê¥×¥È¤Ë¡¢public ½¤¾þ»Ò¤ÇÀë¸À¤·¤¿ÊÑ¿ô¤Î¥¢¥µ¥¤¥ó¾ðÊó¤¬É½¼¨¤µ¤ì¤Þ¤¹¤Î¤Ç¡¢¤½¤Á¤é¤òÅÐÏ¿¤·¤Þ¤¹¡£
¥¢¥µ¥¤¥ó¤Ï¡¢¥Ò¥¨¥é¥ë¥¡¼¤Ë¤¢¤ë¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤ò¥É¥é¥Ã¥°¥¢¥ó¥É¥É¥í¥Ã¥×¤¹¤ë¤³¤È¤Ç¡¢
¤½¤Î¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¡¢¤¢¤ë¤¤¤Ï¤½¤Î¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤¬»ý¤Ã¤Æ¤¤¤ë¥³¥ó¥Ý¡¼¥Í¥ó¥È¤ä¥¹¥¯¥ê¥×¥È¤ò¾ðÊó¤È¤·¤ÆÊÑ¿ô¤ËÂåÆþ¤¹¤ë¤³¤È¤ò¸À¤¤¤Þ¤¹¡£
LevelText ÊÑ¿ô¤Ë¤Ï¡¢Àè¤Û¤ÉºîÀ®¤·¤¿ LevelText ¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤ò¥É¥é¥Ã¥°¥¢¥ó¥É¥É¥í¥Ã¥×¤·¤Æ¥¢¥µ¥¤¥ó¤·¤Æ¤¯¤À¤µ¤¤¡£
¼«Æ°Åª¤Ë¡¢LevelText ¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Î»ý¤Ä¡¢Text¥³¥ó¥Ý¡¼¥Í¥ó¥È¤Î¾ðÊ󤬡¢¤³¤ÎÊÑ¿ô¤ËÂåÆþ¤µ¤ì¤Þ¤¹¡£
¤³¤ì¤Ç¡¢LevelText ÊÑ¿ô¤òÄ̤¸¤Æ¡¢¥²¡¼¥à²èÌ̾å¤Ë Level ¤ÎÃͤòɽ¼¨¤¹¤ë½àÈ÷¤¬À°¤¤¤Þ¤·¤¿¡£
StartShot ÊÑ¿ô¤Ë¤Ï¡¢StartShot¥¹¥¯¥ê¥×¥È¤¬¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤ò¡¢¤³¤³¤Ë¥¢¥µ¥¤¥ó¤¹¤ë¤³¤È¤Ç¾ðÊó¤òÅÐÏ¿¤Ç¤¤Þ¤¹¡£
¥Ò¥¨¥é¥ë¥¡¼¤Ë¤¢¤ë¡¢Ball ¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Ë StartShot ¥¹¥¯¥ê¥×¥È¤¬¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤Þ¤¹¤Î¤Ç
Ball ¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤ò¥É¥é¥Ã¥°¥¢¥ó¥É¥É¥í¥Ã¥×¤·¤Æ¡¢¤³¤³¤Ë¥¢¥µ¥¤¥ó¤·¤Æ¤¯¤À¤µ¤¤¡£
¤³¤ì¤Ç StartShot ÊÑ¿ô¤òÄ̤¸¤Æ¡¢Ball ¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Î StartShot ¥¹¥¯¥ê¥×¥È¤òÁàºî¤Ç¤¤ë¤è¤¦¤Ë¤Ê¤ê¤Þ¤·¤¿¡£
Main¥·¡¼¥ó¤Ë¤Æ¥²¡¼¥à¤ò¥¯¥ê¥¢¤·¤¿¾ì¹ç¡¢¥ê¥¶¥ë¥È¥·¡¼¥ó¤Ë¤ÏÁ«°Ü¤»¤º¤ËMain¥·¡¼¥ó¤òºÆÅÙÆɤ߹þ¤ß
Level¤ò1¤Ä²Ã»»¤·¤Æ¥¹¥Æ¡¼¥¸¤¬ºÆÅٻϤޤë¤è¤¦¤Ë¤·¤Þ¤¹¡£
Result¥·¡¼¥ó¤ÇºîÀ®¤·¤¿¥ê¥¶¥ë¥Èɽ¼¨ÍѤÎCanvas¤òMain¥·¡¼¥ó¤Ë¤âÄɲä·¤Þ¤¹¡£
Result¥·¡¼¥ó¤ÎCanvas¤ò¥³¥Ô¡¼¡Ê¤¢¤ë¤¤¤Ï¥×¥ì¥Õ¥¡¥Ö²½¡Ë¤·¤Æ¡¢Main¥·¡¼¥ó¤ËÄɲä·¤Þ¤¹¡£
̾Á°¤òResultCanvas¤ËÊѹ¹¤·¤Þ¤¹¡£
ResultText¤ÎText¤«¤é¤ÏGameOver¤Îʸ»ú¤òºï½ü¤·¤Æ¡¢¶õÇò¤Î¾õÂ֤ˤ·¤Æ¤ª¤¤¤Æ¤¯¤À¤µ¤¤¡£
¤³¤³¤Ë¤Ï¥¹¥Æ¡¼¥¸¥¯¥ê¥¢»þ¤Ëʸ»ú¤¬É½¼¨¤µ¤ì¤ë¤è¤¦¤Ë¤·¤Þ¤¹¡£
GameMaster.cs¤òÊѹ¹¤·¤Þ¤¹¡£
using System.Collections; using System.Collections.Generic; using UnityEngine;¡¡¡¡¡¡¡¡¡¡ using UnityEngine.SceneManagement; using UnityEngine.UI;¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡// <= Äɲ乤ë public class GameMaster : MonoBehaviour { public int boxNum; public float nowTime; ¡¡¡¡[SerializeField] Text resultMessageText; // <- Äɲ乤롣¥¯¥ê¥¢¤·¤¿ºÝ¤Ë¡Ö¥¯¥ê¥¢¡×¤Èɽ¼¨¤¹¤ë¥Æ¥¥¹¥È ¡¡¡¡private bool isClear; // <- Äɲ乤롣¥¯¥ê¥¢¤·¤¿¾õÂ֤ˤ¹¤ë¥Õ¥é¥° ¡¡¡¡ // Use this for initialization void Start () { nowTime = 0; } // Update is called once per frame void Update () { nowTime += Time.deltaTime; if(!isClear){ // <= ¤³¤³¤«¤é { if (boxNum <= 0) { StageClear(nowTime.ToString("F0") + "Éäǥ¯¥ê¥¢¤Ç¤¤¿¡ª"); //<=====Éÿô¤òstring·¿¤Ë¥¥ã¥¹¥È¤·¤Æ°ú¿ô¤Ø <= ¤³¤³¤Î¸Æ¤Ó½Ð¤¹¥á¥½¥Ã¥É¤òÊѹ¹¤¹¤ë isClear = true; } }¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡// <= ¤³¤³¤Þ¤Ç¤òÄɲ乤ë } void StageClear(string resultMessage) // <= ¤³¤³¤«¤é { resultMessageText.text = resultMessage;¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ // ²èÌ̤˥¯¥ê¥¢¾õÂÖ¤òɽ¼¨¤¹¤ë(ResultText¤ÎText¤Ï¶õÇò¤Ë¤·¤Æ¤ª¤¯)¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ FindObjectOfType<LevelManager>().LevelUp(); // LevelManager¤ÎLevelUp¥á¥½¥Ã¥É¤ò¸Æ¤Ó½Ð¤¹ } // <= ¤³¤³¤Þ¤Ç¤òÄɲ乤ë public void GameOver(string resultMessage) { // °ú¿ô¤ò¤â¤¿¤»¤¿ DataSender.resultMessage = resultMessage; // ¼õ¤±¼è¤Ã¤¿°ú¿ô¤òstaticÊÑ¿ô¤Ø³ÊǼ SceneManager.LoadScene("Result"); } }
¥²¡¼¥à¥¯¥ê¥¢¤ÎȽÄê¤À¤±¤Ç¤Ï¡¢¥²¡¼¥àÆâ¤Î¥Ü¡¼¥ë¤ÏÆ°¤¤¤¿¤Þ¤Þ¤Ç¤¹¡£
¤½¤Î¤¿¤á¡¢¥²¡¼¥à¥¯¥ê¥¢¤ÈƱ»þ¤Ë¡¢¥Ü¡¼¥ë¤òÈó¥¢¥¯¥Æ¥£¥Ö¡Ê¤¢¤ë¤¤¤ÏÇ˲õ¡Ë¤¹¤ë¤è¤¦¤Ë¤·¤Þ¤¹¡£
StartShot.cs¤ò½¤Àµ¤·¤Þ¤¹¡£
using System.Collections; using System.Collections.Generic; using UnityEngine; public class StartShot : MonoBehaviour { // Use this for initialization void Start () { transform.eulerAngles = new Vector3(0, Random.Range(30, 120), 0); gameObject.GetComponent<Rigidbody>().AddForce(transform.forward * 500); } // Update is called once per frame void Update () { } public void BallDestroy() // <= ¤³¤³¤«¤é { this.gameObject.SetActive (false); // <= ¤É¤Á¤é¤«¤ò½ñ¤¯¡£¾å¤Ï¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë¥ª¥Ö¥¸¥§¥¯¥È¤òÈó¥¢¥¯¥Æ¥£¥Ö¡£²¼¤Ï¤³¤Î¥ª¥Ö¥¸¥§¥¯¥È¤òÇ˲õ¤¹¤ë¡£ Destroy(this.gameObject); } // <= ¤³¤³¤Þ¤Ç¤òÄɲ乤ë }
LevelManagerÆâ¤Ë¤Ï¡¢public static¤ÎÊÑ¿ô¤¬£²¤Ä¤¢¤ê¤Þ¤¹¡£
¤³¤ì¤é¤Ï¥·¡¼¥ó¤òÁ«°Ü¤·¤Æ¤â¤½¤ÎÃæ¿È¤¬ÊÝ»ý¤µ¤ì¤Þ¤¹¤¬¡¢¤½¤ì¤Ï¥¿¥¤¥È¥ë²èÌ̤ʤɤËÌá¤Ã¤Æ¤âÊÝ»ý¤µ¤ì¤¿¤Þ¤Þ¤Ç¤¹¡£
¤½¤³¤Ç¥¿¥¤¥È¥ë²èÌ̤è¤ê¿·¤·¤¯¥²¡¼¥à¤ò³«»Ï¤¹¤ë¤È¡¢isStart¤Î¥Õ¥é¥°¤¬¹ß¤ê¤ë¤è¤¦¤Ë¡¢Title¥·¡¼¥ó¤Ë¿·¤·¤¤¥¹¥¯¥ê¥×¥È¤òÄɲä·¤Þ¤¹¡£
Scripts ¥Õ¥©¥ë¥À¤ÎÃæ¤Ç±¦¥¯¥ê¥Ã¥¯¤ò¤·¤Æ Create -> C# Script ¤òÁªÂò¤·¡¢Ì¾Á°¤ò¡ÖResetFlags¡×¤ÈÊѹ¹¤·¤Þ¤¹¡£
ResetFlags.cs¤ò²¼µ¤Î¤è¤¦¤Ë½ñ¤¹þ¤ß¤Þ¤¹¡£
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ResetFlags : MonoBehaviour { // Use this for initialization void Start () { LevelManager.isStart = false; // isStart¤Î¥Õ¥é¥°¤òfalse¤Ë¤¹¤ë¡£public static¤Ê¤Î¤Ç¤É¤³¤«¤é¤Ç¤â»²¾È¤·¤ÆÊѹ¹¤Ç¤¤ë¡£ } }
¤³¤Î¥¹¥¯¥ê¥×¥È¤¬Í¸ú¤Ë¤Ê¤ë¤è¤¦¤Ë¡¢Title¥·¡¼¥ó¤Ë¤¢¤ëMaster¥ª¥Ö¥¸¥§¥¯¥È¤Ë¥¢¥¿¥Ã¥Á¤·¤Þ¤¹¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- Áí¹ç
¥³¥á¥ó¥È¤ò¤«¤¯