Photon PUN2では、photonView.OwnerActorNrを使用して、オブジェクトのリストを作成し、プレイヤーIDに基づいてGameObjectを検索することができます。
方法
シーン内のすべてのPhotonViewコンポーネントを取得します。
取得したPhotonViewのリストから、photonView.OwnerActorNrが一致するものを検索します。
一致するPhotonViewのGameObjectを取得します。
以下のコードは、photonView.OwnerActorNrを使用してGameObjectを検索する方法を示しています。
using Photon.Pun;
using UnityEngine;
public class FindGameObjectByActorNr : MonoBehaviour
{
public GameObject FindGameObjectByOwnerActorNr(int targetActorNr)
{
PhotonView[] photonViews = GameObject.FindObjectsOfType<PhotonView>();
foreach (PhotonView photonView in photonViews)
{
if (photonView.OwnerActorNr == targetActorNr)
{
return photonView.gameObject;
}
}
return null;
}
}
このコードでは、FindGameObjectByOwnerActorNrメソッドを使用して、指定されたtargetActorNrに一致するGameObjectを取得できます。GameObjectが見つからない場合は、nullが返されます。
使い方
上記のFindGameObjectByActorNrクラスをシーン内の適切なGameObjectにアタッチします。
他のスクリプトからFindGameObjectByOwnerActorNrメソッドを呼び出すことで、指定したtargetActorNrに一致するGameObjectを取得できます。
FindGameObjectByActorNr findGameObjectByActorNr = FindObjectOfType<FindGameObjectByActorNr>();
int targetActorNr = 2; // 検索したいプレイヤーID
GameObject targetGameObject = findGameObjectByActorNr.FindGameObjectByOwnerActorNr(targetActorNr);
if (targetGameObject != null)
{
Debug.Log("Found GameObject: " + targetGameObject.name);
}
else
{
Debug.Log("GameObject not found");
}
この方法を使用することで、photonView.OwnerActorNrを利用してGameObjectを検索し、取得することができます。
Comments