facebook twitter hatena line email

「Unity/photon/pun1/位置同期」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(サンプル手順)
行2: 行2:
 
#ヒエラルキーにCubeを作成
 
#ヒエラルキーにCubeを作成
 
#CubeにAddComponentをしてPhotonViewを追加
 
#CubeにAddComponentをしてPhotonViewを追加
 +
#Cubeに以下CubeScript.csも割り当てる。
 
#CubeのInspectorのPhotonViewのObserverdOptionにCubeを指定する
 
#CubeのInspectorのPhotonViewのObserverdOptionにCubeを指定する
 
#ヒエラルキーのCubeをプロジェクトのResourcesにプレハブとして追記する(ドラッグ)
 
#ヒエラルキーのCubeをプロジェクトのResourcesにプレハブとして追記する(ドラッグ)
行7: 行8:
 
  void OnJoinedRoom() {
 
  void OnJoinedRoom() {
 
     GameObject cube = PhotonNetwork.Instantiate ("Cube", new Vector3 (0.0f, 0.0f, 0.0f), Quaternion.Euler(Vector3.zero),0);
 
     GameObject cube = PhotonNetwork.Instantiate ("Cube", new Vector3 (0.0f, 0.0f, 0.0f), Quaternion.Euler(Vector3.zero),0);
 +
}
 +
 +
CubeScript.cs
 +
using System.Collections;
 +
using System.Collections.Generic;
 +
using UnityEngine;
 +
public class CubeScript : MonoBehaviour {
 +
void Start () {
 +
if (GetComponent<PhotonView> ().isMine) {
 +
GetComponent<Renderer> ().material.color = Color.red;
 +
}
 +
}
 +
void OnMouseDrag()
 +
{
 +
Debug.Log ("ownerId=" + GetComponent<PhotonView> ().ownerId);
 +
Debug.Log ("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;
 +
}
 +
}
 
  }
 
  }
  

2017年10月17日 (火) 01:27時点における版

サンプル手順

  1. ヒエラルキーにCubeを作成
  2. CubeにAddComponentをしてPhotonViewを追加
  3. Cubeに以下CubeScript.csも割り当てる。
  4. CubeのInspectorのPhotonViewのObserverdOptionにCubeを指定する
  5. ヒエラルキーのCubeをプロジェクトのResourcesにプレハブとして追記する(ドラッグ)
  6. 部屋入室時に以下コードを実行
void OnJoinedRoom() {
    GameObject cube = PhotonNetwork.Instantiate ("Cube", new Vector3 (0.0f, 0.0f, 0.0f), Quaternion.Euler(Vector3.zero),0);
}

CubeScript.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeScript : MonoBehaviour {
	void Start () {
		if (GetComponent<PhotonView> ().isMine) {
			GetComponent<Renderer> ().material.color = Color.red;
		}
	}
	void OnMouseDrag()
	{
		Debug.Log ("ownerId=" + GetComponent<PhotonView> ().ownerId);
		Debug.Log ("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;
		}
	}
}

注意

以下コードがfalseになっていると離脱後もインスタンスが残るので注意。

PhotonNetwork.autoCleanUpPlayerObjects = true;

参考

http://sleepnel.hatenablog.com/entry/2016/06/09/120200

http://www.urablog.xyz/entry/2016/09/19/232345