Unity¤Ë´ØÏ¢¤¹¤ëµ­»ö¤Ç¤¹

MonoBehaviour.Reset() ¥á¥½¥Ã¥É


¡¡MonoBehaviour ¥¯¥é¥¹¤ËÍÑ°Õ¤µ¤ì¤Æ¤¤¤ë¥á¥½¥Ã¥É¤Ç¤¹¡£
¥³¥ó¥Ý¡¼¥Í¥ó¥È¤ò¥Ç¥Õ¥©¥ë¥È(½é´ü)Ãͤ˥ꥻ¥Ã¥È¤·¤Þ¤¹¡£

¡¡¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Î¥³¥ó¥Æ¥­¥¹¥È¥á¥Ë¥å¡¼¤Î[Reset]¥Ü¥¿¥ó¤ò²¡¤·¤¿¤È¤­¡¢¤Þ¤¿¤Ï¥³¥ó¥Ý¡¼¥Í¥ó¥È¤ò½é¤á¤ÆÄɲÃ(¥¢¥¿¥Ã¥Á´Þ¤à)¤·¤¿¤È¤­¤Ë¸Æ¤Ó½Ð¤µ¤ì¤Þ¤¹¡£


[Reset]¥Ü¥¿¥ó(±¦Â¦¤Ë¤¢¤ë¡Ä¤Î¥á¥Ë¥å¡¼Æâ)



»²¹Í¥µ¥¤¥È
Unity ¸ø¼°¥¹¥¯¥ê¥×¥È¥ê¥Õ¥¡¥ì¥ó¥¹
MonoBehaviour.Reset




¡¡¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Ç Reset ¥Ü¥¿¥ó¤ò³èÍѤ¹¤ë¥±¡¼¥¹¤È¤·¤Æ¤Ï¡¢Transform ¤ÎÃͤò½é´üÃͤËÌ᤹ºÝ¤ËÍøÍѤµ¤ì¤Æ¤¤¤ë¤È»×¤¤¤Þ¤¹¤¬¡¢
¼«ºî¥¯¥é¥¹Æâ¤Ë Reset ¥á¥½¥Ã¥É¤òÍÑ°Õ¤¹¤ë¤³¤È¤Ç¡¢Transform °Ê³°¤ÎÃͤˤâ½é´üÃͤòÀßÄꤷ¡¢Reset ¥Ü¥¿¥ó¤ò²¡¤¹¤³¤È¤Ç¡¢¾ï¤Ë½é´üÃͤËÌ᤹µ¡Ç½¤ò¼ÂÁõ¤Ç¤­¤Þ¤¹¡£

¡¡¤Þ¤¿¡¢¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Ë¸Â¤é¤º¡¢¥¯¥é¥¹Æâ¤Ç Reset ¥á¥½¥Ã¥É¤òǤ°Õ¤Î¥¿¥¤¥ß¥ó¥°¤Ç¼Â¹Ô¤¹¤ë¤³¤È¤â¤Ç¤­¤Þ¤¹¡£
Î㤨¤Ð¡¢Start ¥á¥½¥Ã¥ÉÆâ¤Ç Reset ¥á¥½¥Ã¥É¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ç¡¢¥²¡¼¥à¼Â¹Ô¤Î¥¿¥¤¥ß¥ó¥°¤Ë¹ç¤ï¤»¤Æ½é´üÃͤËÌ᤹¤è¤¦¤ÊµóÆ°¤¬¼ÂÁõ½ÐÍè¤Þ¤¹¡£


»²¹Í¥µ¥¤¥È
Qiita @neusstudio(Unity¥²¡¼¥à³«È¯¼Ô¥®¥ë¥É) ÍÍ
¡ÚUnity¡ÛMonoBehaviour.Reset()¤Î»È¤¤Êý



¼«ºî¥¯¥é¥¹¤Ç¤Î³èÍÑÎã


¡¡¼ÂºÝ¤ËÍøÍѤ·¤Æ¼ÂÁõ¤¹¤ëÊýË¡¤Î°ìÎã¤òÄ󼨤·¤Þ¤¹¡£

PlayerTest ¥¹¥¯¥ê¥×¥È¤òºîÀ®¤¹¤ë


¡¡Reset ¥á¥½¥Ã¥É¤ò¼ÂÁõ¤·¤¿¥¹¥¯¥ê¥×¥È¤òºîÀ®¤·¤Þ¤¹¡£

¡¡¤³¤ÎÊýË¡¤Ç¤¹¤È³ÆÊÑ¿ô¤Ï private ½¤¾þ»Ò¤Ç¤¢¤ë¤¿¤á¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Ë¤Ïɽ¼¨¤µ¤ì¤Ê¤¤¤¿¤á¡¢ÆâÉô¤Ç½èÍý¤¬Æ°¤¤¤Æ¤¤¤ë¤³¤È¤ò³Îǧ¤·¤Þ¤¹¡£

¡¡¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤ò Debug ¥â¡¼¥É¤ËÀÚ¤êÂؤ¨¤Æ¤ª¤­¤Þ¤¹¡£


PlayerTest.cs

<=¡¡¥¯¥ê¥Ã¥¯¤¹¤ë¤È³«¤­¤Þ¤¹¡£


¡¡¥¹¥¯¥ê¥×¥È¤¬´°À®¤·¤¿¤é¥»¡¼¥Ö¤ò¹Ô¤¤¡¢Ç¤°Õ¤Î¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Ë¥¢¥¿¥Ã¥Á¤·¤Æ¤¯¤À¤µ¤¤¡£

¡¡RequireComponent °À­¤Îµ¡Ç½¤Ë¤è¤ê¡¢Rigidbody ¥³¥ó¥Ý¡¼¥Í¥ó¥È¤¬¼«Æ°Åª¤Ë¥¢¥¿¥Ã¥Á¤µ¤ì¤Þ¤¹¡£

¡¡Reset ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤Æ¤ë¤³¤È¤Ë¤è¤ê¡¢hp ÊÑ¿ô¤Î½é´üÃͤȡ¢rb ÊÑ¿ô¤ËÂФ·¤Æ¤Î TryGetComponent ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤Æ rb ÊÑ¿ô¤ËÃͤ¬ÂåÆþ¤µ¤ì¤Þ¤¹¡£


¥¹¥¯¥ê¥×¥È¤ò¥¢¥¿¥Ã¥Á¤·¤Æ Reset ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤ë¤³¤È¤ò³Îǧ¤¹¤ë
Æ°²è¥Õ¥¡¥¤¥ë¤Ø¤Î¥ê¥ó¥¯


¥¤¥ó¥¹¥Ú¥¯¥¿¡¼²èÁü(Debug ¥â¡¼¥É)



¡¡Ìµ»ö¤Ë Reset ¥á¥½¥Ã¥É¤ÎµóÆ°¤¬³Îǧ¤Ç¤­¤¿¤é¡¢¥¹¥¯¥ê¥×¥È¤ò²þÎɤ·¤Þ¤¹¡£


PlayerTest ¥¹¥¯¥ê¥×¥È¤ò²þÎɤ¹¤ë


¡¡Â³¤¤¤Æ¡¢Æ±¤¸¥¹¥¯¥ê¥×¥È¤ò²þÎɤ·¤Þ¤¹¡£

¡¡³ÆÊÑ¿ô¤Ë SerializeField °À­¤òÉÕÍ¿¤·¤Æ¤ª¤­¤Þ¤¹¡£
¤³¤¦¤¹¤ë¤³¤È¤Ç¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤¬ Normal ¥â¡¼¥É¤Ç¤¢¤Ã¤Æ¤âÃͤÎÆ°¤­¤â¤ï¤«¤ê¤Þ¤¹¤Î¤Ç¡¢
¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤ò Debug ¥â¡¼¥É¤«¤é Normal ¥â¡¼¥É¤ËÌᤷ¤Æ¤¯¤À¤µ¤¤¡£

¡¡¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤«¤éÀ©¸æ¤¹¤ë¤³¤È¤â¤Ç¤­¤Þ¤¹¤Î¤Ç¡¢¥Ç¥Ð¥Ã¥°¤·¤ä¤¹¤¤´Ä¶­¤Ë¤Ê¤ê¤Þ¤¹¡£

¡¡¤³¤Î²þÎÉÎã¤Ï¡¢Reset ¥á¥½¥Ã¥É¤ò³°Éô¥¯¥é¥¹¤«¤é¼Â¹Ô¤Ç¤­¤ë¥á¥½¥Ã¥É¤ò½àÈ÷¤·¤Æ¤ª¤¯¤³¤È¤Ç¡¢
ºÇ½é¤Ë¥¹¥¯¥ê¥×¥È¤ò¥¢¥¿¥Ã¥Á¤·¤¿¤È¤­°Ê³°¤Î¥¿¥¤¥ß¥ó¥°¤Ç¤Î¥ê¥»¥Ã¥Èµ¡Ç½¤òÍ­¸ú¤Ë¤¹¤ëÊýË¡¤Î¼ÂÁõÎã¤Ç¤¹¡£

¡¡¤Þ¤¿¡¢¥á¥ó¥ÐÊÑ¿ô¤ÇÃͤòÂåÆþ¤·¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢³Æ·¿¤´¤È¤Î½é´üÃͤ¬ÂåÆþ¤µ¤ì¤Æ¤·¤Þ¤¤¤Þ¤¹¤Î¤Ç¡¢
¤½¤Î¾ì¹ç¤Ë¤Ï¡¢Reset ¥á¥½¥Ã¥ÉÆâ¤Ç½é´üÃͤòÂåÆþ¤¹¤ë¤è¤¦¤ËÍÑ°Õ¤·¤Æ¤ª¤­¤Þ¤¹¡£


PlayerTest.cs

<=¡¡¥¯¥ê¥Ã¥¯¤¹¤ë¤È³«¤­¤Þ¤¹¡£


¡¡¥¹¥¯¥ê¥×¥È¤ò²þÎɤ·¤¿¤é¥»¡¼¥Ö¤·¤Þ¤¹¡£

¡¡¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Ë¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë PlayerTest ¥¹¥¯¥ê¥×¥È¤È Rigidbody ¥³¥ó¥Ý¡¼¥Í¥ó¥È¤ò Remove ¤·¤Æ¤¯¤À¤µ¤¤¡£

¡¡¤½¤Î¸å¡¢ºÆÅÙ¡¢PlayerTest ¥¹¥¯¥ê¥×¥È¤ò¥¢¥¿¥Ã¥Á¤·¤Æ¡¢Àè¤Û¤É¤ÈƱ¤¸¤è¤¦¤Ë Reset ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤ë¤³¤È¤ò³Îǧ¤·¤Þ¤¹¡£

¥¤¥ó¥¹¥Ú¥¯¥¿¡¼²èÁü



¥¹¥¯¥ê¥×¥È¤ò¥¢¥¿¥Ã¥Á¤·¤Æ Reset ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤ë¤³¤È¤ò³Îǧ¤¹¤ë
Æ°²è¥Õ¥¡¥¤¥ë¤Ø¤Î¥ê¥ó¥¯


¡¡³ÆÊÑ¿ô¤ò¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤«¤éÁàºî¤Ç¤­¤ë¤è¤¦¤Ë¤Ê¤ê¤Þ¤·¤¿¤Î¤Ç¡¢ºÇ¸å¤Ë Reset ¥Ü¥¿¥ó¤Î³Îǧ¤ò¹Ô¤¤¤Þ¤¹¡£


¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Î¥³¥ó¥Æ¥­¥¹¥È¥á¥Ë¥å¡¼¤Î[Reset]¥Ü¥¿¥ó¤ò¼Â¹Ô¤¹¤ë


¡¡¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Î¥³¥ó¥Æ¥­¥¹¥È¥á¥Ë¥å¡¼¤«¤é[Reset]¥Ü¥¿¥ó¤ò¼Â¹Ô¤·¤¿¾ì¹ç¤ÎµóÆ°¤ò³Îǧ¤·¤Þ¤¹¡£

¡¡hp ÊÑ¿ô¤ÎÃͤò 100 °Ê³°¤Î¿ôÃͤËÀßÄꤷ(0 ¤Ê¤É)¡¢rb ÊÑ¿ô¤ò None ¤Î¾õÂ֤ˤ·¤Þ¤¹¡£

¡¡¤½¤Î¸å¡¢PlayerTest ¥¹¥¯¥ê¥×¥È¤Î±¦¾å¤Ë¤¢¤ë ¡Ä ¥á¥Ë¥å¡¼¤ò³«¤¤¤Æ¡¢ºÇ¾åÃʤˤ¢¤ë Reset ¥³¥Þ¥ó¥É¤ò¼Â¹Ô¤·¤Þ¤¹¡£
PlayerTest ¥¹¥¯¥ê¥×¥ÈÆâ¤Ë¤¢¤ë Reset ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤Æ¡¢ÊÑ¿ô¤Î¾ðÊó¤¬ Reset ¥á¥½¥Ã¥É¤ÇÀßÄꤷ¤Æ¤¤¤ë¾õÂÖ¤ËÌá¤ê¤Þ¤¹¡£


Reset ¥Ü¥¿¥ó¤ò¼Â¹Ô¤¹¤ë
Æ°²è¥Õ¥¡¥¤¥ë¤Ø¤Î¥ê¥ó¥¯



¡¡¤³¤Îµ¡Ç½¤ò³èÍѤ¹¤ë¤³¤È¤Ç¡¢Î㤨¤Ð¡¢¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Ç¿ôÃͤòÄ´À°¤·¤¿¤¤¾ì¹ç¤Ë¤Ï
»öÁ°¤Ë½é´üÃͤòÅÐÏ¿¤·¤Æ¤ª¤±¤Ð Reset ¥á¥½¥Ã¥É¤òÍøÍѤ¹¤ë¤³¤È¤Ç¡¢¤¤¤Ä¤Ç¤â½é´üÃͤËÌ᤻¤Þ¤¹¤Î¤Ç¡¢°Â¿´¤·¤ÆÄ´À°¤ò¹Ô¤¦¤³¤È¤¬½ÐÍè¤Þ¤¹¡£


TryGetComponent ¥á¥½¥Ã¥É¤È¤ÎÁȤ߹ç¤ï¤»Êý


¡¡Àè¤Û¤É¤Î PlayerTest ¥¯¥é¥¹¤Ç¤ÎÍøÍÑÊýË¡¤Î¾¤Ë¡¢¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë¥¯¥é¥¹¤òȽÃǤ·¤Æ¡¢ÃͤÎÀßÄê¤ò¹Ô¤¦¤È¤¤¤¦½èÍý¤â¼ÂÁõ²Äǽ¤Ç¤¹¡£

¡¡¿·¤·¤¯ enum ¤È OwnerChecker ¥¯¥é¥¹¤òºîÀ®¤·¡¢PlayerTest ¥¯¥é¥¹¤Î¥¢¥¿¥Ã¥Á¤Î²ÄÈݤòȽÄꤷ¤Æ¡¢¥ª¡¼¥Ê¡¼¤Î¼ïÎà¤òÆÃÄꤹ¤ë½èÍý¤ò¼ÂÁõ¤·¤Æ¤ß¤Þ¤¹¡£


OwnerType.cs

<=¡¡¥¯¥ê¥Ã¥¯¤¹¤ë¤È³«¤­¤Þ¤¹¡£




OwnerChecker.cs

<=¡¡¥¯¥ê¥Ã¥¯¤¹¤ë¤È³«¤­¤Þ¤¹¡£


¡¡TryGetComponent ¥á¥½¥Ã¥É¤Ç¼èÆÀ¤·¤¿¥³¥ó¥Ý¡¼¥Í¥ó¥È¤Î¾ðÊó¤òÍøÍѤ·¤Ê¤¤¾ì¹ç(¼èÆÀ¤Ç¤­¤ë¤«¤É¤¦¤«¤À¤±ÃΤꤿ¤¤¾ì¹ç)¤Ë¤Ï¡¢
TryGetComponent ¥á¥½¥Ã¥É¤Î out Æâ¤ÎÊÑ¿ô¤ÎÀë¸ÀÉôʬ¤Ë¤Ï¡¢ÊÑ¿ô¤ÎÂå¤ï¤ê¤Ë _(¥¢¥ó¥À¡¼¥Ð¡¼)¤òÀë¸À¤·¤Þ¤¹¡£

¡¡¾åµ­¤Î¤è¤¦¤Ë¡¢TryGetComponent ¥á¥½¥Ã¥É¤È Reset ¥á¥½¥Ã¥É¤òÁȤ߹ç¤ï¤»¤ë¤³¤È¤Ë¤è¤ê¡¢
¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë¥¯¥é¥¹¤Î¾ðÊó¤òȽÃǤ·¤Æ¡¢ÊÑ¿ô¤Ø¤ÎÂåÆþ½èÍý¤ò¹Ô¤¦¤³¤È¤â½ÐÍè¤Þ¤¹¡£



¡¡°Ê¾å¤Ë¤Ê¤ê¤Þ¤¹¡£

¥³¥á¥ó¥È¤ò¤«¤¯


¡Öhttp://¡×¤ò´Þ¤àÅê¹Æ¤Ï¶Ø»ß¤µ¤ì¤Æ¤¤¤Þ¤¹¡£

ÍøÍѵ¬Ìó¤ò¤´³Îǧ¤Î¤¦¤¨¤´µ­Æþ²¼¤µ¤¤

Menu


´ðÁÃ

µ»½Ñ/Ãμ±(¼ÂÁõÎã)

3D¥¢¥¯¥·¥ç¥ó¥²¡¼¥à

2D¤ª¤Ï¤¸¤­¥²¡¼¥à(ȯŸÊÔ)

2D¶¯À©²£¥¹¥¯¥í¡¼¥ë¥¢¥¯¥·¥ç¥ó(ȯŸÊÔ)

2D¥¿¥Ã¥×¥·¥å¡¼¥Æ¥£¥ó¥°(³ÈÄ¥ÊÔ)

¥ì¡¼¥¹¥²¡¼¥à(È´¿è)

2DÊüÃÖ¥²¡¼¥à(ȯŸÊÔ)

3D¥ì¡¼¥ë¥¬¥ó¥·¥å¡¼¥Æ¥£¥ó¥°(±þÍÑÊÔ)

3Dæ½Ð¥²¡¼¥à(È´¿è)

2D¥ê¥¢¥ë¥¿¥¤¥à¥¹¥È¥é¥Æ¥¸¡¼

3D¥¿¥Ã¥×¥¢¥¯¥·¥ç¥ó(NavMeshAgent »ÈÍÑ)

2D¥È¥Ã¥×¥Ó¥å¡¼¥¢¥¯¥·¥ç¥ó(¥«¥¨¥ë¤Î°Ù¤Ë¡Á¡¢¥Ü¥³¥¹¥«¥¦¥©¡¼¥ºÉ÷)

VideoPlayer ¥¤¥Ù¥ó¥ÈϢư¤Î¼ÂÁõÎã

VideoPlayer ¥ê¥¹¥ÈÆ⤫¤é¥à¡¼¥Ó¡¼ºÆÀ¸¤Î¼ÂÁõÎã(ȯŸ)

AR ²èÁüÉÕ¤­¥ª¥Ö¥¸¥§¥¯¥ÈÀ¸À®¤Î¼ÂÁõÎã

AR ¥ê¥¹¥ÈÆ⤫¤éÀ¸À®¤Î¼ÂÁõÎã(ȯŸ)

2D¥È¥Ã¥×¥Ó¥å¡¼¥¢¥¯¥·¥ç¥ó(¥µ¥Ð¥¤¥Ð¡¼É÷)

private



¤³¤Î¥µ¥¤¥ÈÆâ¤ÎºîÉʤϥæ¥Ë¥Æ¥£¤Á¤ã¤ó¥é¥¤¥»¥ó¥¹¾ò¹à¤Î¸µ¤ËÄ󶡤µ¤ì¤Æ¤¤¤Þ¤¹¡£

´ÉÍý¿Í/Éû´ÉÍý¿Í¤Î¤ßÊÔ½¸¤Ç¤­¤Þ¤¹