1. 程式人生 > >XLua與CSharp互動的採坑點 : 熱修復有返回值的CSharp方法

XLua與CSharp互動的採坑點 : 熱修復有返回值的CSharp方法

1、假如CS的一個類中有如下邏輯: 

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using XLua;
 5 
 6 namespace LGSTEST
 7 {
 8     public class Lgs : MonoBehaviour
 9     {
10         void Update()
11         {
12             if (Input.GetMouseButtonDown(0))
13             {
14 Debug.Log(GetIndex(15, 10)); 15 } 16 } 17 18 int GetIndex(int num1,int num2) 19 { 20 return num1 / num2; 21 } 22 } 23 }

現在有一個需求是:通過Xlua來更換CSharp中的 GetIndex 方法:XLua中的程式碼如下:

1  xlua.hotfix(CS.LGSTEST.Lgs, "GetIndex", function
(this, num1, num2) 2 return num1 / num2 3 end)

有沒有感覺簡單?但是這樣輸出的結果是多少呢?實參 num1 = 15 , num2 = 10 ,按照我們的理解列印的結果應該是 1,但是事實是這樣的嗎?列印看看即可知道,結果如下:

沒錯,你沒有看錯,結果就是 0.具體原因是什麼,其實我也不知道,我只知道怎麼改,正確Xlua程式碼如下:

1  xlua.hotfix(CS.LGSTEST.Lgs, "GetIndex", function(this, num1, num2)
2     return math.floor(num1 / num2) 
3 end)

現在的列印結果如下: