天天看点

卜若的代码笔记-photon系列-第二章:客户端与日志

1.创建Plugins文件夹

卜若的代码笔记-photon系列-第二章:客户端与日志

2.将

Photon3Unity3D.dll 
ExitGamesLibs.dll
           

丢到这个文件家里面

卜若的代码笔记-photon系列-第二章:客户端与日志

3.创建脚本ClientManger

using ExitGames.Client.Photon;
using UnityEngine;

public class ClientManager : MonoBehaviour, IPhotonPeerListener
{

    private static ClientManager Instance;
    public PhotonPeer peer;
    public void connectServer()
    {

        peer = new PhotonPeer(this, ConnectionProtocol.Udp);
        peer.Connect("127.0.0.1:5055", "SavageCollision");

    }
    void Awake()
    {

        Instance = this;
    }
    void Start()
    {
        connectServer();
    }

    void Update()
    {
        peer.Service();
    }
    public void DebugReturn(DebugLevel level, string message)
    {
        
    }
    void OnApplicationQuit()
    {

        if (peer != null & peer.PeerState == PeerStateValue.Connected)
        {

            peer.Disconnect();
        }
    }


    public void OnEvent(EventData eventData)
    {
        
    }

    public void OnMessage(object messages)
    {
       
    }

    public void OnOperationResponse(OperationResponse operationResponse)
    {
        
    }

    public void OnStatusChanged(StatusCode statusCode)
    {
       
    }

 

}
           

4.日志:

4-1:在服务器端添加引用

log4net.dll
ExitGames.Logging.Log4Net.dll
           
卜若的代码笔记-photon系列-第二章:客户端与日志

4.2:写日志初始化函数

private void InitLogging()
        {
            ExitGames.Logging.LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
            GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(this.ApplicationRootPath, "log");
            GlobalContext.Properties["LogFileName"] = "SavageCollision"; //日志名字
            XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(this.BinaryPath, "log4net.config")));
        }
           

4.3:写log函数

private static readonly ILogger logger = 
  ExitGames.Logging.LogManager.GetCurrentClassLogger();

        public static void log(string str)
        {
            logger.Info(str);
        }
           

4.4:复制log4net.config

卜若的代码笔记-photon系列-第二章:客户端与日志

 复制到

卜若的代码笔记-photon系列-第二章:客户端与日志

并选择始终复制

4.5-在setup里面初始化日志

卜若的代码笔记-photon系列-第二章:客户端与日志

4-6:生成解决办法,重启服务器测试日志

卜若的代码笔记-photon系列-第二章:客户端与日志

4.7.链接测试

卜若的代码笔记-photon系列-第二章:客户端与日志

txt可以导入到擦看日志的那个窗口里面就像这样

 5.链接测试:

卜若的代码笔记-photon系列-第二章:客户端与日志

运行客户端(Unity) 

卜若的代码笔记-photon系列-第二章:客户端与日志
Success 
           

6.服务器端代码(有小修改,仅供参考)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using ExitGames.Logging.Log4Net;
using System.IO;
using log4net;
using log4net.Config;
using ExitGames.Logging;

namespace SavageCollision
{
    public class Main : ApplicationBase
    {
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {

            Log.log("有一个链接");

            return new Client(initRequest);
        }

        private void InitLogging()
        {
            ExitGames.Logging.LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
            GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(this.ApplicationRootPath, "log");
            GlobalContext.Properties["LogFileName"] = "SavageCollision"; //日志名字
            XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(this.BinaryPath, "log4net.config")));
        }

        public static readonly ILogger logger = ExitGames.Logging.LogManager.GetCurrentClassLogger();

        public static void log(string str)
        {
            logger.Info(str);
        }

        protected override void Setup()
        {
            InitLogging();
            Log.log("服务器开启");

        }

        protected override void TearDown()
        {

            
            
        }
    }
}
           

继续阅读