UnityLua开发之Lua访问C#(五)

xiaoxiao2021-02-28  7

该代码是基于XLua,XLua插件下载链接:https://github.com/Tencent/xLua

代码如下:

using System.Collections; using System.Collections.Generic; using UnityEngine; using XLua; public class LuaCallCSharp : MonoBehaviour { private LuaEnv m_luaEnv; // Use this for initialization void Start () { m_luaEnv = new LuaEnv(); m_luaEnv.DoString("require 'LuaCallCSharp'"); } // Update is called once per frame void Update () { } private void OnDestroy() { m_luaEnv.Dispose(); } }

Lua脚本如下:

local obj = CS.UnityEngine.GameObject("obj") --创建unity游戏物体 local Unity = CS.UnityEngine local GameObject = CS.UnityEngine.GameObject --CS.UnityEngine.GameObject查找耗时 local camera = GameObject.Find("Main Camera") camera.name = "Update by lua" --调用变量的成员函数时加“ :”,否则需要将自身作为第一个参数添加 local objTransform = obj:GetComponent("Transform") --使用obj:GetComponent<Transform>()会报错 objTransform.position = Unity.Vector3(10, 10, 10)
转载请注明原文地址: https://www.6miu.com/read-1750054.html

最新回复(0)