ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2020ǯ10·î21Æü(¿å) 15:34:39ÍúÎò
¡¡²èÌ̤˥¹¥³¥¢¤È¥Ï¥¤¥¹¥³¥¢¤òɽ¼¨¤µ¤»¤ë¤è¤¦¤Ë¤·¤Þ¤¹¡£
¡¡
¡¡Canvas¤òÍÑ°Õ¤·¤Æ¡¢¤½¤Á¤é¤Ë¥¹¥³¥¢¤È¥Ï¥¤¥¹¥³¥¢¤òɽ¼¨¤µ¤»¤ëText¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤òÍÑ°Õ¤·¤Þ¤¹¡£
¡¡Score¥¹¥¯¥ê¥×¥È¤òºîÀ®¤·¡¢¤³¤ÎÃæ¤Ç¥¹¥³¥¢¤È¥Ï¥¤¥¹¥³¥¢¤òÊÑ¿ô¤Ç´ÉÍý¤·¤Þ¤¹¡£¤³¤ÎÃͤòCanvas¤ÎText¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤ËÁ÷¤Ã¤Æ¡¢²èÌ̤Ëɽ¼¨¤·¤Æ¤â¤é¤¤¤Þ¤¹¡£
¡¡¥¹¥³¥¢¤Ï¥Ö¥í¥Ã¥¯¤òÇ˲õ¤¹¤ë¤È²Ã»»¤µ¤»¤ë¤è¤¦¤Ë¤·¤¿¤¤¤Î¤Ç¡¢Destroyer¥¹¥¯¥ê¥×¥È¤ò½¤Àµ¤·¡¢Ç˲õ¤µ¤ì¤ë¤È¤¤Ë¥¹¥³¥¢¤ØÅÀ¿ô¤òÁ÷¤ë¤è¤¦¤Ë¤·¤Þ¤¹¡£
¡¡¼ÂÁõ¤Î¼ê½ç¤ò¤Þ¤È¤á¤Þ¤¹¡£
£±¡¥¥Ò¥¨¥é¥ë¥¡¼Æâ¤ËUI¤òÄɲä·¤Þ¤¹¡£¤½¤ÎUI¤òÍøÍѤ·¤Æ¥¹¥³¥¢¤È¥Ï¥¤¥¹¥³¥¢¤òɽ¼¨¤·¤Þ¤¹¡£ £²¡¥Score¥¹¥¯¥ê¥×¥È¤òºîÀ®¤·¤Þ¤¹¡£ £³¡¥Destroyer¥¹¥¯¥ê¥×¥È¤ò½¤Àµ¤·¤Þ¤¹¡£ £´¡¥¥²¡¼¥à¤ò¼Â¹Ô¤·¤Æ¡¢ÅÀ¿ô¤¬²Ã»»¤µ¤ì¡¢²èÌÌɽ¼¨¤¬¹¹¿·¤µ¤ì¤ë¤«³Îǧ¤·¤Þ¤¹¡£
¡¡¥Ò¥¨¥é¥ë¥¡¼¤Î create -> UI -> Text ¤òÁªÂò¤·¤Æ¿·¤·¤¤Text¥ª¥Ö¥¸¥§¥¯¥È¤òºîÀ®¤·¤Þ¤¹¡£Ì¾Á°¤ò¡ÖScoreText¡×¤ËÊѹ¹¤·¤Þ¤¹¡£
¡ÖScoreText¡×¤ò¥À¥Ö¥ë¥¯¥ê¥Ã¥¯¤·¤ÆÇÛÃÖ¤µ¤ì¤¿°ÌÃ֤ޤǰÜÆ°¤·¡¢¥¢¥ó¥«¡¼¤òÀßÄê¸å¡¢°ÌÃÖ¤ò¥²¡¼¥à¥·¡¼¥ó¾å¤ÇÄ´À°¤·¤Þ¤¹¡£
ScoreText¤ò¡ÖDuplicate¡ÊÊ£À½¡Ë¡×¤·¤Æ¡¢Ì¾Á°¤ò¡ÖScoreLabel¡×¤ËÊѹ¹¤·¤Þ¤¹¡£Text¤ÎÃæ¤Ë¡ÖScore : ¡×¤È½ñ¤¹þ¤ß¤Þ¤¹¡£
¡¡¿·¤·¤¤Canvas¤òºî¤ê¡ÊºîÀ®¤µ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¡Ë¡¢Ì¾Á°¤ò¡ÖScoreCanvas¡×¤ËÊѹ¹¤·¤Þ¤¹¡£
¡¡¥Ò¥¨¥é¥ë¥¡¼Æâ¤ÎScoreText¤ÈScoreLabel¾å¤Ç±¦¥¯¥ê¥Ã¥¯¤ò¤·¤Æ¡ÖDuplicate¡ÊÊ£À½¡Ë¡×¤òÁªÂò¤·¤Æ¡¢
̾Á°¤ò¤½¤ì¤¾¤ì¡ÖHighScoreText¡×¤È¡ÖHighScoreLabel¡×¤ËÊѹ¹¤·¤Þ¤¹¡£
¡ÖHighScoreLabel¡×¤ÎText¤ÎÃæ¤Ë¤Ï¡ÖHighScore : ¡×¤È½ñ¤¹þ¤ß¤Þ¤¹¡£
¡ÖScoreText¡×¤ÈƱ¤¸¤è¤¦¤Ë¤½¤ì¤¾¤ì¤Î°ÌÃÖ¤òÄ´À°¤·¤Þ¤¹¡£
¡¡ScoreText¤ÈHighScoreText¤Ï¡¢¤É¤Á¤é¤â¥Ò¥¨¥é¥ë¥¡¼¤ÎScoreCanvas°Ê²¼¤ËÇÛÃÖ¤·¤Æ¤¯¤À¤µ¤¤¡£
ScoreCanvas
¡¡¡¡ScoreText
¡¡¡¡ScoreLabel
¡¡¡¡HighScoreText
¡¡¡¡HighScoreLabel
¤Î¤è¤¦¤Ê¾õÂ֤Ǥ¹¡£
¡¡¥¹¥³¥¢¤Ê¤É¤Îɽ¼¨°ÌÃÖ¤ÏǤ°Õ¤Ç¤¹¡£¥²¡¼¥à²èÌ̤˱£¤ì¤º¤Ë¥æ¡¼¥¶¡¼¤Î¸«¤¨¤ë¤è¤¦¤ËÇÛÃÖ¤·¤Æ¤¯¤À¤µ¤¤¡£
»²¹ÍÎã
Game¥Ó¥å¡¼
Scene¥Ó¥å¡¼
¡¡Assets -> Script¥Õ¥©¥ë¥ÀÆâ¤Ç±¦¥¯¥ê¥Ã¥¯¤ò¤·¤Æ create -> C# scrpt¤òÁªÂò¤·¤Þ¤¹¡£
̾Á°¤ò¡ÖScore¡×¤ËÊѹ¹¤·¤Þ¤¹¡£
²¼µ¤Î¥½¡¼¥¹¥³¡¼¥É¤ò½ñ¤¹þ¤ß¤Þ¤¹¡£
score.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Score : MonoBehaviour { // ¥¹¥³¥¢¤òɽ¼¨¤¹¤ëUI¤Î¼èÆÀÍÑ public Text scoreText; // ¥Ï¥¤¥¹¥³¥¢¤òɽ¼¨¤¹¤ëUI¤Î¼èÆÀÍÑ public Text highScoreText; // ¥¹¥³¥¢¤Î¥«¥¦¥ó¥ÈÍÑ private int score; // ¥Ï¥¤¥¹¥³¥¢¤Î¥«¥¦¥ó¥ÈÍÑ private int highScore; // PlayerPrefs¤ÇÊݸ¤¹¤ë¤¿¤á¤Î¥¡¼ private string highScoreKey = "highScore"; void Start () { Initialize (); } // ¥²¡¼¥à³«»ÏÁ°¤Î¾õÂÖ¤ËÌ᤹ private void Initialize () { // ¥¹¥³¥¢¤ò0¤ËÌ᤹ score = 0; // highScoreKey¤ÎÃͤÇÊݸ¤µ¤ì¤Æ¤¤¤ë¥Ï¥¤¥¹¥³¥¢¤ò¸¡º÷¤·¤Æ¼èÆÀ¤¹¤ë¡£Êݸ¤µ¤ì¤Æ¤Ê¤±¤ì¤Ð0¤ò¼èÆÀ¤¹¤ë¡£ highScore = PlayerPrefs.GetInt (highScoreKey, 0); } // ¥Ý¥¤¥ó¥È¤ÎÄɲ᣽¤¾þ»Ò¤òpublic¤Ë¤·¤Æ¤¤¤ë¤Î¤Ç³°Éô¤è¤ê»²¾È¤Ç¤¤ë¥á¥½¥Ã¥É¤Ë¤Ê¤Ã¤Æ¤¤¤ë public void AddPoint (int point)¡¡¡¡¡¡¡¡¡¡¡¡//¡¡³°Éô¤è¤ê¼õ¤±¼è¤Ã¤¿int·¿¤Î°ú¿ô¤òpoint¤È¤·¤Æ¼õ¤±¼è¤ë { score += point; Debug.Log(score); // ¤³¤³¤Ç³Îǧ¤¹¤ë¤È¡¢¥Ö¥í¥Ã¥¯¤«¤é¤Î¾ðÊó¤¬ÆϤ¤¤Æ¤¤¤ë¤«³Îǧ¤Ç¤¤ë¡£É½¼¨¤µ¤ì¤Ê¤±¤ì¤ÐÆϤ¤¤Æ¤¤¤Ê¤¤¤È¤¤¤¦¤³¤È¤Ë¤Ê¤ë¡£ // ¥¹¥³¥¢¤¬¥Ï¥¤¥¹¥³¥¢¤è¤êÂ礤¯¤Ê¤ì¤Ð¡¢¥Ï¥¤¥¹¥³¥¢¤ò¹¹¿·¤¹¤ë if (highScore < score) { highScore = score; Debug.Log(highScore); // ¥Ï¥¤¥¹¥³¥¢¤Î¹¹¿·¤¬Àµ¾ï¤Ë¹Ô¤ï¤ì¤¿¤«³Îǧ¤Ç¤¤ë } // ¥²¡¼¥à²èÌ̾å¤Î¥¹¥³¥¢¤È¥Ï¥¤¥¹¥³¥¢¤Îɽ¼¨¤ò¹¹¿·¤¹¤ë DisplayScores(); } // ¥²¡¼¥à²èÌ̾å¤Î¥¹¥³¥¢¤È¥Ï¥¤¥¹¥³¥¢¤Îɽ¼¨¤ò¹¹¿·¤¹¤ë ¡¡¡¡private void DisplayScores(){ // ¸½ºß¤Î¥¹¥³¥¢¤È¥Ï¥¤¥¹¥³¥¢¤ò²èÌ̤Ëɽ¼¨¤¹¤ë scoreText.text = score.ToString (); highScoreText.text = highScore.ToString (); } // ¥Ï¥¤¥¹¥³¥¢¤ÎÊݸ public void Save () { // ¥Ï¥¤¥¹¥³¥¢¤òÊݸ¤¹¤ë PlayerPrefs.SetInt (highScoreKey, highScore); PlayerPrefs.Save (); // ¥²¡¼¥à³«»ÏÁ°¤Î¾õÂÖ¤ËÌ᤹ Initialize (); } }
PlayerPrefs¤Ï¡¢¥Ç¡¼¥¿¤Î¥»¡¼¥Ö¡¦¥í¡¼¥É¤ò¹Ô¤¦¡¢Unity¤ÎÍÑ°Õ¤·¤Æ¤¤¤ë¥¯¥é¥¹¤Ç¤¹¡£
¸ø¼°¥ê¥Õ¥¡¥ì¥ó¥¹
https://docs.unity3d.com/jp/current/ScriptReferenc...
¸½ºß¤Ï¤Þ¤ÀÆ°ºî¤Ï¤·¤Æ¤¤¤Þ¤»¤ó¤¬¡¢¤³¤³¤Ç¤Ï£³¤Ä¤Î»È¤¤Êý¤ò¤·¤Æ¤¤¤Þ¤¹¡£
¤Þ¤º¡¢¥Ç¡¼¥¿¤Î¥»¡¼¥Ö¡¦¥í¡¼¥ÉÍѤΥ¡¼¤òÀßÄꤷ¤Þ¤¹¡£¥¡¼¤ÎÀßÄê¤Ïstring·¿¤ÎÊÑ¿ô¤ò»ÈÍѤ·¤Þ¤¹¡£
private string highScoreKey = "highScore";
¤³¤³¤ÇÀßÄê¤ò¤·¤Æ¤¤¤Þ¤¹¡£
highScore = PlayerPrefs.GetInt (highScoreKey, 0);
¤³¤³¤Ç¤Ï¡¢highScoreKey¤ËInt·¿¤ÎÊÑ¿ô¤Ë¥Ç¡¼¥¿¤¬¥»¡¼¥Ö¤µ¤ì¤Æ¤¤¤ë¤«¤É¤¦¤«¤ò³Îǧ¤·¡¢¥»¡¼¥Ö¤µ¤ì¤Æ¤¤¤ì¤Ð¤½¤Î¿ôÃͤò¼è¤ê½Ð¤·¤ÆhighScore¤ØÂåÆþ¤·¤Æ¤¤¤Þ¤¹¡£¤Ê¤±¤ì¤Ð0¤òÂåÂåÆþ¤·¤Æ¤¤¤Þ¤¹¡£
PlayerPrefs.SetInt (highScoreKey, highScore);
¤³¤³¤Ç¤Ï¡¢highScoreKey¤ËhighScore¤Î¿ôÃͤò»²¾È¤·¤Æ¤¤¤Þ¤¹¡£¤³¤Î¾õÂÖ¤òºî¤ë¤³¤È¤Ç¡¢¼¡¤Î¥»¡¼¥Ö¤¬½ÐÍè¤Þ¤¹¡£
PlayerPrefs.Save ();
¥»¥Ã¥È¤·¤¿¿ôÃͤò¥»¡¼¥Ö¤ò¼Â¹Ô¤¹¤ë¥á¥½¥Ã¥É¤Ç¤¹¡£
¤³¤Á¤é¤Î¥µ¥¤¥È¤¬¾Ü¤·¤¤¤Ç¤¹¡£
https://freesworder.net/unity-playerprefs/
¡¡¤³¤ÎScore¥¹¥¯¥ê¥×¥È¤ò¥Ò¥¨¥é¥ë¥¡¼¤Î¡ÖScoreCanvas¡×¤Ø¥¢¥¿¥Ã¥Á¤·¤Þ¤¹¡£¡á¡¡ScoreCanvas¤ÇScore¤Î´ÉÍý¤ò¤µ¤»¤Þ¤¹¡£
ScoreCanvas¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Î¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤ò³Îǧ¤·¤Æ¤¯¤À¤µ¤¤¡£Score¥¹¥¯¥ê¥×¥È¤¬¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë¤È¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Ëɽ¼¨¤µ¤ì¤Þ¤¹¡£
¡¡public½¤¾þ»Ò¤ÇÀë¸À¤·¤¿ÊÑ¿ô¤¬£²¤Ä¤¢¤ê¤Þ¤¹¤Î¤Ç¡¢¥¢¥µ¥¤¥ó¾ðÊó¤òÅÐÏ¿½ÐÍè¤ë¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
¤½¤ì¤¾¤ìÊÑ¿ô̾¤ÈƱ̾¤Î¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤ò¥Ò¥¨¥é¥ë¥¡¼¤«¤é¥É¥é¥Ã¥°¥¢¥ó¥É¥É¥í¥Ã¥×¤·¤Æ¡¢Âбþ¤¹¤ë²Õ½ê¤Ë¥¢¥µ¥¤¥ó¤·¤Æ¤¯¤À¤µ¤¤¡£
¡¡Â³¤¤¤Æ¡¢Box¤ÎÊý¤Ë¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë¥¹¥¯¥ê¥×¥È¤Ë¤âÊѹ¹¤ò²Ã¤¨¤Þ¤¹¡£
¤³¤ì¤ÏBox¤¬Ç˲õ¤µ¤ì¤¿ºÝ¤ËÆÀÅÀ¤òScore¥¹¥¯¥ê¥×¥È¤Ë°ú¤ÅϤ¹¤è¤¦¤Ë¤¹¤ë½èÍý¤òÄɲä·¤Þ¤¹¡£
¡¡ÆÀÅÀÍѤÎÊÑ¿ô¤Ï³°Éô¤ÇÄ´À°¤Ç¤¤ë¤è¤¦¤Ë¡¢public½¤¾þ»Ò¤ÇÀë¸À¤·¤Æ¤¤¤Þ¤¹¡£
Destroyer.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Destroyer : MonoBehaviour { public int point; <- Äɲᣥ¤¥ó¥¹¥Ú¥¯¥¿¡¼¤«¤éÅÀ¿ô¤òÅÐÏ¿¤·¤Æ¤¯¤À¤µ¤¤ public GameObject masterObj; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnCollisionEnter(Collision collision) { //¥¹¥³¥¢½èÍý¤òÄɲà FindObjectOfType<Score>().AddPoint(point);¡¡¡¡<- Äɲà ¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡masterObj.GetComponent<GameMaster>().boxNum--; Destroy(this.gameObject); } }
¢¨¡¡FindObjectOfType<Object>()¥á¥½¥Ã¥É¤Ï¡¢¥·¡¼¥óÆâ¤Î<Object>¡Ê¤³¤³¤Ç¤Ï¡ãScore¡ä¥¹¥¯¥ê¥×¥È¡Ë¤ò»²¾È¤·¤Æ¡¢¤½¤Î¥¹¥¯¥ê¥×¥È¤ÎAddPoint¥á¥½¥Ã¥É¤Ë¥¢¥¯¥»¥¹¤¹¤ë¥á¥½¥Ã¥É¤Ç¤¹¡£
ÂоݤòÆÃÄê¤Ç¤¤ë¾ì¹ç¤Ë¤Ï¡ÖGetComponent¡×¡¢ÉÔÆÃÄê¤Ê狼¤ÎÃ椫¤é¾ðÊó¤ò»²¾È¤·¤Æ¡¢¤½¤Î¸Ä¿Í¾ðÊó¤Ë¥¢¥¯¥»¥¹¤¹¤ë¾ì¹ç¤Ë¤Ï¡ÖFindObjectOfType¡×¤òÍøÍѤ·¤Þ¤¹¡£
¡¡¿·¤·¤¯pointÊÑ¿ô¤òÄɲä·¡¢¤½¤ì¤òBox¤¬Ç˲õ¤µ¤ì¤¿ºÝ¤ËScore¥¹¥¯¥ê¥×¥È¤ÎAddPoint¥á¥½¥Ã¥É¤ØÅϤ¹¤è¤¦¤Ë¤·¤Æ¤¤¤Þ¤¹¡£
¡¡½¤Àµ¤¬¤ª¤ï¤ê¤Þ¤·¤¿¤é¡¢Box¤Î¥×¥ì¥Õ¥¡¥Ö¤òÁªÂò¤·¡¢Open Prefab¤òÁªÂò¤·¤Þ¤¹¡£
Destroyer¥¹¥¯¥ê¥×¥È¤Î¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Ë¿·¤·¤¯ point ÊÑ¿ô¤¬Äɲ䵤ì¤Æ¤¤¤Þ¤¹¤Î¤Ç¡¢ÅÀ¿ô¤òÅÐÏ¿¤·¤Æ¤¯¤À¤µ¤¤¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- Áí¹ç
¥³¥á¥ó¥È¤ò¤«¤¯