我是靠谱客的博主 舒心秋天,这篇文章主要介绍ToLua学习笔记(二) Example 02 ScriptsFromFileToLua学习笔记(二) Example 02 ScriptsFromFile,现在分享给大家,希望可以做个参考。

ToLua学习笔记(二) Example 02 ScriptsFromFile

转载请注明出处四川包邮

  • ToLua学习笔记二 Example 02 ScriptsFromFile
    • 测试
    • 示例代码
    • 函数用法
      • AddSearchPath
      • DoFile
      • Require

这一篇文章主要是通过Example讲解searchpath,require,dofile.

测试

通过运行Example可以发现,不管点击多少次DoFile,每次都会在屏幕上显示

This is a script from a utf8 file
tolua: 你好! こんにちは! 안녕하세요!

但是点击Require只有第一次会出现,之后就无效了

This is a script from a utf8 file
tolua: 你好! こんにちは! 안녕하세요!

示例代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using UnityEngine; using System.Collections; using LuaInterface; using System; using System.IO; //展示searchpath 使用,require 与 dofile 区别 public class ScriptsFromFile : MonoBehaviour { LuaState lua = null; private string strLog = ""; void Start () { #if UNITY_5 Application.logMessageReceived += Log; #else Application.RegisterLogCallback(Log); #endif lua = new LuaState(); lua.Start(); //如果移动了ToLua目录,自己手动修复吧,只是例子就不做配置了 string fullPath = Application.dataPath + "\ToLua/Examples/02_ScriptsFromFile"; lua.AddSearchPath(fullPath); } void Log(string msg, string stackTrace, LogType type) { strLog += msg; strLog += "rn"; } void OnGUI() { GUI.Label(new Rect(100, Screen.height / 2 - 100, 600, 400), strLog); if (GUI.Button(new Rect(50, 50, 120, 45), "DoFile")) { strLog = ""; lua.DoFile("ScriptsFromFile.lua"); } else if (GUI.Button(new Rect(50, 150, 120, 45), "Require")) { strLog = ""; lua.Require("ScriptsFromFile"); } lua.Collect(); lua.CheckTop(); } void OnApplicationQuit() { lua.Dispose(); lua = null; #if UNITY_5 Application.logMessageReceived -= Log; #else Application.RegisterLogCallback(null); #endif } }

函数用法

AddSearchPath

AddSearchPath用来添加索引路径,类似c++中添加库函目录
将需要搜索的路径通过AddSearchPath添加之后DoFile的时候会直接去找对应的Lua文件

DoFile

执行文件中的代码.

Require

require和dofile有点像,不过又很不一样,require在第一次加载文件的时候,会执行里面的代码。
但是,第二次之后,再次加载文件,则不会重复执行了.换句话说,它会保存已经加载过的文件,不会重复加载.

最后

以上就是舒心秋天最近收集整理的关于ToLua学习笔记(二) Example 02 ScriptsFromFileToLua学习笔记(二) Example 02 ScriptsFromFile的全部内容,更多相关ToLua学习笔记(二)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部