Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(196评价)
价格: 4009.00元
老师好,我用的xlua热更新,想问一个关于纯lua编程的想法
cyl3181515发起了问答2020-08-12
5
回复
352
浏览

我用的是xlua实例带的LuaBehaviour.cs来调用相关联的lua脚本,我觉得很好,

可如果是我的子物体也挂载了一个LuaBehaviour.cs脚本也有对应的lua脚本

我该如何通过父关联Lua脚本去调用儿关联Lua脚本的funtion呢?

所有回复
  • 老师_Trigger 2020-08-13

    同学你好,可以把对应脚本复制给老师看看吗?这个脚本没有看过实现

    还有-4条回复,点击查看
    你还没有登录,请先登录注册
  • cyl3181515 2020-08-13
    /*
     * 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
    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • cyl3181515 2020-08-13

    已经找到原因了,是生命周期函数相同导致的。。。

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • 老师_Trigger 2020-08-13

    因为没有具体测试过所以老师不是很清楚,但如果想要调用它的方法可以获取到子游戏物体的luaBehavior游戏对象,然后像调用其他游戏物体里边的lua方法去调用就可以了,如果获取不到,可以提前在Unity那边获取一下,然后在lua这边使用或者封装一个获取查找的方法试试

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!