facebook twitter hatena line email

「Unity/photon/pun1/変数同期/アバター風」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
 
(同じ利用者による、間の3版が非表示)
行7: 行7:
 
#ヒエラルキートップにCanvas/InputTextとCanvas/Buttonを追加
 
#ヒエラルキートップにCanvas/InputTextとCanvas/Buttonを追加
 
#MemberのObserverdOptionにMemberScriptとPhotonTransformViewを追加
 
#MemberのObserverdOptionにMemberScriptとPhotonTransformViewを追加
#MemberをPrefubに追加
+
#MemberをPrefabに追加
#以下スクリプトでPrefubのMemberを呼び出す
+
#以下スクリプトでPrefabのMemberを呼び出す
  
 
変数同期でx,y,str(名前)を同期した
 
変数同期でx,y,str(名前)を同期した
  
 
#PhotonManager.cs
 
#PhotonManager.cs
  GameObject member = PhotonNetwork.Instantiate ("Member", new Vector3 (0.0f, 0.0f, 0.0f),
+
  GameObject member = PhotonNetwork.Instantiate ("Member", Vector3.zero,
 
  Quaternion.Euler(Vector3.zero),0);
 
  Quaternion.Euler(Vector3.zero),0);
 
  member.name = "Member"+cube.GetComponent<PhotonView>().ownerId;
 
  member.name = "Member"+cube.GetComponent<PhotonView>().ownerId;
行35: 行35:
 
}
 
}
 
void Awake() {
 
void Awake() {
this.name = "Member"+GetComponent<PhotonView> ().ownerId;
+
this.name = "Member"+GetComponent<PhotonView>().ownerId;
if (GetComponent<PhotonView> ().isMine) {
+
if (GetComponent<PhotonView>().isMine) {
GetComponent<Renderer> ().material.color = Color.red;
+
GetComponent<Renderer>().material.color = Color.red;
 
}
 
}
 
GameObject obj = GameObject.Find ("/Canvas/Button");
 
GameObject obj = GameObject.Find ("/Canvas/Button");
if (GetComponent<PhotonView> ().isMine) {
+
if (GetComponent<PhotonView>().isMine) {
obj.GetComponent<Button> ().onClick.AddListener (OnClick);
+
obj.GetComponent<Button>().onClick.AddListener (OnClick);
 
}
 
}
 
SetNameText ();
 
SetNameText ();
行47: 行47:
 
void OnMouseDrag()
 
void OnMouseDrag()
 
{
 
{
Debug.Log ("member ownerId=" + GetComponent<PhotonView> ().ownerId);
+
Debug.Log ("member ownerId=" + GetComponent<PhotonView>().ownerId);
Debug.Log ("member isMine=" + GetComponent<PhotonView> ().isMine);
+
Debug.Log ("member isMine=" + GetComponent<PhotonView>().isMine);
 
if (GetComponent<PhotonView>().isMine) {
 
if (GetComponent<PhotonView>().isMine) {
GetComponent<Renderer> ().material.color = Color.red;
+
GetComponent<Renderer>().material.color = Color.red;
 
Vector3 objectPointInScreen = Camera.main.WorldToScreenPoint (this.transform.position);
 
Vector3 objectPointInScreen = Camera.main.WorldToScreenPoint (this.transform.position);
 
Vector3 mousePointInScreen = new Vector3 (Input.mousePosition.x,
 
Vector3 mousePointInScreen = new Vector3 (Input.mousePosition.x,
行62: 行62:
 
void OnClick()
 
void OnClick()
 
{
 
{
if (GetComponent<PhotonView> ().isMine) {
+
if (GetComponent<PhotonView>().isMine) {
 
GameObject objstr = GameObject.Find ("/Canvas/InputField/Text");
 
GameObject objstr = GameObject.Find ("/Canvas/InputField/Text");
str = objstr.transform.GetComponent<Text> ().text;
+
str = objstr.transform.GetComponent<Text>().text;
 
}
 
}
 
SetNameText ();
 
SetNameText ();
行73: 行73:
 
GameObject objstr = GameObject.Find(this.name + "/Canvas/Text");
 
GameObject objstr = GameObject.Find(this.name + "/Canvas/Text");
 
try {
 
try {
objstr.transform.GetComponent<Text> ().text = str;
+
objstr.transform.GetComponent<Text>().text = str;
 
} catch (NullReferenceException ex) {
 
} catch (NullReferenceException ex) {
 
}
 
}
行99: 行99:
 
</pre>
 
</pre>
  
PhotonNetwork.Instantiateで作ったGameObjectのInstanceは、photonを経由して、相手から送られてくるっぽい。
+
PhotonNetwork.Instantiateで作ったGameObjectのInstanceは、オブジェクト名の文字列だけ、photonを経由して、相手から送られてくるっぽい。

2022年12月7日 (水) 16:27時点における最新版

変数同期のみでアバター風を作成する

  1. unity/photon/pun1/変数同期/シンプル [ショートカット] の手順の続き
  2. Memberの下にCanvasを追加
  3. MemberのCanvasの下にTextを追加
  4. Memberの下のCanvasのRenderModeをWorldSpaceへ(これでMemberにText座標がくっつくようになる)
  5. MemberにBoxColliderが追加する
  6. ヒエラルキートップにCanvas/InputTextとCanvas/Buttonを追加
  7. MemberのObserverdOptionにMemberScriptとPhotonTransformViewを追加
  8. MemberをPrefabに追加
  9. 以下スクリプトでPrefabのMemberを呼び出す

変数同期でx,y,str(名前)を同期した

  1. PhotonManager.cs
GameObject member = PhotonNetwork.Instantiate ("Member",  Vector3.zero,
Quaternion.Euler(Vector3.zero),0);
member.name = "Member"+cube.GetComponent<PhotonView>().ownerId;
  1. MemberScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class MemberScript : Photon.MonoBehaviour {
	public string str = "guest";
	public float x = 0f;
	public float y = 0f;
	// Use this for initialization
	void Start () {
	}
	// Update is called once per frame
	void Update () {
	}
	void Awake() {
		this.name = "Member"+GetComponent<PhotonView>().ownerId;
		if (GetComponent<PhotonView>().isMine) {
			GetComponent<Renderer>().material.color = Color.red;
		}
		GameObject obj = GameObject.Find ("/Canvas/Button");
		if (GetComponent<PhotonView>().isMine) {
			obj.GetComponent<Button>().onClick.AddListener (OnClick);
		}
		SetNameText ();
	}
	void OnMouseDrag()
	{
		Debug.Log ("member ownerId=" + GetComponent<PhotonView>().ownerId);
		Debug.Log ("member isMine=" + GetComponent<PhotonView>().isMine);
		if (GetComponent<PhotonView>().isMine) {
			GetComponent<Renderer>().material.color = Color.red;
			Vector3 objectPointInScreen = Camera.main.WorldToScreenPoint (this.transform.position);
			Vector3 mousePointInScreen = new Vector3 (Input.mousePosition.x,
				Input.mousePosition.y,
				objectPointInScreen.z);
			Vector3 mousePointInWorld = Camera.main.ScreenToWorldPoint (mousePointInScreen);
			mousePointInWorld.z = this.transform.position.z;
			this.transform.position = mousePointInWorld;
		}
	}
	void OnClick()
	{
		if (GetComponent<PhotonView>().isMine) {
			GameObject objstr = GameObject.Find ("/Canvas/InputField/Text");
			str = objstr.transform.GetComponent<Text>().text;
		}
		SetNameText ();
	}
	void SetNameText()
	{
		Debug.Log("name="+this.name);
		GameObject objstr = GameObject.Find(this.name + "/Canvas/Text");
		try {
			objstr.transform.GetComponent<Text>().text = str;
		} catch (NullReferenceException ex) {
		}
	}
	void OnPhotonSerializeView( PhotonStream stream, PhotonMessageInfo info )
	{
		if (stream.isWriting) {
			Debug.Log ("member isWriting="+str);
			//データの送信
			stream.SendNext(str);
			stream.SendNext(this.transform.position.x);
			stream.SendNext(this.transform.position.y);

		} else {
			Debug.Log ("member  isread="+str);
			//データの受信
			this.str = (string)stream.ReceiveNext();
			this.x = (float)stream.ReceiveNext();
			this.y = (float)stream.ReceiveNext();
			this.transform.position = new Vector3 (x, y, transform.position.z);
			SetNameText ();
		}
	}
}

PhotonNetwork.Instantiateで作ったGameObjectのInstanceは、オブジェクト名の文字列だけ、photonを経由して、相手から送られてくるっぽい。