tolua正式学习(二)

读取lua文件并输出显示

------------------------------------------------------------------------------------------------------------------

原文:

c#

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 || UNITY_2017 || UNITY_2018		
        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 += "\r\n";
    }

    void OnGUI()
    {
        GUI.Label(new Rect(100, Screen.height / 2 - 100, 600, 400), strLog);

        ///----------------------------------
        ///lua.DoFile(); 可以重复调用
        ///----------------------------------
        if (GUI.Button(new Rect(50, 50, 120, 45), "DoFile"))
        {
            strLog = "";
            lua.DoFile("ScriptsFromFile.lua");                        
        }
        ///----------------------------------
        ///lua.Require(); 只首次可用
        ///----------------------------------
        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 || UNITY_2017 || UNITY_2018	
        Application.logMessageReceived -= Log;
#else
        Application.RegisterLogCallback(null);
#endif 
    }
}

lua

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

输出

测试结果dofile()可反复使用,require()只有第一次可以使用

-------------------------------------------------------------------------------------------------------------------------------------

继续测试 目的尝试更改存储路径,复制一个lua文件 并改名为ScriptsFromFile2,并将其存在01_HelloWorld文件夹下

创建新的C#脚本进行测试。

c#

using UnityEngine;
using System.Collections;
using LuaInterface;
using System;
using System.IO;

//展示searchpath 使用,require 与 dofile 区别
public class ScriptsFromFile2 : MonoBehaviour
{
    LuaState lua = null;
    private string strLog = "";

    void Start()
    {
#if UNITY_5 || UNITY_2017 || UNITY_2018		
        Application.logMessageReceived += Log;
#else
        Application.RegisterLogCallback(Log);
#endif
        lua = new LuaState();
        lua.Start();

        ///----------------------
        ///因修改了lua文件的存储地址 故需要更改文件地址
        ///lua.AddSearchPath() 获取lua文件地址
        ///----------------------
        string fullPath = Application.dataPath + "\\ToLua/Examples/01_HelloWorld";
        lua.AddSearchPath(fullPath);
          
    }

    void Log(string msg, string stackTrace, LogType type)
    {
        strLog += msg;
        strLog += "\r\n";
    }
    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("ScriptsFromFile2.lua");  //文件名要正确
        }
        else if (GUI.Button(new Rect(50, 150, 120, 45), "Require"))
        {
            strLog = "";
            lua.Require("ScriptsFromFile2");     //文件名要正确
        }

        lua.Collect();
        lua.CheckTop();
    }

    void OnApplicationQuit()
    {
        lua.Dispose();
        lua = null;
#if UNITY_5 || UNITY_2017 || UNITY_2018	
        Application.logMessageReceived -= Log;
#else
        Application.RegisterLogCallback(null);
#endif
    }
}

lua

--------------------------------------------------
--
--[[测试处理 --]]
--------------------------------------------------
function showMessage(s1,s2)

	return string.format("测试:%s %s",s1,s2);
end
--------------------------------------------------
print("这是个新的读取文件")

print(string.format("%s%s", "xxxxxxx","ssssssssss"))

print(showMessage("hi","你好"))

输出结果

另外大致看了下lua.Collect(); 是清空引用的 

猜你喜欢

转载自blog.csdn.net/leisurely_orange/article/details/81566757
今日推荐