'PlayerControls.photonView' 由于其保护级别而无法访问
'PlayerControls.photonView' is inaccessible due to its protection level
需要修复什么?
我在浪费时间找错误,但都不成功,我该怎么办?
Error: Assets\MapController.cs(54,31): error CS0122: 'PlayerControls.photonView' is inaccessible due to its protection level
using ExitGames.Client.Photon;
using Photon.Pun;
using Photon.Realtime;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MapController : MonoBehaviour, IOnEventCallback
{
public GameObject CellPrefab;
private GameObject[,] cells;
private List<PlayerControls> players = new List<PlayerControls>();
private double lastTickTime;
public void AddPlayer(PlayerControls player)
{
players.Add(player);
cells[player.GamePosition.x, player.GamePosition.y].SetActive(false);
}
private void Start()
{
cells = new GameObject[20, 10];
for (int x = 0; x < cells.GetLength(0); x++)
{
for (int y = 0; y < cells.GetLength(1); y++)
{
cells[x, y] = Instantiate(CellPrefab, new Vector3(x, y), Quaternion.identity, transform);
}
}
}
private void Update()
{
if (PhotonNetwork.Time > lastTickTime + 1 &&
PhotonNetwork.IsMasterClient &&
PhotonNetwork.CurrentRoom.PlayerCount == 2)
{
Vector2Int[] directions = players
.OrderBy(p=>p.photonView.Owner.ActorNumber)
.Select(p=>p.Direction)
.ToArray();
RaiseEventOptions options = new RaiseEventOptions { Receivers = ReceiverGroup.Others };
SendOptions sendOptions = new SendOptions { Reliability = true };
PhotonNetwork.RaiseEvent(42, directions, options, sendOptions);
PerformTick(directions);
}
}
public void OnEvent(EventData photonEvent)
{
switch (photonEvent.Code)
{
case 42:
Vector2Int[] directions = (Vector2Int[]) photonEvent.CustomData;
PerformTick(directions);
break;
}
}
private void PerformTick(Vector2Int[] directions)
{
if (players.Count != directions.Length) return;
int i = 0;
foreach (var player in players.OrderBy(p=>p.photonView.Owner.ActorNumber))
{
player.Direction = directions[i++];
player.GamePosition += player.Direction;
if (player.GamePosition.x < 0) player.GamePosition.x = 0;
if (player.GamePosition.y < 0) player.GamePosition.y = 0;
if (player.GamePosition.x >= cells.GetLength(0)) player.GamePosition.x = cells.GetLength(0)-1;
if (player.GamePosition.y >= cells.GetLength(1)) player.GamePosition.y = cells.GetLength(1)-1;
cells[player.GamePosition.x, player.GamePosition.y].SetActive(false);
}
lastTickTime = PhotonNetwork.Time;
}
}
PlayerControls.cs
using ExitGames.Client.Photon;
using Photon.Pun;
using Photon.Realtime;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerControls : MonoBehaviour, IPunObservable
{
private PhotonView photonView;
private SpriteRenderer spriteRenderer;
public Vector2Int Direction;
public Vector2Int GamePosition;
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(Direction);
}
else
{
Direction = (Vector2Int)stream.ReceiveNext();
}
}
private void Start()
{
photonView = GetComponent<PhotonView>();
spriteRenderer = GetComponent<SpriteRenderer>();
GamePosition = new Vector2Int((int)transform.position.x, (int)transform.position.y);
FindObjectOfType<MapController>().AddPlayer(this);
}
private void Update()
{
if (photonView.IsMine)
{
if (Input.GetKey(KeyCode.LeftArrow)) Direction = Vector2Int.left;
if (Input.GetKey(KeyCode.RightArrow)) Direction = Vector2Int.right;
if (Input.GetKey(KeyCode.UpArrow)) Direction = Vector2Int.up;
if (Input.GetKey(KeyCode.DownArrow)) Direction = Vector2Int.down;
if (Direction == Vector2Int.left) spriteRenderer.flipX = true;
if (Direction == Vector2Int.right) spriteRenderer.flipX = false;
transform.position = Vector3.Lerp(transform.position, (Vector2)GamePosition, Time.deltaTime * 3);
}
}
}
PhotonView 是 class 的 属性,声明为受保护或私有。
如果您处理代码,则需要声明它public。如果没有,那么您需要找到某种其他方式来访问它。
分享一些代码 class 来帮助你更多。
在 PlayerControls.cs
中,将 private PhotonView photonView;
更改为 public PhotonView photonView;
。
此问题是 属性 声明中使用的 private
关键字。这将不允许您访问 PlayerControls.cs
class 之外的 photonView
属性。将其声明为 public
可解决此问题。
需要修复什么? 我在浪费时间找错误,但都不成功,我该怎么办?
Error: Assets\MapController.cs(54,31): error CS0122: 'PlayerControls.photonView' is inaccessible due to its protection level
using ExitGames.Client.Photon;
using Photon.Pun;
using Photon.Realtime;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MapController : MonoBehaviour, IOnEventCallback
{
public GameObject CellPrefab;
private GameObject[,] cells;
private List<PlayerControls> players = new List<PlayerControls>();
private double lastTickTime;
public void AddPlayer(PlayerControls player)
{
players.Add(player);
cells[player.GamePosition.x, player.GamePosition.y].SetActive(false);
}
private void Start()
{
cells = new GameObject[20, 10];
for (int x = 0; x < cells.GetLength(0); x++)
{
for (int y = 0; y < cells.GetLength(1); y++)
{
cells[x, y] = Instantiate(CellPrefab, new Vector3(x, y), Quaternion.identity, transform);
}
}
}
private void Update()
{
if (PhotonNetwork.Time > lastTickTime + 1 &&
PhotonNetwork.IsMasterClient &&
PhotonNetwork.CurrentRoom.PlayerCount == 2)
{
Vector2Int[] directions = players
.OrderBy(p=>p.photonView.Owner.ActorNumber)
.Select(p=>p.Direction)
.ToArray();
RaiseEventOptions options = new RaiseEventOptions { Receivers = ReceiverGroup.Others };
SendOptions sendOptions = new SendOptions { Reliability = true };
PhotonNetwork.RaiseEvent(42, directions, options, sendOptions);
PerformTick(directions);
}
}
public void OnEvent(EventData photonEvent)
{
switch (photonEvent.Code)
{
case 42:
Vector2Int[] directions = (Vector2Int[]) photonEvent.CustomData;
PerformTick(directions);
break;
}
}
private void PerformTick(Vector2Int[] directions)
{
if (players.Count != directions.Length) return;
int i = 0;
foreach (var player in players.OrderBy(p=>p.photonView.Owner.ActorNumber))
{
player.Direction = directions[i++];
player.GamePosition += player.Direction;
if (player.GamePosition.x < 0) player.GamePosition.x = 0;
if (player.GamePosition.y < 0) player.GamePosition.y = 0;
if (player.GamePosition.x >= cells.GetLength(0)) player.GamePosition.x = cells.GetLength(0)-1;
if (player.GamePosition.y >= cells.GetLength(1)) player.GamePosition.y = cells.GetLength(1)-1;
cells[player.GamePosition.x, player.GamePosition.y].SetActive(false);
}
lastTickTime = PhotonNetwork.Time;
}
}
PlayerControls.cs
using ExitGames.Client.Photon;
using Photon.Pun;
using Photon.Realtime;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerControls : MonoBehaviour, IPunObservable
{
private PhotonView photonView;
private SpriteRenderer spriteRenderer;
public Vector2Int Direction;
public Vector2Int GamePosition;
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(Direction);
}
else
{
Direction = (Vector2Int)stream.ReceiveNext();
}
}
private void Start()
{
photonView = GetComponent<PhotonView>();
spriteRenderer = GetComponent<SpriteRenderer>();
GamePosition = new Vector2Int((int)transform.position.x, (int)transform.position.y);
FindObjectOfType<MapController>().AddPlayer(this);
}
private void Update()
{
if (photonView.IsMine)
{
if (Input.GetKey(KeyCode.LeftArrow)) Direction = Vector2Int.left;
if (Input.GetKey(KeyCode.RightArrow)) Direction = Vector2Int.right;
if (Input.GetKey(KeyCode.UpArrow)) Direction = Vector2Int.up;
if (Input.GetKey(KeyCode.DownArrow)) Direction = Vector2Int.down;
if (Direction == Vector2Int.left) spriteRenderer.flipX = true;
if (Direction == Vector2Int.right) spriteRenderer.flipX = false;
transform.position = Vector3.Lerp(transform.position, (Vector2)GamePosition, Time.deltaTime * 3);
}
}
}
PhotonView 是 class 的 属性,声明为受保护或私有。
如果您处理代码,则需要声明它public。如果没有,那么您需要找到某种其他方式来访问它。
分享一些代码 class 来帮助你更多。
在 PlayerControls.cs
中,将 private PhotonView photonView;
更改为 public PhotonView photonView;
。
此问题是 属性 声明中使用的 private
关键字。这将不允许您访问 PlayerControls.cs
class 之外的 photonView
属性。将其声明为 public
可解决此问题。