ºÇ½ª¹¹¿·¡§
orika_ex_miyako 2023ǯ08·î05Æü(ÅÚ) 23:16:43ÍúÎò
¡¡¤³¤Î¥¯¥é¥¹¼«ÂΤÏÊ̤ε»ö¤Ç¥¯¥é¥¹¤òʬ³ä¤·¤ÆºîÀ®¤·¤¿¥¯¥é¥¹¤Ë¤Ê¤ê¤Þ¤¹¡£
¡¡¡¡¡¡=>¡¡ÈîÂç²½¤·¤¿¥¯¥é¥¹Æâ¤Î½èÍý¤ò¡¢Ìò³ä¤Ë±þ¤¸¤¿¥¯¥é¥¹¤òÊ£¿ôºîÀ®¤·¤Æʬ³ä¤¹¤ë¼ÂÁõÎã¢
¡¡SetUpCharaManager ¥á¥½¥Ã¥ÉÆâ¤Î½èÍý¤È¡¢Update ¥á¥½¥Ã¥ÉÆâ¤Î½èÍý¤Ë¤Ä¤¤¤Æ¸«Ä¾¤·¤Þ¤¹¡£
public void SetUpCharaManager() { // ¤³¤Î¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Ë¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë IChara ¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤·¤Æ¤¤¤ë¤¹¤Ù¤Æ¤Î¥¯¥é¥¹¤ò¼èÆÀ¤·¤ÆÇÛÎó¤ËÂåÆþ IChara[] charas = GetComponents<IChara>(); // ³Æ¥¯¥é¥¹¤Î½é´üÀßÄê(¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤Î¾ðÊó¤Ç¥¯¥é¥¹¤ò¼èÆÀ¤·¤Æ¤¢¤ë¤Î¤Ç¡¢¥¯¥é¥¹Ì¾¤¬¤½¤ì¤¾¤ì°ã¤Ã¤Æ¤¤¤Æ¤â foreach ¤Ç¤Þ¤È¤á¤Æ½èÍý¤Ç¤¤ë) foreach (var chara in charas) { chara.SetUpChara(); } // ³Æ¥¯¥é¥¹¤Î¼èÆÀ TryGetComponent(out charaMove); TryGetComponent(out charaAnime); if(TryGetComponent(out charaAttack)) { // ¹¶·â½àÈ÷ charaAttack.PrepareAttackRepeatedly(); } }
¡¡ºÇ½é¤Ë GetComponents ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ IChara ¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤·¤Æ¤¤¤ë¥¯¥é¥¹¤ò¼èÆÀ¤·¤Æ¤¤¤Þ¤¹¤¬¡¢
¤½¤Î¸å¡¢ºÆÅÙ TryGetComponent ¥á¥½¥Ã¥É¤Î½èÍý¤¬¤¢¤ê¤Þ¤¹¡£
¡¡¤½¤Î¤¿¤á¡¢¤¤¤ï¤ÐƱ¤¸¥¯¥é¥¹¤ËÂФ·¤Æ£²²ó¤º¤Ä¼èÆÀ¤ÎÌ¿Î᤬¼Â¹Ô¤µ¤ì¤Æ¤¤¤ë¾õÂ֤ˤʤäƤ¤¤Þ¤¹¡£
¹ç¤ï¤»¤Æ¡¢¥á¥ó¥ÐÊÑ¿ô¤â¤½¤ì¤¾¤ìÍÑ°Õ¤µ¤ì¤Æ¤¤¤ë¤¿¤á¡¢¿·¤·¤¤¥¯¥é¥¹¤òÄɲä·¤¿ºÝ¤Ë¤âÄɲäÎÊÑ¿ô¤¬É¬Íפˤʤê¤Þ¤¹¡£
¡¡Update ¥á¥½¥Ã¥ÉÆâ¤Ë¤ÏŬÀڤʥ¿¥¤¥ß¥ó¥°¤Ç null ¥Á¥§¥Ã¥¯¤¬¹Ô¤ï¤ì¤Æ¤¤¤Þ¤¹¤¬¡¢
¥Í¥¹¥È¤¬Àõ¤¯¤Ê¤Ã¤ÆÆɤߤ䤹¤¯¤Ê¤Ã¤Æ¤¤¤ëÈ¿ÌÌ¡¢null ¥Á¥§¥Ã¥¯¤Ë¤è¤ê¡¢½èÍý¤¬Ä¹¤¯¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
void Update() { // null ¥Á¥§¥Ã¥¯ if (charaMove == null || charaAnime == null) { return; } // ¥¿¥Ã¥×ÆþÎÏ if (Input.GetMouseButton(0)) { // ¥¿¥Ã¥×¤·¤¿°ÌÃÖ¤«¤é¥ï¡¼¥ë¥ÉºÂɸ¼èÆÀ Vector2 tapPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); // °ÜÆ°¤È¥¢¥Ë¥áƱ´ü charaMove.Move(tapPos); // ¸þ¤¤Î¹¹¿· UpdateDirection(tapPos); // °ÜÆ°¸þ¤¤È¥¢¥Ë¥á¤Î¸þ¤¤ÎƱ´ü charaAnime.UpdateAnimation(direction); } // CharaAttack ¤Ë¹¶·âÍѤθþ¤¤Î¾ðÊó¤òÄó¶¡ if (charaAttack != null) { charaAttack.UpdateAttackDirection(direction); } // ¹¶·â¤Î°ì»þÄä»ß¤ÈºÆ³« if (Input.GetMouseButtonDown(1)) { // ¹¶·â¤Î°ì»þÄä»ß¡¦ºÆ³«¤òÀÚ¤êÂؤ¨¤ë charaAttack.ToggleAttack(); } }
¡¡¤³¤ì¤é¤ÎÉôʬ¤ËÃíÌܤ·¤Æ¡¢¤½¤ì¤¾¤ì¤Îµ¡Ç½¤ò³èÍѤ·¤Æ¸úΨ²½¤ò¿Þ¤ê¤Þ¤¹¡£
¡¡¤Ê¤ª¡¢UpdateDirection ¥á¥½¥Ã¥É¤ÏÊѹ¹¤¢¤ê¤Þ¤»¤ó¡£
¡¡¥¯¥é¥¹ÍѤΣ³¤Ä¤Î¥á¥ó¥ÐÊÑ¿ô¤òºï½ü¤·¡¢³Æ¥¯¥é¥¹¤Î¼èÆÀ½èÍý¤â£±²ó¤º¤Ä¤Ë¤·¤Þ¤¹¡£
¡¡¥¯¥é¥¹¤Î¾ðÊó¤Ï Dictionary Æâ¤Ë³ÊǼ¤µ¤ì¤Æ¤¤¤Þ¤¹¤¬¡¢¡Ö¤É¤ì¡×¤¬¡Ö¤É¤Î¡×Í×ÁǤȤ·¤Æ¸ºß¤·¤Æ¤¤¤ë¤«¤ÏÉÔÌÀ¤Ç¤¢¤ë¤¿¤á¡¢
¼Â¹ÔÌ¿Îá¤ò¤ª¤³¤Ê¤¦ºÝ¤Ë¥¯¥é¥¹¤ò»ØÄꤷ¤Æ½èÍý¤ò¹Ô¤¨¤ë¤è¤¦¤Ë¿·¤·¤¯ GetChara ¥á¥½¥Ã¥É¤òÄɲ䷤Ƥ¤¤Þ¤¹¡£
using System.Collections.Generic;¡¡¡¡//¡¡¢«¡¡Dictionary ¤òÍøÍѤ¹¤ë¤¿¤á¤Ë»È¤¤¤Þ¤¹ using UnityEngine; using System;¡¡¡¡//¡¡¢«¡¡Type ¤òÍøÍѤ¹¤ë¤¿¤á¤ËÄɲä·¤Þ¤¹ /// <summary> /// ¥¥ã¥é¤Î´ÉÍý¥¯¥é¥¹ /// </summary> [RequireComponent(typeof(CharaMove))] [RequireComponent(typeof(CharaAnime))] [RequireComponent(typeof(CharaAttack))] public class CharaManager : MonoBehaviour { // private CharaMove charaMove; // private CharaAnime charaAnime; // private CharaAttack charaAttack; private Vector2 direction; //¥¥ã¥é¤¬¸þ¤¤¤Æ¤¤¤ëÊý¸þ public Vector2 Direction => direction; // get ¤Î¤ß¤Î¥×¥í¥Ñ¥Æ¥£ private Dictionary<Type, IChara> charaDictionary = new(); void Start() { // ¥Ç¥Ð¥Ã¥°ÍÑ //SetUpCharaManager(); } /// <summary> /// ³Æ¥¯¥é¥¹¤Î½é´üÀßÄê¡£³°Éô¥¯¥é¥¹¤«¤é¼Â¹Ô¤¹¤ëÁ°Äó /// </summary> public void SetUpCharaManager() { // // Ä̾ï¤Î¾ì¹ç¡£GetComponents ¤È TryGetComponent ¤¬¤¢¤ë¤Î¤Ç¡¢Æ±¤¸¥¯¥é¥¹¤ò¼Â¼Á2²ó¼èÆÀ¤·¤Æ¤¤¤ë // IChara[] charas = GetComponents<IChara>(); // // // ³Æ»Ò¥¯¥é¥¹¤Î½é´üÀßÄê(¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤Ç¼èÆÀ¤·¤Æ¤¢¤ë¤Î¤Ç¡¢»Ò¥¯¥é¥¹¤¬°ã¤Ã¤Æ¤â foreach ¤Ç¤Þ¤È¤á¤Æ½èÍý¤Ç¤¤ë) // foreach (var chara in charas) { // chara.SetUpChara(); // } // // // ³Æ»Ò¥¯¥é¥¹¤Î¼èÆÀ // TryGetComponent(out charaMove); // // TryGetComponent(out charaAnime); // // // if(TryGetComponent(out charaAttack)) // { // // ¹¶·â½àÈ÷ // charaAttack.PrepareAttackRepeatedly(); // } // Dictionary ¤Î¾ì¹ç¡¢¾åµ¤Î£²½Å¼èÆÀ¤ÎÌäÂê¤ò²ò¾Ã¤Ç¤¤ë IChara[] charas = GetComponents<IChara>(); // ³Æ¥¯¥é¥¹¤Î½é´üÀßÄê(¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤Ç¼èÆÀ¤·¤Æ¤¢¤ë¤Î¤Ç¡¢¥¯¥é¥¹¤¬°ã¤Ã¤Æ¤â foreach ¤Ç¤Þ¤È¤á¤Æ½èÍý¤Ç¤¤ë) foreach (var chara in charas) { charaDictionary[chara.GetType()] = chara; chara.SetUpChara(); } GetChara<CharaAttack>()?.PrepareAttackRepeatedly(); ¡¡¡¡} /// <summary> /// Dictionary ¤Ë¤¢¤ë»ØÄꤷ¤¿¥¯¥é¥¹¤ò¼èÆÀ /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> private T GetChara<T>() where T : class, IChara // where ¤Ë class ¤ò»ØÄꤷ¤Ê¤¤¤È»²¾È·¿¤¬ÊݾڤǤ¤Ê¤¤¤Î¤Ç null ¤¬ÊÖ¤»¤Ê¤¤ { if (charaDictionary.TryGetValue(typeof(T), out IChara chara)) { return (T)chara; } return null; // Ìá¤êÃͤη¿¤Ï T? ¤Î¤è¤¦¤Ë null µöÍƤˤ·¤Ê¤¯¤Æ¤âÌäÂê¤Ê¤¤ } void Update() { // null ¥Á¥§¥Ã¥¯ // if (charaMove == null || charaAnime == null) { // return; // } // ¥¿¥Ã¥×ÆþÎÏ if (Input.GetMouseButton(0)) { // ¥¿¥Ã¥×¤·¤¿°ÌÃÖ¤«¤é¥ï¡¼¥ë¥ÉºÂɸ¼èÆÀ Vector2 tapPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); // °ÜÆ°¤È¥¢¥Ë¥áƱ´ü //charaMove.Move(tapPos); GetChara<CharaMove>()?.Move(tapPos); // ¸þ¤¤Î¹¹¿· UpdateDirection(tapPos); // °ÜÆ°¸þ¤¤È¥¢¥Ë¥á¤Î¸þ¤¤ÎƱ´ü //charaAnime.UpdateAnimation(direction); GetChara<CharaAnime>()?.UpdateAnimation(direction); } ¡¡¡¡¡¡¡¡// CharaAttack ¤Ë¹¶·âÍѤθþ¤¤Î¾ðÊó¤òÄó¶¡ // if (charaAttack != null) { // charaAttack.UpdateAttackDirection(direction); // } GetChara<CharaAttack>()?.UpdateAttackDirection(direction); // ¹¶·â¤Î°ì»þÄä»ß¤ÈºÆ³« if (Input.GetMouseButtonDown(1)) { // ¹¶·â¤Î°ì»þÄä»ß¡¦ºÆ³«¤òÀÚ¤êÂؤ¨¤ë //charaAttack.ToggleAttack(); GetChara<CharaAttack>()?.ToggleAttack(); } } /// <summary> /// ¸þ¤¤Î¹¹¿· /// </summary> /// <param name="newPos"></param> private void UpdateDirection(Vector2 newPos) { direction = (newPos - (Vector2)transform.position).normalized; } }
¡¡½èÍý¤Î¼Â¹ÔÌ¿Î᤬¡¢ÊÑ¿ô·Ðͳ¤Ç¤Ï¤Ê¤¯¡¢List Æâ¤Î½êÄê¤Î¥¯¥é¥¹¤ò»ØÄꤷ¤Æ¼Â¹Ô¤¹¤ë·Á¼°¤ËÊѹ¹¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
GetChara<CharaAttack>()?.PrepareAttackRepeatedly();
¡¡¤³¤ÎÌ¿Îá¤Ë¤è¤ê¡¢²¼µ¤Î¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤Þ¤¹¡£
private T GetChara<T>() where T : class, IChara { if (charaDictionary.TryGetValue(typeof(T), out IChara chara)) { return (T)chara; } return null; }
¡¡¥¸¥§¥Í¥ê¥Ã¥¯¤Ç¤¢¤ë T ¤ÎÉôʬ¤Ë¤Ï¡¢¼Â¹ÔÌ¿Îá»þ¤Ë GetChara<T> ¤Ç»ØÄꤷ¤¿¥¯¥é¥¹¤¬Æþ¤ê¤Þ¤¹¤Î¤Ç¡¢
º£²ó¤Ç¤¢¤ì¤Ð¡¢T ¤Ï²¼µ¤Î¤è¤¦¤Ë¤¹¤Ù¤Æ CharaAttack ¤Ç½èÍý¤µ¤ì¤Þ¤¹¡£
¡ã¥¸¥§¥Í¥ê¥Ã¥¯¤Ç¤¢¤ë T Éôʬ¤òÃÖ¤´¹¤¨¤ë¡ä
private CharaAttack GetChara<CharaAttack>() where CharaAttack : class, IChara { if (charaDictionary.TryGetValue(typeof(CharaAttack), out IChara chara)) { return (CharaAttack)chara; } return null; }
¡¡¤Þ¤º T ¤ÎÃͤǤ¢¤ë CharaAttack ¥¯¥é¥¹¤¬¡¢where ¤Ë¤è¤Ã¤Æ¥¯¥é¥¹¤«¡¢IChara ¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤Î¤¤¤º¤ì¤«¤Ç¤¢¤ë¤«¥Á¥§¥Ã¥¯¤µ¤ì¤Þ¤¹¡£
º£²ó¤Ï¤É¤Á¤é¤âÌäÂꤢ¤ê¤Þ¤»¤ó¤Î¤Ç¡¢Â³¤±¤Æ¥á¥½¥Ã¥ÉÆâ¤Î½èÍý¤¬¼Â¹Ô¤µ¤ì¤Þ¤¹¡£
¡¡Dictionary ¤ËÍÑ°Õ¤µ¤ì¤Æ¤¤¤ë TryGetValue ¥á¥½¥Ã¥É¤ò¼Â¹Ô¤·¡¢Dictionary Æ⤫¤é CharaAttack ¥¯¥é¥¹¤¬¼èÆÀ¤Ç¤¤ë¤«È½Äꤷ¤Þ¤¹¡£
¼èÆÀ¤Ç¤¤¿¾ì¹ç¤Ë¤Ï out ¥¡¼¥ï¡¼¥É¤Î±¦ÊÕ¤ËÍÑ°Õ¤µ¤ì¤Æ¤¤¤ë chara ÊÑ¿ô¤Ë¾ðÊó¤¬ÂåÆþ¤µ¤ì¤Þ¤¹¡£
¤¿¤À¤·¤³¤Î¤È¤¼èÆÀ¤·¤¿ÃÍ¤Ï IChara ·¿¤Ç¤¢¤ë¤¿¤á¡¢return ¤Î¥¿¥¤¥ß¥ó¥°¤Ç CharaAttack ¥¯¥é¥¹¤Ë¥¥ã¥¹¥È¤·¤ÆÌᤷ¤Þ¤¹¡£
¡¡¤â¤·¤â Dictionary Æâ¤Ë CharaAttack ¥¯¥é¥¹¤¬¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¤Ë¤Ï null ¤òÌᤷ¤Þ¤¹¡£
GetChara<CharaAttack>()?.PrepareAttackRepeatedly();
¡¡¤³¤Î½èÍý¤Ç¤Ï¡¢¥á¥½¥Ã¥É¤ÎºÇ¸å¤Ë ? ±é»»»Ò¤¬¤¢¤ê¡¢Ìá¤êÃͤ¬ null ¤Ç¤Ï¤Ê¤¤¤«¡¢³Îǧ¤ò¹Ô¤Ã¤Æ¤¤¤Þ¤¹(null ¥Á¥§¥Ã¥¯)¡£
¤½¤·¤Æ¡¢null ¤Ç¤Ï¤Ê¤¤¾ì¹ç¤Ë¤Ï»ØÄꤷ¤¿¥¯¥é¥¹¤¬¼èÆÀ¤Ç¤¤¿¾ì¹ç¤Î¤ß¡¢¥á¥½¥Ã¥É¤ò¼Â¹Ô¤·¤Þ¤¹¡£
¡¡Dictionary ¤Ë¤è¤Ã¤Æ¥¯¥é¥¹¤ò´ÉÍý¤¹¤ë¾ì¹ç¤Ë¤Ï¡¢¤³¤ÎÊýË¡¤ò»È¤¦¤³¤È¤Ç¡¢¥¯¥é¥¹¤Î½ÅÊ£¼èÆÀ¤ä¥á¥ó¥ÐÊÑ¿ô¤ò¤Ê¤¯¤·¡¢
¤½¤·¤Æ¥¯¥é¥¹¤ò»È¤¦¥¿¥¤¥ß¥ó¥°¤Ç¥Á¥§¥Ã¥¯¤·¤Æ»È¤¦Êý¼°¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
¡¡Update ¥á¥½¥Ã¥ÉÆâ¤Ç¤Î null ¥Á¥§¥Ã¥¯¤â¤¹¤Ù¤Æ¾åµ¤Î ? ±é»»»Ò¤Ë¤è¤ë null ¥Á¥§¥Ã¥¯¤ËÃÖ¤´¹¤¨¤Æ¤¤¤Þ¤¹¡£
¤½¤Î¤¿¤á¡¢½èÍý¤¬´Ê·é¤Ëµ½Ò¤µ¤ì¤Æ¤¤¤Þ¤¹¡£
¡¡¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤·¤Þ¤·¤¿¤Î¤Ç¡¢¤¤¤Þ¤Þ¤Ç¤ÈƱ¤¸¤è¤¦¤Ëµ¡Ç½¤¹¤ë¤«¡¢³Îǧ¤ò¹Ô¤¤¤Þ¤·¤ç¤¦
¡¡ºï½ü¤·¤¿¥á¥ó¥ÐÊÑ¿ô¤ä¡¢Update ¥á¥½¥Ã¥É¤ÎÆâÍÆ¤Ï Dictionary ¤Î¾ì¹ç¤ÈƱ¤¸¤Ç¤¹¡£
¤½¤Î¤¿¤á¤³¤Á¤é¤Ç¤Ï¼ÂºÝ¤Ë¥³¥á¥ó¥È¥¢¥¦¥ÈÉôʬ¤òºï½ü¤·¡¢½èÍý¤¬¸úΨ²½¤µ¤ì¤Æ¤¤¤ë¤³¤È¤ò¤ï¤«¤ê¤ä¤¹¤¤·Á¤ÇÄ󼨤·¤Þ¤¹¡£
¡¡¤³¤Á¤é¤â List Æâ¤Ë¤Ï IChara ¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤ò³ÊǼ¤·¤Æ¤ª¤¡¢GetChara ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ¥¯¥é¥¹¤òÆÃÄꤷ¤ÆÍøÍѤ·¤Þ¤¹¡£
Dictionary ¤Î¼ÂÁõ»þ¤ÈƱ̾¤Î¥á¥½¥Ã¥É¤Ç¤¹¤¬¡¢¤³¤Á¤é¤Î½èÍý¤Ë¤Ï LINQ ¤Î OfType<T> ¥á¥½¥Ã¥É¤Îµ¡Ç½¤òÍøÍѤ·¤Æ¤¤¤Þ¤¹¡£
¡¡Ê£¿ô¤Î²Õ½ê¤Ë LINQ ¤ò»È¤ï¤Ê¤¤¾ì¹ç¤Î½èÍý¤â°ì½ï¤Ëµ½Ò¤·¤Æ¤¢¤ê¤Þ¤¹¤Î¤Ç¡¢
¤É¤Î¤è¤¦¤Ë¤·¤Æ foreach ¤Î½èÍý¤ÎÉôʬ¤¬ LINQ ¤Î½èÍý¤ËÃÖ¤´¹¤ï¤Ã¤Æ¤¤¤ë¤Î¤«¤ò³Îǧ¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£
using System.Collections.Generic;¡¡¡¡//¡¡¢«¡¡List ¤òÍøÍѤ¹¤ë¤¿¤á¤Ë»È¤¤¤Þ¤¹ using UnityEngine; using System.Linq;¡¡¡¡//¡¡¢«¡¡Äɲä·¤Þ¤¹(System ¤ÏÉÔÍפǤ¹) /// <summary> /// ¥¥ã¥é¤Î´ÉÍý¥¯¥é¥¹ /// </summary> [RequireComponent(typeof(CharaMove))] [RequireComponent(typeof(CharaAnime))] [RequireComponent(typeof(CharaAttack))] public class CharaManager : MonoBehaviour { private Vector2 direction; //¥¥ã¥é¤¬¸þ¤¤¤Æ¤¤¤ëÊý¸þ public Vector2 Direction => direction; // get ¤Î¤ß¤Î¥×¥í¥Ñ¥Æ¥£ private List<IChara> charaList = new(); void Start() { // ¥Ç¥Ð¥Ã¥°ÍÑ //SetUpCharaManager(); } /// <summary> /// ³Æ¥¯¥é¥¹¤Î½é´üÀßÄê¡£³°Éô¥¯¥é¥¹¤«¤é¼Â¹Ô¤¹¤ëÁ°Äó /// </summary> public void SetUpCharaManager() { // List ¤Î¾ì¹ç¤â¡¢¾åµ¤Î£²½Å¼èÆÀ¤ÎÌäÂê¤ò²ò¾Ã¤Ç¤¤ë charaList.AddRange(GetComponents<IChara>()); foreach (var chara in charaList) { chara.SetUpChara(); } ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¢ ¡¡¡¡¡¡¡¡// ¾åµ¤Î foreach Éôʬ¤Ï¡¢foreach ÆâÉô¤Ç¼Â¹Ô¤¹¤ë¥á¥½¥Ã¥É¤ËÌá¤êÃͤ¬¤Ê¤±¤ì¤Ð LINQ ¤Î ForEach ¥á¥½¥Ã¥É¤òÍøÍѤ¹¤ë¤È£±¹Ô¤Çµ½Ò¤Ç¤¤ë ¡¡¡¡¡¡¡¡charaList.ForEach(chara => chara.SetUpChara()); GetChara<CharaAttack>()?.PrepareAttackRepeatedly(); ¡¡¡¡} /// <summary> /// List Æâ¤Ë¤¢¤ë»ØÄꤷ¤¿¥¯¥é¥¹¤ò¼èÆÀ /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> private T GetChara<T>() where T : class, IChara { ¡¡¡¡¡¡¡¡// LINQ ¤ò»È¤ï¤Ê¤¤°ìÈÌŪ¤Ê½èÍý foreach (var chara in charaList) { if (chara is T typedChara) { return typedChara; } } return null; ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¢ // ¾åµ¤Î foreach Éôʬ¤Ï¡¢LINQ ¤Î OfType ¥á¥½¥Ã¥É¤È FirstOrDefault ¥á¥½¥Ã¥É¤òÍøÍѤ¹¤ë¤È£±¹Ô¤Çµ½Ò¤Ç¤¤ë return charaList.OfType<T>().FirstOrDefault(); } void Update() { // ¥¿¥Ã¥×ÆþÎÏ if (Input.GetMouseButton(0)) { // ¥¿¥Ã¥×¤·¤¿°ÌÃÖ¤«¤é¥ï¡¼¥ë¥ÉºÂɸ¼èÆÀ Vector2 tapPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); // °ÜÆ°¤È¥¢¥Ë¥áƱ´ü GetChara<CharaMove>()?.Move(tapPos); // ¸þ¤¤Î¹¹¿· UpdateDirection(tapPos); // °ÜÆ°¸þ¤¤È¥¢¥Ë¥á¤Î¸þ¤¤ÎƱ´ü GetChara<CharaAnime>()?.UpdateAnimation(direction); } GetChara<CharaAttack>()?.UpdateAttackDirection(direction); // ¹¶·â¤Î°ì»þÄä»ß¤ÈºÆ³« if (Input.GetMouseButtonDown(1)) { // ¹¶·â¤Î°ì»þÄä»ß¡¦ºÆ³«¤òÀÚ¤êÂؤ¨¤ë GetChara<CharaAttack>()?.ToggleAttack(); } } /// <summary> /// ¸þ¤¤Î¹¹¿· /// </summary> /// <param name="newPos"></param> private void UpdateDirection(Vector2 newPos) { direction = (newPos - (Vector2)transform.position).normalized; } }
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- ¥×¥í¥°¥é¥ß¥ó¥°
¥³¥á¥ó¥È¤ò¤«¤¯