XLua import and object mount Lua components

XLua import

  1. Official download link: add a link description
    to download the source code and unzip it:
    insert image description here
    open the Assets folder, drag Plugins and XLua into Unity Assets
  2. Drag Tools into Unity. After the import is complete, the console will report an error, showing that there are duplicate dll files. Delete some files, and the rest are shown in the figure
    insert image description here
  3. Then add a macro in Project Setting->Player->Script Compilation: HOTFIX_ENABLE
    insert image description here
  4. The preparatory work is completed, simply test the lua function, create a C# script, named Test
    in which we need to import the XLua namespace, and use LuaEnv, the code is as follows:
public class Test : MonoBehaviour
{
    
    
	public LuaEnv luaEnv;
    void Start()
    {
    
    
    	luaEnv=new LuaEnv();//创建对象
    	//执行lua脚本需要DoString()
    	luaEnv.DoString("print('hello xlua')");
    	//如果需要执行指定lua文件,则需要require+'lua文件名'
    	//luaEnv.DoString("require'fileName'")
    	lua.Dispose();//释放
    }
}
  1. Create a game object in Unity and mount the Test script, click in sequence above the Unity window to generate and inject
    Generate Code
    Hotfix Inject In Editor
    insert image description here
  2. Click Run, you can see that the console prints this sentence, indicating that the function has been successfully used, and the next step is to make some better tools
    insert image description here

Lua script mount

  • illustrate:
    • I don't want to load through the path, but want to mount the lua script like the object mount component by dragging and dropping. I have been looking for a solution for a long time, and there is currently a solution that can barely achieve this goal.
    • plan:
      1. Use the class inherited from mono to mount on the object
      2. This class contains the lua file to be executed (feeling a bit of putting the cart before the horse)
  1. The first step is to convert the lua file into a file supported by Unity. TextAssets
    needs to create a class inside Unity:
[ScriptedImporter(1, ".lua")]
public class LuaImporter : ScriptedImporter
{
    
    
    public override void OnImportAsset(AssetImportContext ctx)
    {
    
    
        var luaTxt = File.ReadAllText(ctx.assetPath);
        var assetText = new TextAsset(luaTxt);
        ctx.AddObjectToAsset("main obj", assetText);
        ctx.SetMainObject(assetText);
    }
}

This class is used to convert .lua files into TextAssets resource files that Unity can use

  1. The second step is that currently Unity does not support creating Lua files directly, so it is necessary to implement the function of creating Lua files.
    insert image description here
    This method can refer to Create a new Lua file using a Lua script template in Unity
    . After the above implementation, create a Lua script test. lua, in this lua file we can make a simple attempt to create objects and name them
print('hello')
go=CS.UnityEngine.GameObject('wo');
  1. Implement a class that contains TextAssets
public class LuaContainer: MonoBehaviour
{
    
    
	//lua文件
    public TextAsset luaFile;
    //lua环境
    LuaEnv luaEnv;
}

Create a game object and mount LuaContainer

  • The next step is how to execute the lua script mounted on the current object
  • The first is to execute the lua script through DoString (string), then first get the lua file just written in the window
    insert image description here
  • Get the text content in it and execute
    void Start()
    {
    
    
        luaEnv= new LuaEnv();
        if (luaFile!=null)
        {
    
    
            Debug.Log("加载不为空");
            luaEnv.DoString(luaFile.text);
        }
     }

Click to run the game:
insert image description here
the result is successful

Replenish

  1. The current method is not very friendly. Every time a game object that mounts a lua file is created, a LuaEnv will be created for new and Dispose

Guess you like

Origin blog.csdn.net/SuShengQuanshiW/article/details/129231619