首先去PhotonServer SDK下载服务器端SDK,需要登录的,就先注册一个账号吧.
解压出来是四个文件
deploy:主要存放photon的服务器控制程序和服务端Demo
doc:顾名思义,文档
lib:Photon类库,开发服务端需要引用的
src-server:服务端Demo源代码
今天搞一个客户端连接服务器最简单的程序,也算是hello world吧
客户端以Unity3d 为基础,hello world包括配置服务器,客户端,客户端连接服务器,客户端状态改变。
第一步:配置服务器端
打开deploy文件夹,看到包含了不同平台编译出的Photon目录,以“bin_”为前缀命名目录,选择你的电脑对应的文件夹打开,看到PhotonControl.exe,运行后,可以在windows右下角看到一个图标,点击图标可以看到photon服务器控制菜单,这个以后再做详细介绍.
打开visual stadio,新建项目,选择c# 类库,应用程序名字叫做MyServer.
完成后,把我们的Class1.cs,改名为MyApplication.cs,作为服务器端主类.然后在当前项目添加引用,链接到刚才提到的lib文件夹目录下,添加以下引用:
ExitGamesLibs.dll,
Photon.SocketServer.dll,
PhotonHostRuntimeInterfaces.dll
然后新建一个类:MyPeer.cs,写法如下:
- using System;
-
using System.Collections.Generic;
-
using Photon.SocketServer;
-
using PhotonHostRuntimeInterfaces;
-
namespace MyServer
- {
-
public class MyPeer : PeerBase
- {
-
public MyPeer(IRpcProtocol protocol,IPhotonPeer photonPeer)
-
: base(protocol, photonPeer)
- {
- }
-
protected
override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode,
string reasonDetail)
- {
- }
-
protected override
void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
- {
- }
- }
- }
using System;
using System.Collections.Generic;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
namespace MyServer
{
public class MyPeer : PeerBase
{
public MyPeer(IRpcProtocol protocol,IPhotonPeer photonPeer)
: base(protocol, photonPeer)
{
}
protected override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode, string reasonDetail)
{
}
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
}
}
}
接上,MyApplication.cs类这样写:
- using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using Photon.SocketServer;
-
namespace MyServer
- {
-
public class MyApplication : ApplicationBase
- {
-
protected override PeerBase CreatePeer(InitRequest initRequest)
- {
-
return new MyPeer(initRequest.Protocol, initRequest.PhotonPeer);
- }
-
protected
override void Setup()
- {
- }
-
protected override
void TearDown()
- {
- }
- }
- }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Photon.SocketServer;
namespace MyServer
{
public class MyApplication : ApplicationBase
{
protected override PeerBase CreatePeer(InitRequest initRequest)
{
return new MyPeer(initRequest.Protocol, initRequest.PhotonPeer);
}
protected override void Setup()
{
}
protected override void TearDown()
{
}
}
}
完成后,在解决方案资源管理器中选中当前项目,打开属性,选择生成选项卡,把输出路径改成bin\,然后就生成类库吧
复制当前项目下MyServer文件夹到deploy文件夹下,删除除了bin文件夹以外其他所有文件和文件夹,然后文本编辑器打开deploy\bin_Win64\PhotonServer.config配置文件(我的是win7 64位机器,就选择这个),添加以下配置:
- <Application
-
Name="MyServer"
-
BaseDirectory="MyServer"
-
Assembly="MyServer"
-
Type="MyServer.MyApplication"
-
EnableAutoRestart="true"
-
WatchFiles="dll;config"
-
ExcludeFiles="log4net.config">
- </Application>
<Application
Name="MyServer"
BaseDirectory="MyServer"
Assembly="MyServer"
Type="MyServer.MyApplication"
EnableAutoRestart="true"
WatchFiles="dll;config"
ExcludeFiles="log4net.config">
</Application>
这段代码放在<Default><Applications>放这里</Applications></Default>节点下面
Name:项目名字
BaseDirectory:根目录,deploy文件夹下为基础目录
Assembly:是在生成的类库中的bin目录下与我们项目名称相同的.dll文件的名字
Type:是主类的全称,在这里是:MyServer.MyApplication,一定要包括命名空间
EnableAutoRestart:是否是自动启动,表示当我们替换服务器文件时候,不用停止服务器,替换后photon会自动加载文件
WatchFiles和ExcludeFiles
完成后保存,运行托盘程序deploy\bin_Win64\PhotonControl.exe,
(证书放在deploy\bin_Win64\目录下)
下面开始编写客户端代码,首先从官网下载Unity
SDK
打开Unity3d编辑器,首先把Photon-Unity3D_v3-0-1-14_SDK\libs\Release\Photon3Unity3D.dll导入到Unity中,新建脚本TestConnection.cs,脚本代码如下:
- using UnityEngine;
-
using System.Collections;
-
using ExitGames.Client.Photon;
-
public class TestConnection : MonoBehaviour,IPhotonPeerListener {
-
public PhotonPeer peer;
-
-
void Start () {
-
peer = new PhotonPeer(this,ConnectionProtocol.Udp);
- }
-
-
void Update () {
- peer.Service();
- }
-
void OnGUI(){
-
if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,200,100),"Connect")){
-
peer.Connect("localhost:5055","MyServer");
- }
-
}
- #region IPhotonPeerListener implementation
-
public void DebugReturn (DebugLevel level,
string message)
- {
- }
-
public void OnOperationResponse (OperationResponse operationResponse)
- {
- }
-
public void OnStatusChanged (StatusCode statusCode)
- {
-
switch(statusCode){
-
case StatusCode.Connect:
-
Debug.Log("Connect Success!");
-
break;
-
case StatusCode.Disconnect:
-
Debug.Log("Disconnect!");
-
break;
- }
- }
-
public void OnEvent (EventData eventData)
- {
-
}
- #endregion
- }
using UnityEngine;
using System.Collections;
using ExitGames.Client.Photon;
public class TestConnection : MonoBehaviour,IPhotonPeerListener {
public PhotonPeer peer;
// Use this for initialization
void Start () {
peer = new PhotonPeer(this,ConnectionProtocol.Udp);
}
// Update is called once per frame
void Update () {
peer.Service();
}
void OnGUI(){
if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,200,100),"Connect")){
peer.Connect("localhost:5055","MyServer");
}
}
#region IPhotonPeerListener implementation
public void DebugReturn (DebugLevel level, string message)
{
}
public void OnOperationResponse (OperationResponse operationResponse)
{
}
public void OnStatusChanged (StatusCode statusCode)
{
switch(statusCode){
case StatusCode.Connect:
Debug.Log("Connect Success!");
break;
case StatusCode.Disconnect:
Debug.Log("Disconnect!");
break;
}
}
public void OnEvent (EventData eventData)
{
}
#endregion
}
把脚本绑定到场景中物体上,运行后可以看到一个按钮,点击连接,如果连接成功会打印"Connect Success!".
转载地址:http://blog.csdn.net/asd237241291/article/details/8819095
分享到:
相关推荐
高通AR 最新版 vuforia-unity-6-2-10 vuforia-unity-6-2-10
Photon服务器引擎 入门教程一 客户端连接服务器,客户端状态变化
vuforia-unity-6-0-112.unitypackage
vuforia-samples-advanced-android-ios-unity-4-2-3.zip官网下载太慢了,放在网盘方便大家下载
Unity日志输出插件,在手机和pc上均有用,能很清晰的看到日志打印,非常好用。 使用这个工具,你可以很容易地在游戏内部检查你的编辑器控制台日志!在移动端同样有效! 支持unity2017.4.29 or 更高版本
Unity和C#-游戏开发-官方超好画质的射击游戏源码+源代码工程
第一人称射击游戏----FPSGame.zip------------------------unity4.3.4f1----------源代码 第一人称射击游戏----FPSGame.zip------------------------unity4.3.4f1----------源代码
Unity 免ADB真机日志查看插件分享Unity Logs Viewe。详情参看https://blog.csdn.net/wuming2016/article/details/90609787
unity-webview-integration-master unity-webview-integration-master unity-webview-integration-master
vuforia-unity-android-modeltargetstest-10-5-5
spine骨骼动画导入unity插件
vuforia-samples-core-unity-6-2-6 These samples show how to build apps using the following core features of Vuforia. Image Targets VuMark Object Recognition Cylinder Targets Multi Targets User ...
包含了客户端登陆服务器,接收服务器响应等
spine-unity-3.8-2021-01-08.unitypackage
Unity真机调试编辑器(Unity-Logs-Viewer-master),兼容iOS和Andriod系统边玩游戏面板显示Debug信息,非常方便;
为了速度慢下载失败或者无法下载的人提供,这是最新的vuforia unity sdk 6.2.10
Unity和C#-游戏开发-简单的3D回合制游戏教程+源代码工程
Unity-UI-Extensions,总有一款效果是你想要的!
unity spain 3.8 2021-11-10