Pun2插件结合Xlua热更新开发 一、在lua中自定义PunRpc方法

需求

近有一个需求是在pun2联网项目中结合纯lua实现项目热更新(这里使用的是xlua),起初在NetworkMgr框架搭建,房间搭建等非游戏操作逻辑中使用纯lua开发一切正常,没有发现什么问题。但是进入游戏角色逻辑开发时,就遇到了难题,众所周知,pun2中有一个很好用的attribute,叫做[PunRpc]。
具体可看官方文档:PunRpc标签官方描述
但是lua中貌似并不支持给lua方法添加Attribute,既然官方不支持,网上也几乎没有看到有人使用pun2结合xlua开发的,那我们只能选择自力更生,曲线救国的方式了。

初次尝试

我这里想到的方法一开始是使用委托结合事件中心的方式去实现,但是报了一个错误**(LuaException: c# exception:Exception has been thrown by the target of an…)**
经过百度,谷歌,必应的搜索,都没有解决,也许是我使用方式不对,其中还发现了一篇文章,可以在C#中不用频繁添加PunRpc标签,有兴趣的可以跳转去看:使用委托调用PunRpc

第二种方法

于是我想到了官方例子中的第二个例子,在Lua中实现UnityMono,这里解释一下大概的原理就是在Lua中注册委托,再让C#继承Mono的脚本调用改委托
详情可以到官方例子中名为《02_U3DScripting》的文件夹去看。
例子具体位置
代码详情:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using XLua;
using System;

namespace XLuaTest
{
    [System.Serializable]
    public class Injection
    {
        public string name;
        public GameObject value;
    }

    [LuaCallCSharp]
    public class LuaBehaviour : MonoBehaviour
    {
        public TextAsset luaScript;
        public Injection[] injections;

        internal static LuaEnv luaEnv = new LuaEnv(); //all lua behaviour shared one luaenv only!
        internal static float lastGCTime = 0;
        internal const float GCInterval = 1;//1 second 

        private Action luaStart;
        private Action luaUpdate;
        private Action luaOnDestroy;

        private LuaTable scriptEnv;

        void Awake()
        {
            scriptEnv = luaEnv.NewTable();

            // 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
            LuaTable meta = luaEnv.NewTable();
            meta.Set("__index", luaEnv.Global);
            scriptEnv.SetMetaTable(meta);
            meta.Dispose();

            scriptEnv.Set("self", this);
            foreach (var injection in injections)
            {
                scriptEnv.Set(injection.name, injection.value);
            }

            luaEnv.DoString(luaScript.text, "LuaTestScript", scriptEnv);

            Action luaAwake = scriptEnv.Get<Action>("awake");
            scriptEnv.Get("start", out luaStart);
            scriptEnv.Get("update", out luaUpdate);
            scriptEnv.Get("ondestroy", out luaOnDestroy);

            if (luaAwake != null)
            {
                luaAwake();
            }
        }

        // Use this for initialization
        void Start()
        {
            if (luaStart != null)
            {
                luaStart();
            }
        }

        // Update is called once per frame
        void Update()
        {
            if (luaUpdate != null)
            {
                luaUpdate();
            }
            if (Time.time - LuaBehaviour.lastGCTime > GCInterval)
            {
                luaEnv.Tick();
                LuaBehaviour.lastGCTime = Time.time;
            }
        }

        void OnDestroy()
        {
            if (luaOnDestroy != null)
            {
                luaOnDestroy();
            }
            luaOnDestroy = null;
            luaUpdate = null;
            luaStart = null;
            scriptEnv.Dispose();
            injections = null;
        }
    }
}
理论可行,开始实践

首先既然是Photon项目,那么游戏角色的脚本肯定是要继承MonoBehaviourPun这个类的,于是仿照LuaBehaviour这个类,改造成LuaBehaviourPun,我也是刚学xlua没多久的小白,写的有点繁琐,也请大家指证和优化。

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

public class LuaBehaviourPun : MonoBehaviourPun
{
//由于改造LuaBehaviour不是重点,且篇幅太长,这里省略其他实现代码,如果需要可以私聊我
.......
    public Action<string> punRpcAction;

    public void SetRpcAction(Action<string> action) => punRpcAction = action;
    public void ExecuteRpc(string key)
    {
        this.photonView.RPC("RpcAction", RpcTarget.All, key);
    }
    [PunRPC]
    public void RpcAction(string key)
    {
        punRpcAction?.Invoke(key);
    }
.......
}

重点其实就是一个Action变量punRpcAction,一个注册委托方法SetRpcAction,以及最后两个方法,一个方法加上[PunRPC]属性,再使用另外一个方法调用photonView上的Rpc方法去调用我们自定义的Rpc方法,这部分是C#中的逻辑,而在lua中则直接为punRpcAction添加委托事件即可。
lua中的业务逻辑为:

--通过键名区分要调用什么方法,这里的例子是显示或隐藏武器
--注册RpcAction,编写委托内逻辑
	self:SetRpcAction(function(key)
		if key=="ShowWeapon" then
			local go=self.transform:GetChild(0).gameObject
			go:SetActive(not go.activeSelf)
		end
	end)
--调用Rpc方法,重点为Input.GetKeyDown下的逻辑
	self:SetAction("update",function()
		if self.photonView.IsMine and PhotonNetwork.IsConnected then
			self.transform:Rotate(Vector3.up*Input.GetAxis("Horizontal")*Time.deltaTime*50)
			if Input.GetKeyDown(KeyCode.Space) then
				self:ExecuteRpc("ShowWeapon")
			end
		end
	end)

最后效果,完美实现两边武器的显隐同步,正确接收房主和玩家的Rpc请求。
实现了需求,但是肯定有正确的方法实现该功能,只是由于我还够了解Lua工作流程,没有想到方便快捷的方法。
隐藏武器
显示武器

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值