こんにちは。
UniRxのObjectPoolを使ってシューティングゲームを作ろうと思いテストコードを書いてみたのですが、使っているオブジェクトを無理矢理持ってきて使ってしまっています。
なぜこうなっているかが良く解りません、どうかよろしくお願いします。
ObjectPoolの宣言側です。
using System.Collections; using System.Collections.Generic; using UniRx.Toolkit; using UnityEngine; public class TestBulletPool : ObjectPool<TestPlayerBullet> { //private field private readonly Transform _PerentTransform; private readonly GameObject _Bullet; //public field //Method /// <summary> /// /// </summary> /// <param name="PerentTransform">生成されるオブジェクトの親オブジェクト。インスペクター散らかし防止用</param> /// <param name="BulletPrefab">実際に打ち出される弾。TestPlayerBulletをコンポーネントにつけたものでないといけない</param> public TestBulletPool(Transform PerentTransform,GameObject BulletPrefab) { _PerentTransform = PerentTransform; _Bullet = BulletPrefab; } protected override TestPlayerBullet CreateInstance() { var k = GameObject.Instantiate(_Bullet); k.transform.parent = _PerentTransform; return k.GetComponent<TestPlayerBullet>(); } }
Poolするオブジェクトのスクリプトです。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UniRx; using UniRx.Triggers; using System; [RequireComponent(typeof(Rigidbody))] public class TestPlayerBullet : MonoBehaviour { //private field private Rigidbody rb; //public field public int damege; //Method void Start() { rb = GetComponent<Rigidbody>(); rb.AddForce(transform.forward * 10, ForceMode.Impulse); } public void SetPosition(Vector3 position) { transform.position = position; } public IObservable<Collider> CollisionToDestroy() { return this.OnTriggerEnterAsObservable(); } }
オブジェクトを生成するスクリプトです。
using System.Collections; using System.Collections.Generic; using UnityEngine; using PlayerStatus.Domain.UseCase; using UniRx; using UniRx.Triggers; using System; using UniRx.Toolkit; using System.Linq; public class TestShoot : MonoBehaviour { IShootInput shootInput; [SerializeField] int BulletLevel; [SerializeField] float _LevelOneShootInterval; //[SerializeField] //float _LevelTwoShootInterval; //[SerializeField] //float _LevelThreeShootInterval; //[SerializeField] //float _LevelFourShootInterval; [SerializeField] GameObject bullet; TestBulletPool bulletPool; string DestroyedGameObject(GameObject gameObject) { if(gameObject == null) { return "null"; } return gameObject.name; } void Awake() { bulletPool = new TestBulletPool(transform, bullet); List<TestPlayerBullet> list = new List<TestPlayerBullet>(); //this.UpdateAsObservable() // .Where(_ => BulletLevel == 1) // .ThrottleFirst(TimeSpan.FromSeconds(_LevelOneShootInterval)) // .Subscribe(_ => // { // var pool = bulletPool.Rent(); // pool.CollisionToDestroy(gameObject.transform.position + new Vector3(0,0,1)) // .Subscribe(x => // { // bulletPool.Return(pool); // }); // }); Observable .Interval(TimeSpan.FromSeconds(_LevelOneShootInterval)) .Subscribe(x => { var pool = bulletPool.Rent(); pool.SetPosition(gameObject.transform.position + new Vector3(0, 0, 1)); list.Add(pool); }); list.ObserveEveryValueChanged(x => x.Count) .Skip(1) .Subscribe(_ => { var peeked = list.Last(); peeked.CollisionToDestroy() .Subscribe(x => { bulletPool.Return(peeked); list.Remove(peeked); Debug.Log(DestroyedGameObject(peeked.gameObject)); }); }); } }
回答1件
あなたの回答
tips
プレビュー