码迷,mamicode.com
首页 > 编程语言 > 详细

搭建unity客户端

时间:2018-02-11 00:09:36      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:exit   ase   send   div   请求   stat   null   return   generic   

1.新建个unity的项目ChatClient

 

2.在unityMain Camera下挂载个脚本PhotonServerEngine做为与服务器端通信的脚本

 

3.PhotonServerEngine脚本中添加引用Photon3Unity3D.dll

路径:C:\Program Files\Photon Server\lib

Photon3DotNet.dll   //普通的客户端程序

Photon3Unity3D.dll  //unity的客户端程序

 

4.PhotonServerEngine下编写具体的代码

 

using UnityEngine;

using System.Collections;

using ExitGames.Client.Photon;

using System;

using System.Collections.Generic;

//让当前类继承IPhotonPeerListener,用于接收服务器的信息

public class PhotonServerEngine : MonoBehaviour, IPhotonPeerListener

{

    private PhotonPeer peer;

    private bool bConnet = false;

 

    void Start()

    {

        //实例化一个PhotonPeer,把当前类当成接收对象,使用TCP协议

        peer = new PhotonPeer(this, ConnectionProtocol.Tcp);

        //连接本地服务器 IP127.0.0.1  TCP端口号:4530

        //通过PhotonServer.config文件,查找TCPListeners获取TCP端口号

        peer.Connect("127.0.0.1:4530", "ChatServer");

    }

    void Update()

    {

        //当每一帧时调用Service检查消息队列中的请求,并且发送请求

        peer.Service();

    }

    void OnGUI()

    {

        if (bConnet)

        {

            if (GUILayout.Button("Send Operation"))

            {

                //与服务器端进行通信,发起请求

                Dictionary<byte, object> dict = new Dictionary<byte, object>();

                dict.Add(1, "UserName");

                dict.Add(2, "UserPassWord");

                peer.OpCustom(1, dict, true);

            }

        }

    }

 

    //当返回调试信息时被调用

    public void DebugReturn(DebugLevel level, string message)

    {

 

    }

    //当有新消息事件时被调用

    public void OnEvent(EventData eventData)

    {

        

    }

  //当服务器端响应时被调用

  public void OnOperationResponse(OperationResponse operationResponse)

    {

        //接收服务器返回的数据

        object ob_1 = null;

        object ob_2 = null;

        operationResponse.Parameters.TryGetValue(1, out ob_1);

        operationResponse.Parameters.TryGetValue(2, out ob_2);

        Debug.Log("UserName = " + ob_1.ToString() +"|" +  "PassWord = " + ob_2.ToString());

    }

    //当状态改变时调用

    public void OnStatusChanged(StatusCode statusCode)

    {

        switch(statusCode)

        {

            case StatusCode.Connect:

                bConnet = true;

                Debug.Log("connect Succes");

                break;

        }

    }

}

5.必须保证Server应用己启动,未启动的话先运行PhotonControl.exe,选择default->start as appliaction, 如果正常显示蓝色图标,出现异常显示灰色图标.

6.最后运行unity, start方法被调用连接本地服务器端的TCP端口,点击GUI按钮,发起请求.

搭建unity客户端

标签:exit   ase   send   div   请求   stat   null   return   generic   

原文地址:https://www.cnblogs.com/fzxiaoyi/p/8440013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!