我是靠谱客的博主 舒心秋天,最近开发中收集的这篇文章主要介绍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: 你好! こんにちは! 안녕하세요!
示例代码
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学习笔记(二) Example 02 ScriptsFromFileToLua学习笔记(二) Example 02 ScriptsFromFile所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复