P U N

1.继承MonoBehaviourPunCallbacks

2.重写4个方法OnConnectedToMaster,OnDisconnected(DisconnectCause cause),OnJoinRandomFailed,OnJoinedRoom

using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
*创建者:
*创建时间:
*描述:
*版本:
*/
public class _4_6 : MonoBehaviourPunCallbacks
{
    bool isConnect=false;//连接服务器状态
    // Start is called before the first frame update
    void Start()
    {
        PhotonNetwork.AutomaticallySyncScene=true;//同步所有客户端
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void Connect()//连接方法
    {
        if (PhotonNetwork.IsConnected)
        {
            print("连接成功");
            PhotonNetwork.JoinRandomRoom();//随机加入房间
        }
        else
        {
            print("连接失败");
            isConnect=PhotonNetwork.ConnectUsingSettings();//连接服务器
            print(isConnect);
        }
    }
    public override void OnConnectedToMaster()//连接上主客户端(连接上服务器后自动执行(一直执行直到连接上主客户端))
    {
        base.OnConnectedToMaster();
        print("连接上主客户端");
        if (isConnect)
        {
            PhotonNetwork.JoinRandomRoom();//随机加入房间
        }
    }
    public override void OnDisconnected(DisconnectCause cause)//连接主客户端失败
    {
        base.OnDisconnected(cause);
        print("连接主客户端失败");
    }
    public override void OnJoinRandomFailed(short returnCode, string message)//随机加入房间失败
    {
        base.OnJoinRandomFailed(returnCode, message);
        print("随机加入房间失败");
        PhotonNetwork.CreateRoom("PUBG",new RoomOptions() { MaxPlayers=100});//创建房间
        //RoomOptions ro=new RoomOptions();
        //ro.MaxPlayers=100;
        //PhotonNetwork.CreateRoom("PUBG",ro);
    }
    public override void OnJoinedRoom()//加入房间成功
    {
        base.OnJoinedRoom();
        print("加入房间成功");
        if (PhotonNetwork.CurrentRoom.PlayerCount==1)//房间人数
        {
            PhotonNetwork.LoadLevel("shoot");//跳转场景
        }
    }
}

3.非玩家本人不能操作对应的游戏物体

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
*创建者:
*创建时间:
*描述:
*版本:
*/
public class _4_6_Player : MonoBehaviourPunCallbacks
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

        if (photonView.IsMine==false&&PhotonNetwork.IsConnected==true)//若非玩家本人在操作客户端,其余操作不执行
        {
            return;
        }
        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");
        Vector3 dir = new Vector3(x,y,0);
        if (Mathf.Abs(x)>0||Mathf.Abs(y)>0)
        {
            transform.Translate(dir*Time.deltaTime*5);
        }
    }
}

4.主客户端生成游戏资源,防止游戏资源生成引起冲突(同步客户端进行资源共享)

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
*创建者:
*创建时间:
*描述:
*版本:
*/
public class _4_6_GameManager : MonoBehaviour
{
    GameObject player, enemy;
    // Start is called before the first frame update
    void Start()
    {
        player = Resources.Load("player1");
        enemy = Resources.Load("enemy");
        PhotonNetwork.Instantiate(player.name,new Vector3(0,1,0),Quaternion.identity);
        StartCoroutine(IE());
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    IEnumerator IE()
    {
        if (PhotonNetwork.IsMasterClient)//主客户端(可以作为房主/所有场景资源由该客户端产生,防止不同客户端产生相同游戏资源出现冲突)
        {
            while (true)
            {
                float x = Random.Range(-9.5f, 9.5f);
                PhotonNetwork.Instantiate(enemy.name, new Vector3(x, enemy.transform.position.y, enemy.transform.position.z), Quaternion.identity);
                yield return new WaitForSeconds(1f);
            }
        }
        
    }
}

PS:1.所有游戏资源通过动态加载获取        2.克隆销毁 使用PhotonNetwork调用

5.为预制件添加PhotonView脚本

P U N_第1张图片

        5.1  synchronization(同步方式):Reliable(实时)

        5.2  observable search(观察搜索模式):Manual(手动) 

        5.3  切换为手动模式后,将该预制件Transform拖拽至Observed Components

       

你可能感兴趣的:(c#,unity)