1. 程式人生 > >unity逐行講解騰訊XLua文件LuaBehaviour(六)

unity逐行講解騰訊XLua文件LuaBehaviour(六)

騰訊出品的XLua越來越受到遊戲開發者的青睞,小編也不例外,但在學習XLua文件的時候,深感騰訊的這個文件寫的,還不如他們的各種SDK寫的好。小編決定逐行分析其文件,把缺下的註釋補回來。同是也歡迎大神訂正。

第一篇 LuaBehaviour

/*
 * 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;

[System.Serializable]
public class Injection
{
    public string name;
    public GameObject value;
}

[LuaCallCSharp]
public class LuaBehaviour : MonoBehaviour {
    //lua程式碼,在騰訊例項中是在Hierarchy面板賦值的
    public TextAsset luaScript;
    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;

    private LuaTable scriptEnv;

    void Awake()
    {
        //獲取LuaTable,要用luaEnv.NewTable(),這裡會做一些排序,共用虛擬機器等操作
        scriptEnv = luaEnv.NewTable();
        LuaTable meta = luaEnv.NewTable();
        //key,value的方式,虛擬機器環境對應的key值一定要是這個“__index”,
        //在xlua的底層,獲取LuaTable所屬虛擬機器的環境是get的時候,用的key是這個名字,所以不能改
        meta.Set("__index", luaEnv.Global);
        //將有虛擬機器和全域性環境的table繫結成他自己的metatable
        scriptEnv.SetMetaTable(meta);
        //值已經傳遞過去了,就釋放他
        meta.Dispose();
        //這裡的"self"和上面的"__index"是一個道理啦。將c#指令碼繫結到LuaTable
        scriptEnv.Set("self", this);
        //在騰訊示例中,Hierarchy面板賦值的,在這裡應該是約定名字和子物體的對應,繫結到luatable
        foreach (var injection in injections)
        {
            scriptEnv.Set(injection.name, injection.value);
        }
        //執行lua語句,三個引數的意思分別是lua程式碼,lua程式碼在c#裡的代號,lua程式碼在lua虛擬機器裡的代號
        luaEnv.DoString(luaScript.text, "LuaBehaviour", scriptEnv);
        //xlua搞了這麼久,也就是為了最後這幾個錘子,c#呼叫lua裡的方法。
        //總結起來一句話,通過luatable這個類來完成c#呼叫Lua
        //怎樣完成這一步呢?就是獲取luatable物件,配置lua虛擬機器,配置虛擬機器環境,繫結c#程式碼,最後執行lua語句
        Action luaAwake = scriptEnv.Get<Action>("awake");
        scriptEnv.Get("start", out luaStart);
        scriptEnv.Get("update", out luaUpdate);
        scriptEnv.Get("ondestroy", out luaOnDestroy);
        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程式碼
-- 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.

local speed = 10

function start()
	print("lua start...")
end

function update()
	local r = CS.UnityEngine.Vector3.up * CS.UnityEngine.Time.deltaTime * speed
	self.transform:Rotate(r)
end

function ondestroy()
    print("lua destroy")
end

騰訊XLua開源地址傳送門:點選開啟連結
裡面的資源是需要git下載的,為了幫助還沒有用過git的夥伴,小編簡單說兩句怎樣使用

1.下載安裝git安裝包

2.開啟它


2.cd到你文件要下載到的目錄

3.輸入git clone https://github.com/Tencent/xLua.git 回車成功!

如果您覺得有用,就請我喝杯咖啡吧0.0


相關推薦

unity講解XLuaLuaBehaviour()

騰訊出品的XLua越來越受到遊戲開發者的青睞,小編也不例外,但在學習XLua文件的時候,深感騰訊的這個文件寫的,還不如他們的各種SDK寫的好。小編決定逐行分析其文件,把缺下的註釋補回來。同是也歡迎大神訂正。 第一篇 LuaBehaviour/* * Tencent is

python讀取以非換符分隔的超大,並輸出

pri while spa new for int 逐行輸出 bre class def myreadline(f, newline): buf = "" while True: while True:

Unity調用系統窗口選擇路徑

ssi iou bool 調用 文件 number replace gas edi using UnityEngine;using System.Collections;using System;using System.Runtime.InteropServices;[S

shell 讀取連續指定輸入到另一個

shell#!/bin/bash a=3975 lines=`wc -l /tmp/zabbix_proxy.log | awk ‘{print $1}‘` echo $a:$lines for ((i=$a;i<=$lines;i++)) do n=$[i]p cmd="sed -n

unity 播放視頻 WWW下載StreamingAssets

oge nsf nbsp sys resource text 紋理 engine ges 1. 2. 3. 4. 5. 6.代碼如下 using UnityEngine;using System.Collections;using UnityEngin

Linux命令下svn ignore忽略夾用法

let export 方式 同時 oca 現在 rect strong pos Linux命令行下svn ignore忽略文件或文件夾用法 一、忽略單個目錄 1、忽略文件夾 假如目錄oa.youxi.com是從svn checkout出來的,在服務器本地目錄添加了m

Unity編輯器生成可配置編輯

prefab tga 可編輯 edit 字段 work highlight save csharp using UnityEditor; public class PoolManagerEditor { [MenuItem("Manager/Creat Game

python3 在確實存在的情況下,運提示找不到

python head file python3 path tor 替換 找不到文件 成功 提示 [Errno 2] No such file or directory: 但是路徑下確實存在此文件,在不改動的情況下,再次運行,執行成功。 百思不得其解,看到此鏈接下的回答 h

如何在Win10裏使用命令來壓縮/解壓縮

歸檔 顯示 blog 公眾 成功 寶寶 簡單 公眾號 zip 如果你的電腦的硬盤空間十分有限,那麽這篇文章應該對你有用。在這篇文章中,我們將討論如何為文件或者文件夾啟用文件壓縮。和ZIP文件壓縮或者RAR文件壓縮相比,使用這種方式,你無需創建歸檔文件,壓縮後的文件也將可以像

如何遍歷夾 取得名和首記錄輸出到CSV

type china tput col ref nbsp 遍歷文件夾 如何 color for x in `find -type f`; do sed -n "1{s,.*,${x##*/}\t&,p}" $x; done > output.csv f

Android Studio Terminal 不是內部或外部命令,也不是可運程序或批處理

打包apk 點擊 bin adb命令 系統環境變量 tar 依然 .net 工具 1、Android Studio Terminal 命令行無效的問題 在Android Studio中自帶了命令行終端Terminal,但是我們在輸入命令時經常會發現:“XX

詳細講解NFS網絡存儲系統配置

redhat 指定 移動介質 通過 col don ext 9.png src 詳細講解NFS網絡文件存儲系統配置 -----------------------------------------------NFS 優點--------------------------

校園網小蝴蝶運顯示:缺少packet.dll

svr3 復制 文件目錄 打開 .exe pca 搜索 輸入 目錄 一、校園網小蝴蝶修復: 打開C盤下sinf文件,點擊WinPcap_4_1_3.exe文件進行安裝。 二、如果是其他遊戲或軟件,修復方法: 1.網上搜索packet.dll文件並下載。 2.把packet.

Confluence 6 通過 SSL 或 HTTPS 運 - 修改你 server.xml

syn enc conf ret key tomcat disabled client strong 下一步你需要配置 Confluence 來使用 HTTPS:編輯 <install-directory>/conf/server.xml 文件。取消註釋下面的行

Unity遊戲開發】tolua之wrap的原理與使用

nop 微信 attr hiera n) 接下來 system 作者 prim   本文內容轉載自:https://www.cnblogs.com/blueberryzzz/p/9672342.html 。非常感謝原作者慷慨地授權轉載,比心!@blueberryzzz

Unity 3D(三)附件 Unity 3D 5.6.5f1 AssetBundle官方的翻譯

1 AssetBundles(資源包) An AssetBundle is an archive file containing platform specific Assets (Models, Textures, Prefabs, Audio clips, and ev

Unity讀取電腦磁碟中的txt

1、首先我這個只能從電腦磁碟中讀取,不能寫入。 2、在Unity中是按行讀取的txt文字中的內容 3、程式碼如下 using System.Collections; using System.Collections.Generic; using UnityEng

android 中語音停頓合成技巧

最近寫android的時候碰到一個需求,就是當我們想要自己合成語音的時候,需要在播報一句話的某些位置進行幾秒鐘的停頓,比如想要合成這樣的語音:"大家好,【停頓一秒】歡迎【停頓兩秒】來到我的部落格,如果有用【停頓一秒】,頂一下唄"。在訊飛的文件中,我沒看到有實現這樣功能的方法

Unity開始android開發之旅——官方翻譯

本譯文只是自己檢視官方文件時的學習之作,大家以批判的方式來看待。 開始android開發之旅 為andorid作業系統開發遊戲使用的方式和IOS開發類似。但是,相較於IOS開發,android開發有一個嚴重的問題就是對於所有的android裝置來說,他們的硬體並不是完全標

C 裏怎樣得到當前執行的函數名,當前代碼,源代碼

ext apt 文件 column ref dia level nbsp view 得到函數名: System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(); t