Configure xlua environment in unity
Download xlua
and then open the directory and copy the Plugins and Xlua files in the xLua>Assets folder folder and move it to the Assets folder of your project.
After moving, open the project with unity, and the XLua option will appear in the menu bar.
OpenProjectSettingsFindScripting Define SymbolsFill inHOTFIX_ENABLE
At this time, there will be an additional option in the XLua menu
One more thing I forgot to mention is that the Tools file in the root directory of the downloaded Xlua project must be folder into the root directory of your project.
Otherwise, please install the Tools error will be reported when injecting.
Then open the scene below in your own project.
Then select XLua>Generate Code and wait for finished to appear on the console! Tips from @#¥
Then select XLua>Hotfix Inject In Editor, and finish appears on the console! This means the injection is complete!
This step is very important. You must perform these two steps every time you modify the code!
Then click the Start button, the following prompt will be displayed on the console
Then click the Hotfix button, the console prompt will change to lua
At this time, the official example has been allowed to succeed, indicating that the configuration of xlua in unity is completed.
Now we create a new scene named Game in the project. Then click XLua>Clear Generated Code to clear the redundant code first.
Scaffolding scene
Create a new Plane and Cube game object in the Game scene and return their positions to 0. Adjust the Cube position so that it is on the plane, then add the Rigidbody component to the Cube object, create a new Cube script, and add the script to the Cube.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cube : MonoBehaviour
{
private Rigidbody rigidbody;
// Start is called before the first frame update
void Start()
{
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
rigidbody.AddForce(Vector3.up*200);
}
}
}
Then save. Run the scenario.
Press the W key at this time and find that the object jumps up.
Read external Lua code and run it in unity
We create a new Txt text at any position, name it Test.lua.txt, and enter the print statement in lua. Then save the encoding format asUTF-8Key point otherwise the compilation will not pass.
Save the file and remember the path of this file, and then create a new HotFixScript script. This is the xlua hot update script. The code is as follows
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using XLua;
/// <summary>
/// 演示脚本
/// </summary>
public class HotFixScript : MonoBehaviour
{
private LuaEnv luaEnv;//lua虚拟环境
private void Awake()
{
luaEnv = new LuaEnv();//开启lua虚拟环境
luaEnv.AddLoader(MyLoader);
luaEnv.DoString("require 'Test'");//用虚拟机运行Lua代码
}
private byte[] MyLoader(ref string filePath)
{
string absPath = @"H:\xiangmu\Txt\Assets\Lua\" + filePath + ".lua.txt";//在任何地方都可以新建一个存放lua的文件夹,存放Lua代码的路径
return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));
}
private void OnDestroy()
{
luaEnv.Dispose();//关闭
}
}
Because there are code changes, we repeat the first two operations:
XLua>Generate Code
XLua>Hotfix Inject In Editor
Run scenario
The lua code runs successfully and is displayed in the console!