In the process of game development, there are often updates, and the small version does not need to be re-downloaded when it is updated, so as to better protect the player's experience. Here we use our hot update technology
Only learning is recorded here, and simple function updates are completed
Here you need to import an XLua plugin
To use hot update, you need to modify the settings here
Add a command here to allow hot update operations as follows:
The case this time is a game about eating snakes. The hot update adds an operation of clicking the left mouse button and then stopping the right button and vice versa.
Two methods are declared here for hot update. Of course, you can update any method. Hot update will update the content in your method to the content in your hot update method.
Then judge the call in update
You also need to write a class to call Lua
using XLua;
using System.IO;
public class TestSnake : MonoBehaviour
{
LuaEnv lua;
// Start is called before the first frame update
void Start()
{
lua = new LuaEnv();
lua.AddLoader(Loads);
lua.DoString("require('Snake')");
}
byte[] Loads(ref string fileName)
{
string path = Application.dataPath + "/Lua/" + fileName + ".lua";
return File.ReadAllBytes(path);
}
// Update is called once per frame
void OnDestory()
{
lua.Dispose();
}
}
Next is the hot update of Lua
engine=CS.UnityEngine
xlua.hotfix(CS.Player,'Stop',--你需要更新的方法名
function(self)
engine.Time.timeScale=0
end
)
xlua.hotfix(CS.Player,'ReStart',
function(self)
engine.Time.timeScale=1
end
)
Xlua hot update can cover all the content of the original method after updating
After writing, you need to update it in Unity
First click on the second item to clear, click on the first item to rewrite, and finally click on the third item to update
This completes the hot update