「Unity/photon/pun2/位置同期」の版間の差分
ナビゲーションに移動
検索に移動
編集の要約なし |
|||
| 12行目: | 12行目: | ||
*PhotonAvater.sc | *PhotonAvater.sc | ||
<pre> | <pre> | ||
using UnityEngine; | using UnityEngine; | ||
using | using Photon.Pun; | ||
public class PhotonAvater : MonoBehaviourPunCallbacks { | public class PhotonAvater : MonoBehaviourPunCallbacks, IPunInstantiateMagicCallback | ||
{ | |||
public string str = "guest1"; | public string str = "guest1"; | ||
void Awake() | void Awake() | ||
| 25行目: | 24行目: | ||
{ | { | ||
GetComponent<Renderer>().material.color = Color.red; | GetComponent<Renderer>().material.color = Color.red; | ||
} | } else { | ||
// もし操作するクラスをaddしてた場合は、自分以外のときは無効とする | |||
GetComponent<CharacterControl>().enabled = false; | |||
} | |||
} | |||
void IPunInstantiateMagicCallback.OnPhotonInstantiate(PhotonMessageInfo info) | |||
{ | |||
if (info.Sender.IsLocal) | |||
{ | |||
Debug.Log("自身が生成"); | |||
} | |||
else | |||
{ | |||
Debug.Log("他人が生成"); | |||
} | |||
info.Sender.TagObject = this.gameObject; | |||
} | |||
} | } | ||
</pre> | </pre> | ||
公式:https://doc.photonengine.com/ja-jp/pun/v2/gameplay/instantiation | 公式:https://doc.photonengine.com/ja-jp/pun/v2/gameplay/instantiation | ||
2022年12月7日 (水) 11:06時点における版
アバター風にしてみる
- Cube作成。ここでは"Avatar"という名前に変更した。
- AvatarにBoxColliderが追加されていることを確認
- AvatarのAddComponentにPhotonAvaterとPhotonViewとPhotonTransformViewを追加
- PhotonTransformViewに、PositionとRotationが、追加されてることを確認。
- Assets下に、Resourcesディレクトリを追加して、AvatarをResourcesに追加
- 以下スクリプトでPrefabのAvaterをロードする。
- PhotonManager.cs
GameObject avater = PhotonNetwork.Instantiate("Avatar", Vector3.zero, Quaternion.Euler(Vector3.zero),0);
- PhotonAvater.sc
using UnityEngine;
using Photon.Pun;
public class PhotonAvater : MonoBehaviourPunCallbacks, IPunInstantiateMagicCallback
{
public string str = "guest1";
void Awake()
{
this.name = "Avater" + GetComponent<PhotonView>().InstantiationId;
if (GetComponent<PhotonView>().IsMine)
{
GetComponent<Renderer>().material.color = Color.red;
} else {
// もし操作するクラスをaddしてた場合は、自分以外のときは無効とする
GetComponent<CharacterControl>().enabled = false;
}
}
void IPunInstantiateMagicCallback.OnPhotonInstantiate(PhotonMessageInfo info)
{
if (info.Sender.IsLocal)
{
Debug.Log("自身が生成");
}
else
{
Debug.Log("他人が生成");
}
info.Sender.TagObject = this.gameObject;
}
}
公式:https://doc.photonengine.com/ja-jp/pun/v2/gameplay/instantiation