PhotonNetWork.InstantiatePrefab()でDIしたい時
PUN2はNetworkObjectをInstantiateする時
PhotonNetwork.Instantiate(string prefabName, Vector3 position, Quaternion rotation)
を使わなければならない.
prefabName
には Assets/Resources以下のパスを入れる.
Resourcesを使いたくない時
IPunPrefabPool
を実装して独自のプールを使う. この中で
GameObject.Instantiate()
とかすれば Prefab から Instantiate できる.
public class HogePool : MonoBehaviour, IPunPrefabPool {
public void Awake() {
DontDestroyOnLoad(this);
PhotonNetwork.PrefabPool = this;
}
public GameObject Instantiate(string prefabId, Vector3 position, Quaternion rotation) {}
public void Destroy(GameObject gameObject) {}
}DIしたいとき
Zenjectは動的にMonoBehaviourのプロパティ等をInjectしたい時
DIContainer.InstantiatePrefab()
等を使う必要がある(または、FactoryつくってMethod Injection?)
がNetworkObjectとしてInstantiateさせつつDIもしたい場合はこの方法は使えないのでCustomPool内で
_container.InjectGameObject()
を使う
public class HogePool : MonoBehaviour, IPunPrefabPool {
[Inject]
private DiContainer _container;
public void Awake() {
DontDestroyOnLoad(this);
PhotonNetwork.PrefabPool = this;
}
public GameObject Instantiate(string prefabId, Vector3 position, Quaternion rotation) {
// instantiate(省略)
var go = XXXPool.ReUse(prefab);
// 明示的にInject
_container.InjectGameObject(go);
go.SetActive(false);
return go;
}
public void Destroy(GameObject gameObject) {}
}Poolで使うPrefabの中のInjectはPunPrefabPool.Awake()時にDIContainerにBindされてないと解決できずエラーとなるので注意(Project Context等でBindしておくとか)