我用的是xlua实例带的LuaBehaviour.cs来调用相关联的lua脚本,我觉得很好,
可如果是我的子物体也挂载了一个LuaBehaviour.cs脚本也有对应的lua脚本
我该如何通过父关联Lua脚本去调用儿关联Lua脚本的funtion呢?
/* * Tencent is pleased to support the open source community by making xLua available. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at * http://opensource.org/licenses/MIT * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ using UnityEngine; using System.Collections; using System.Collections.Generic; using XLua; using System; using System.IO; using System.Text; using UnityEngine.Serialization; namespace XLuaTest { [System.Serializable] public class Injection { public string name; public GameObject value; } [LuaCallCSharp] public class LuaBehaviour : MonoBehaviour { [FormerlySerializedAs("AssetText")] public TextAsset luaTextAsset; public Injection[] injections; internal static LuaEnv luaEnv = new LuaEnv(); //all lua behaviour shared one luaenv only! internal static float lastGCTime = 0; internal const float GCInterval = 1; //1 second private Action luaStart; private Action luaUpdate; private Action luaOnDestroy; public LuaTable scriptEnv; void Awake() { scriptEnv = luaEnv.NewTable(); // 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突 LuaTable meta = luaEnv.NewTable(); meta.Set("__index", luaEnv.Global); scriptEnv.SetMetaTable(meta); meta.Dispose(); scriptEnv.Set("self", this); foreach (var injection in injections) { scriptEnv.Set(injection.name, injection.value); } luaEnv.DoString(luaTextAsset.text, luaTextAsset.name.Split('.')[0], scriptEnv); Action luaAwake = scriptEnv.Get<Action>("awake"); scriptEnv.Get("start", out luaStart); scriptEnv.Get("update", out luaUpdate); if (luaAwake != null) { luaAwake(); } } // Use this for initialization void Start() { if (luaStart != null) { luaStart(); } } // Update is called once per frame void Update() { if (luaUpdate != null) { luaUpdate(); } if (Time.time - LuaBehaviour.lastGCTime > GCInterval) { luaEnv.Tick(); LuaBehaviour.lastGCTime = Time.time; } } void OnDestroy() { if (luaOnDestroy != null) { luaOnDestroy(); } luaOnDestroy = null; luaUpdate = null; luaStart = null; scriptEnv.Dispose(); injections = null; } } }
----------------华丽分割线下面是Lua脚本--------
--write by sky_allen local u =CS.UnityEngine; local g =u.GameObject; local speed =10 function awake() local go =g.Find("Cylinder"); local tt= go:GetComponent("LuaBehaviour").scriptEnv;--这个scriptEnv应该相当于mono类吧,可是获取不到,我着急了老师帮帮我 print(tt); print("hello world") end