ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2022ǯ10·î19Æü(¿å) 16:47:33ÍúÎò
¡¡MonoBehaviour ¥¯¥é¥¹¤ËÍÑ°Õ¤µ¤ì¤Æ¤¤¤ë¥á¥½¥Ã¥É¤Ç¤¹¡£
¥³¥ó¥Ý¡¼¥Í¥ó¥È¤ò¥Ç¥Õ¥©¥ë¥È(½é´ü)Ãͤ˥ꥻ¥Ã¥È¤·¤Þ¤¹¡£
¡¡¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Î¥³¥ó¥Æ¥¥¹¥È¥á¥Ë¥å¡¼¤Î[Reset]¥Ü¥¿¥ó¤ò²¡¤·¤¿¤È¤¡¢¤Þ¤¿¤Ï¥³¥ó¥Ý¡¼¥Í¥ó¥È¤ò½é¤á¤ÆÄɲÃ(¥¢¥¿¥Ã¥Á´Þ¤à)¤·¤¿¤È¤¤Ë¸Æ¤Ó½Ð¤µ¤ì¤Þ¤¹¡£
[Reset]¥Ü¥¿¥ó(±¦Â¦¤Ë¤¢¤ë¡Ä¤Î¥á¥Ë¥å¡¼Æâ)
»²¹Í¥µ¥¤¥È
Unity ¸ø¼°¥¹¥¯¥ê¥×¥È¥ê¥Õ¥¡¥ì¥ó¥¹
MonoBehaviour.Reset
¡¡¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Ç Reset ¥Ü¥¿¥ó¤ò³èÍѤ¹¤ë¥±¡¼¥¹¤È¤·¤Æ¤Ï¡¢Transform ¤ÎÃͤò½é´üÃͤËÌ᤹ºÝ¤ËÍøÍѤµ¤ì¤Æ¤¤¤ë¤È»×¤¤¤Þ¤¹¤¬¡¢
¼«ºî¥¯¥é¥¹Æâ¤Ë Reset ¥á¥½¥Ã¥É¤òÍÑ°Õ¤¹¤ë¤³¤È¤Ç¡¢Transform °Ê³°¤ÎÃͤˤâ½é´üÃͤòÀßÄꤷ¡¢Reset ¥Ü¥¿¥ó¤ò²¡¤¹¤³¤È¤Ç¡¢¾ï¤Ë½é´üÃͤËÌ᤹µ¡Ç½¤ò¼ÂÁõ¤Ç¤¤Þ¤¹¡£
¡¡¤Þ¤¿¡¢¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Ë¸Â¤é¤º¡¢¥¯¥é¥¹Æâ¤Ç Reset ¥á¥½¥Ã¥É¤òǤ°Õ¤Î¥¿¥¤¥ß¥ó¥°¤Ç¼Â¹Ô¤¹¤ë¤³¤È¤â¤Ç¤¤Þ¤¹¡£
Î㤨¤Ð¡¢Start ¥á¥½¥Ã¥ÉÆâ¤Ç Reset ¥á¥½¥Ã¥É¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ç¡¢¥²¡¼¥à¼Â¹Ô¤Î¥¿¥¤¥ß¥ó¥°¤Ë¹ç¤ï¤»¤Æ½é´üÃͤËÌ᤹¤è¤¦¤ÊµóÆ°¤¬¼ÂÁõ½ÐÍè¤Þ¤¹¡£
»²¹Í¥µ¥¤¥È
Qiita @neusstudio(Unity¥²¡¼¥à³«È¯¼Ô¥®¥ë¥É) ÍÍ
¡ÚUnity¡ÛMonoBehaviour.Reset()¤Î»È¤¤Êý
¡¡Reset ¥á¥½¥Ã¥É¤ò¼ÂÁõ¤·¤¿¥¹¥¯¥ê¥×¥È¤òºîÀ®¤·¤Þ¤¹¡£
¡¡¤³¤ÎÊýË¡¤Ç¤¹¤È³ÆÊÑ¿ô¤Ï private ½¤¾þ»Ò¤Ç¤¢¤ë¤¿¤á¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Ë¤Ïɽ¼¨¤µ¤ì¤Ê¤¤¤¿¤á¡¢ÆâÉô¤Ç½èÍý¤¬Æ°¤¤¤Æ¤¤¤ë¤³¤È¤ò³Îǧ¤·¤Þ¤¹¡£
¡¡¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤ò Debug ¥â¡¼¥É¤ËÀÚ¤êÂؤ¨¤Æ¤ª¤¤Þ¤¹¡£
PlayerTest.cs
¡¡¥¹¥¯¥ê¥×¥È¤¬´°À®¤·¤¿¤é¥»¡¼¥Ö¤ò¹Ô¤¤¡¢Ç¤°Õ¤Î¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Ë¥¢¥¿¥Ã¥Á¤·¤Æ¤¯¤À¤µ¤¤¡£
¡¡RequireComponent °À¤Îµ¡Ç½¤Ë¤è¤ê¡¢Rigidbody ¥³¥ó¥Ý¡¼¥Í¥ó¥È¤¬¼«Æ°Åª¤Ë¥¢¥¿¥Ã¥Á¤µ¤ì¤Þ¤¹¡£
¡¡Reset ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤Æ¤ë¤³¤È¤Ë¤è¤ê¡¢hp ÊÑ¿ô¤Î½é´üÃͤȡ¢rb ÊÑ¿ô¤ËÂФ·¤Æ¤Î TryGetComponent ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤Æ rb ÊÑ¿ô¤ËÃͤ¬ÂåÆþ¤µ¤ì¤Þ¤¹¡£
¥¹¥¯¥ê¥×¥È¤ò¥¢¥¿¥Ã¥Á¤·¤Æ Reset ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤ë¤³¤È¤ò³Îǧ¤¹¤ë
Æ°²è¥Õ¥¡¥¤¥ë¤Ø¤Î¥ê¥ó¥¯
¥¤¥ó¥¹¥Ú¥¯¥¿¡¼²èÁü(Debug ¥â¡¼¥É)
¡¡Ìµ»ö¤Ë Reset ¥á¥½¥Ã¥É¤ÎµóÆ°¤¬³Îǧ¤Ç¤¤¿¤é¡¢¥¹¥¯¥ê¥×¥È¤ò²þÎɤ·¤Þ¤¹¡£
¡¡Â³¤¤¤Æ¡¢Æ±¤¸¥¹¥¯¥ê¥×¥È¤ò²þÎɤ·¤Þ¤¹¡£
¡¡³ÆÊÑ¿ô¤Ë SerializeField °À¤òÉÕÍ¿¤·¤Æ¤ª¤¤Þ¤¹¡£
¤³¤¦¤¹¤ë¤³¤È¤Ç¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤¬ Normal ¥â¡¼¥É¤Ç¤¢¤Ã¤Æ¤âÃͤÎÆ°¤¤â¤ï¤«¤ê¤Þ¤¹¤Î¤Ç¡¢
¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤ò Debug ¥â¡¼¥É¤«¤é Normal ¥â¡¼¥É¤ËÌᤷ¤Æ¤¯¤À¤µ¤¤¡£
¡¡¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤«¤éÀ©¸æ¤¹¤ë¤³¤È¤â¤Ç¤¤Þ¤¹¤Î¤Ç¡¢¥Ç¥Ð¥Ã¥°¤·¤ä¤¹¤¤´Ä¶¤Ë¤Ê¤ê¤Þ¤¹¡£
¡¡¤³¤Î²þÎÉÎã¤Ï¡¢Reset ¥á¥½¥Ã¥É¤ò³°Éô¥¯¥é¥¹¤«¤é¼Â¹Ô¤Ç¤¤ë¥á¥½¥Ã¥É¤ò½àÈ÷¤·¤Æ¤ª¤¯¤³¤È¤Ç¡¢
ºÇ½é¤Ë¥¹¥¯¥ê¥×¥È¤ò¥¢¥¿¥Ã¥Á¤·¤¿¤È¤°Ê³°¤Î¥¿¥¤¥ß¥ó¥°¤Ç¤Î¥ê¥»¥Ã¥Èµ¡Ç½¤ò͸ú¤Ë¤¹¤ëÊýË¡¤Î¼ÂÁõÎã¤Ç¤¹¡£
¡¡¤Þ¤¿¡¢¥á¥ó¥ÐÊÑ¿ô¤ÇÃͤòÂåÆþ¤·¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢³Æ·¿¤´¤È¤Î½é´üÃͤ¬ÂåÆþ¤µ¤ì¤Æ¤·¤Þ¤¤¤Þ¤¹¤Î¤Ç¡¢
¤½¤Î¾ì¹ç¤Ë¤Ï¡¢Reset ¥á¥½¥Ã¥ÉÆâ¤Ç½é´üÃͤòÂåÆþ¤¹¤ë¤è¤¦¤ËÍÑ°Õ¤·¤Æ¤ª¤¤Þ¤¹¡£
PlayerTest.cs
¡¡¥¹¥¯¥ê¥×¥È¤ò²þÎɤ·¤¿¤é¥»¡¼¥Ö¤·¤Þ¤¹¡£
¡¡¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Ë¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë PlayerTest ¥¹¥¯¥ê¥×¥È¤È Rigidbody ¥³¥ó¥Ý¡¼¥Í¥ó¥È¤ò Remove ¤·¤Æ¤¯¤À¤µ¤¤¡£
¡¡¤½¤Î¸å¡¢ºÆÅÙ¡¢PlayerTest ¥¹¥¯¥ê¥×¥È¤ò¥¢¥¿¥Ã¥Á¤·¤Æ¡¢Àè¤Û¤É¤ÈƱ¤¸¤è¤¦¤Ë Reset ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤ë¤³¤È¤ò³Îǧ¤·¤Þ¤¹¡£
¥¤¥ó¥¹¥Ú¥¯¥¿¡¼²èÁü
¥¹¥¯¥ê¥×¥È¤ò¥¢¥¿¥Ã¥Á¤·¤Æ Reset ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤ë¤³¤È¤ò³Îǧ¤¹¤ë
Æ°²è¥Õ¥¡¥¤¥ë¤Ø¤Î¥ê¥ó¥¯
¡¡³ÆÊÑ¿ô¤ò¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤«¤éÁàºî¤Ç¤¤ë¤è¤¦¤Ë¤Ê¤ê¤Þ¤·¤¿¤Î¤Ç¡¢ºÇ¸å¤Ë Reset ¥Ü¥¿¥ó¤Î³Îǧ¤ò¹Ô¤¤¤Þ¤¹¡£
¡¡¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Î¥³¥ó¥Æ¥¥¹¥È¥á¥Ë¥å¡¼¤«¤é[Reset]¥Ü¥¿¥ó¤ò¼Â¹Ô¤·¤¿¾ì¹ç¤ÎµóÆ°¤ò³Îǧ¤·¤Þ¤¹¡£
¡¡hp ÊÑ¿ô¤ÎÃͤò 100 °Ê³°¤Î¿ôÃͤËÀßÄꤷ(0 ¤Ê¤É)¡¢rb ÊÑ¿ô¤ò None ¤Î¾õÂ֤ˤ·¤Þ¤¹¡£
¡¡¤½¤Î¸å¡¢PlayerTest ¥¹¥¯¥ê¥×¥È¤Î±¦¾å¤Ë¤¢¤ë ¡Ä ¥á¥Ë¥å¡¼¤ò³«¤¤¤Æ¡¢ºÇ¾åÃʤˤ¢¤ë Reset ¥³¥Þ¥ó¥É¤ò¼Â¹Ô¤·¤Þ¤¹¡£
PlayerTest ¥¹¥¯¥ê¥×¥ÈÆâ¤Ë¤¢¤ë Reset ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤Æ¡¢ÊÑ¿ô¤Î¾ðÊó¤¬ Reset ¥á¥½¥Ã¥É¤ÇÀßÄꤷ¤Æ¤¤¤ë¾õÂÖ¤ËÌá¤ê¤Þ¤¹¡£
Reset ¥Ü¥¿¥ó¤ò¼Â¹Ô¤¹¤ë
Æ°²è¥Õ¥¡¥¤¥ë¤Ø¤Î¥ê¥ó¥¯
¡¡¤³¤Îµ¡Ç½¤ò³èÍѤ¹¤ë¤³¤È¤Ç¡¢Î㤨¤Ð¡¢¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Ç¿ôÃͤòÄ´À°¤·¤¿¤¤¾ì¹ç¤Ë¤Ï
»öÁ°¤Ë½é´üÃͤòÅÐÏ¿¤·¤Æ¤ª¤±¤Ð Reset ¥á¥½¥Ã¥É¤òÍøÍѤ¹¤ë¤³¤È¤Ç¡¢¤¤¤Ä¤Ç¤â½é´üÃͤËÌ᤻¤Þ¤¹¤Î¤Ç¡¢°Â¿´¤·¤ÆÄ´À°¤ò¹Ô¤¦¤³¤È¤¬½ÐÍè¤Þ¤¹¡£
¡¡Àè¤Û¤É¤Î PlayerTest ¥¯¥é¥¹¤Ç¤ÎÍøÍÑÊýË¡¤Î¾¤Ë¡¢¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë¥¯¥é¥¹¤òȽÃǤ·¤Æ¡¢ÃͤÎÀßÄê¤ò¹Ô¤¦¤È¤¤¤¦½èÍý¤â¼ÂÁõ²Äǽ¤Ç¤¹¡£
¡¡¿·¤·¤¯ enum ¤È OwnerChecker ¥¯¥é¥¹¤òºîÀ®¤·¡¢PlayerTest ¥¯¥é¥¹¤Î¥¢¥¿¥Ã¥Á¤Î²ÄÈݤòȽÄꤷ¤Æ¡¢¥ª¡¼¥Ê¡¼¤Î¼ïÎà¤òÆÃÄꤹ¤ë½èÍý¤ò¼ÂÁõ¤·¤Æ¤ß¤Þ¤¹¡£
OwnerType.cs
OwnerChecker.cs
¡¡TryGetComponent ¥á¥½¥Ã¥É¤Ç¼èÆÀ¤·¤¿¥³¥ó¥Ý¡¼¥Í¥ó¥È¤Î¾ðÊó¤òÍøÍѤ·¤Ê¤¤¾ì¹ç(¼èÆÀ¤Ç¤¤ë¤«¤É¤¦¤«¤À¤±ÃΤꤿ¤¤¾ì¹ç)¤Ë¤Ï¡¢
TryGetComponent ¥á¥½¥Ã¥É¤Î out Æâ¤ÎÊÑ¿ô¤ÎÀë¸ÀÉôʬ¤Ë¤Ï¡¢ÊÑ¿ô¤ÎÂå¤ï¤ê¤Ë _(¥¢¥ó¥À¡¼¥Ð¡¼)¤òÀë¸À¤·¤Þ¤¹¡£
¡¡¾åµ¤Î¤è¤¦¤Ë¡¢TryGetComponent ¥á¥½¥Ã¥É¤È Reset ¥á¥½¥Ã¥É¤òÁȤ߹ç¤ï¤»¤ë¤³¤È¤Ë¤è¤ê¡¢
¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë¥¯¥é¥¹¤Î¾ðÊó¤òȽÃǤ·¤Æ¡¢ÊÑ¿ô¤Ø¤ÎÂåÆþ½èÍý¤ò¹Ô¤¦¤³¤È¤â½ÐÍè¤Þ¤¹¡£
¡¡°Ê¾å¤Ë¤Ê¤ê¤Þ¤¹¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- ¥×¥í¥°¥é¥ß¥ó¥°
¥³¥á¥ó¥È¤ò¤«¤¯