'PlayerControls.photonView'由于其保护级别而无法访问

问题描述 投票:0回答:2

需要解决什么?我在浪费时间寻找错误,但都失败了,我该怎么办?

错误:Assets \ MapController.cs(54,31):错误CS0122:由于其保护级别,无法访问'PlayerControls.photonView'

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);
        }
    }
}
c# unity3d photon
2个回答
0
投票

PlayerControls.cs中,将private PhotonView photonView;更改为public PhotonView photonView;

此问题是属性声明中使用的private关键字。这将不允许您访问photonView类之外的PlayerControls.cs属性。将其声明为public可解决此问题。


0
投票

PhotonView是该类的一个属性,它被声明为protected或private。

如果处理代码,则需要将其声明为公共。如果没有,那么您需要找到某种其他方式来访问它。

共享课程的一些代码,以帮助您更多。

© www.soinside.com 2019 - 2024. All rights reserved.