Generate EmmyLua code hints for Unity Api for XLua

problem background

When writing Lua code, using Unity's Api does not have code hints to affect efficiency. A type system and code hints are needed. Let's solve this problem

Support type system

Lua is a dynamic language that does not support declaring variable types, so you need to start from outside the language. There is a plug-in called EmmyLua, which can build a type system by adding special annotations, such as

---@class CS.UnityEngine.Vector3 : CS.System.ValueType
---@field kEpsilon number
---@field kEpsilonNormalSqrt number
---@field x number
---@field y number
---@field z number
---@field zero CS.UnityEngine.Vector3
---@field one CS.UnityEngine.Vector3
---@field forward CS.UnityEngine.Vector3
---@field back CS.UnityEngine.Vector3
---@field up CS.UnityEngine.Vector3
---@field down CS.UnityEngine.Vector3
---@field left CS.UnityEngine.Vector3
---@field right CS.UnityEngine.Vector3
---@field positiveInfinity CS.UnityEngine.Vector3
---@field negativeInfinity CS.UnityEngine.Vector3
---@field Item number
---@field normalized CS.UnityEngine.Vector3
---@field magnitude number
---@field sqrMagnitude number
CS.UnityEngine.Vector3 = {
    
    }
---@overload fun(x : number, y : number, z : number) : CS.UnityEngine.Vector3
---@param x number
---@param y number
---@return CS.UnityEngine.Vector3
function CS.UnityEngine.Vector3.New(x, y) end

If there is a file with this content in the source code folder, you can use the following method to specify the type in the code, and then use emmylua to prompt the method and attribute

---@type CS.UnityEngine.Vector3
local v3

Generate Type Annotation Code

Because it is unrealistic to write type annotation codes by hand, reflective batch generation is chosen.

Principle: By reflecting a class, find the field and method of this class, generate the corresponding type annotation according to the type annotation syntax of emmylua, and finally output it to a lua file

Find an open source git project on github that supports ToLua to generate code hints https://github.com/LagField/EmmyLuaTypeGenerator

Transform a wave of code hints used to generate xlua

The main changes here are as follows
  • Use namespace filtering to find all UnityApi or third-party Api that need to be supported. If you want to reduce the size of this hint file, you can optimize it to use XLua's export type list configuration to generate corresponding code hints
  • The exported type uses. Connect namespace and class
  • Keep the type consistent with XLua, using the form of CS.UnityEngine.xxx

some caveats

  • To add a namespace cs={} definition for code hinting
  • Handles generic names, filtering out non-alphanumeric underscores
  • If there is no need to use Lua to write Editor code, remove the type export under UnityEditor
  • Similarly, the type under Burst is also removed

Source link

  • Emmylua type annotation export tool that supports xlua: https://github.com/ak47007tiger/EmmyLuaXLuaSnippetGenerator

Guess you like

Origin blog.csdn.net/ak47007tiger/article/details/127940571