使用Slua框架开发Unity项目的重要步骤
下载与安装
- 下载地址 GitHub
- 安装过程
1.下载最新版,这里, 解压缩,将Assets目录里的所有内容复制到你的工程中,对于最终产品,可以删除slua_src,例子,文档等内容,如果是开发阶段则无所谓。
2.等待unity编译完毕,如果一切顺利的话,将出现slua菜单, 点击slua菜单中 All->Make 命令 手动生成针对当前版本的U3d接口文件。
3.每次更新slua版本,务必记得clear all,然后make all,否则可能运行不正确
主要的内容包括
LuaState状态机对象执行Lua字符串LuaState状态机对象执行Lua脚本LuaState状态机对象调用Lua脚本内的自定义函数LuaState状态机对象注册C#自定义类Lua脚本中调用C#中的自定义类
创建第一个可以使用Slua框架的Unity项目
在
MainCamera对象上创建AppDelegate.cs组件using UnityEngine;
using System.Collections; public class AppDelegate : MonoBehaviour
{
void Start ()
{
//在下方添加初始化代码
}
}使用
LuaState状态机对象执行Lua字符串using UnityEngine;
using System.Collections;
using SLua; public class AppDelegate : MonoBehaviour
{
private static LuaState ls_state = new LuaState();
void Start ()
{
//在下方添加初始化代码
ls_state.doString("print(\"Hello Lua!\")");
}
}使用
LuaState状态机对象执行Lua脚本文件HelloLua.lua在
Resources文件夹下添加HelloLua.lua文件print("Lua Scripts:Hello");在
AppDelegate.cs中设置LuaState.loaderDelegate启动文件委托代理using UnityEngine;
using System.Collections;
using SLua;
using System.IO; public class AppDelegate : MonoBehaviour
{
void Start ()
{
//设置脚本启动代理
LuaState.loaderDelegate = ((string fn) => {
//获取Lua文件执行目录
string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn; Debug.Log(file_path); return File.ReadAllBytes(file_path);
});
}
}在
AppDelegate.cs中通过LuaState对象执行HelloLua.lua脚本using UnityEngine;
using System.Collections;
using SLua;
using System.IO; public class AppDelegate : MonoBehaviour
{ void Start ()
{
//设置脚本启动代理
LuaState.loaderDelegate = ((string fn) => {
//获取Lua文件执行目录
string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn; Debug.Log(file_path); return File.ReadAllBytes(file_path);
}); //设置执行脚本
LuaState ls_state = new LuaState ();
ls_state.doFile ("HelloLua.lua");
}
}
通过
LuaState对象获取并执行HelloLua.lua脚本中的一个函数HelloLua.luafunction sum( v1,v2 )
-- body
return v1 + v2
end function mul( v1,v2 )
-- body
return v1 * v2
endAppDelegate.csusing UnityEngine;
using System.Collections;
using SLua;
using System.IO;
using LuaInterface;
using System; public class AppDelegate : MonoBehaviour
{
//添加LuaState初始化时的回调函数特性函数
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static int init(IntPtr L)
{
//设置初始化LuaObject对象
LuaObject.init(L);
return 0;
} void Start ()
{
//创建状态机对象
LuaState ls_state = new LuaState (); //设置脚本启动代理
LuaState.loaderDelegate = ((string fn) => {
//获取Lua文件执行目录
string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn; Debug.Log(file_path); return File.ReadAllBytes(file_path);
}); //初始化LuaState状态机与C#的转换对象
LuaState.pcall (ls_state.L, init); //设置状态机对象的执行脚本
ls_state.doFile ("HelloLua.lua"); //获取脚本中的mul函数
LuaFunction mul = ls_state.getFunction ("mul"); //调用该函数并且接收返回值
double result = (double)mul.call (-2, 3); Debug.Log(result);
}
}
自定义C#对象
LOHuman.cs,在HelloLua.lua中LOHuman.csusing System;
using LuaInterface;
using SLua;
//该特性可以修饰以下类将会注册到Slua执行环境中
[CustomLuaClass]
public class HHHuman
{
//年龄成员
protected int age = 0;
//姓名成员
protected string name = ""; //添加Lua代码中的静态函数
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
[StaticExport]
public static int CreateHuman(IntPtr l)
{
HHHuman item = new HHHuman ();
LuaObject.pushObject (l, item);
//只执行了1次LuaObject.push,返回值写1
return 1;
} public int Age {
set;
get;
} public string Name{ set; get; }
}通过点击菜单栏中的
SLua->Custom->Clear将旧版本的自定义类删除- 通过点击菜单栏中的
SLua->Custom->Make重新制作适用于SLua的新的自定义类Lua_HHHuman.cs- 默认存放目录
Assets->SLua->LuaObject->Custom->
- 默认存放目录
- 同时会自动生成一个
BindCustom.cs类,代码如下:using System;
namespace SLua {
[LuaBinder(3)]
public class BindCustom {
public static void Bind(IntPtr l) {
Lua_HHHuman.reg(l);
Lua_System_Collections_Generic_List_1_int.reg(l);
Lua_System_Collections_Generic_Dictionary_2_int_string.reg(l);
Lua_System_String.reg(l);
}
}
} - 在
AppDelegate.cs的init函数中,绑定自定义类HHHuman.cs//添加LuaState初始化时的回调函数特性函数
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static int init(IntPtr L)
{
LuaObject.init(L);
BindCustom.Bind (L);
return 0;
} - 在
HelloLua.lua脚本中,调用C#中的自定义类的静态函数CreateHuman()function testHuman()
-- body
local human = HHHuman.CreateHuman()
-- local list = human:getList()
print(human.Age)
end 在
AppDelegate.cs的Start函数中,调用HelloLua.lua脚本中的testHuman函数static LuaState ls_state;
void Start ()
{
//创建状态机对象
ls_state = new LuaState (); //设置脚本启动代理
LuaState.loaderDelegate = ((string fn) => {
//获取Lua文件执行目录
string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn; Debug.Log(file_path); return File.ReadAllBytes(file_path);
}); //初始化LuaState状态机与C#的转换对象
LuaState.pcall (ls_state.L, init); //设置执行脚本
ls_state.doFile ("HelloLua.lua"); //获取testHuman函数
LuaFunction testHuman = ls_state.getFunction ("testHuman"); //无参函数的调用
testHuman.call ();
}
创建新的Unity工程并且导入SLua框架
使用已经注入
UnityEngie引擎的状态机对象LuaSvr调用Lua脚本AppDelegate.csusing UnityEngine;
using System.Collections;
using SLua; public class Main : MonoBehaviour
{ private LuaSvr lua_svr;
// Use this for initialization
void Start()
{
//创建一个已经注入UnityEngine的状态机对象..
lua_svr = new LuaSvr(); //通过Resources文件夹下的main.txt(lua)文件内的main函数启动程序
//当然也可以通过修改LuaState.loaderDelegate来修改默认的路径
lua_svr.start("main");
}
}main.txtfuncion main()
print("Hello LuaSvr...")
end在
main.txt文件内导入UnityEngine包并且创建游戏对象import "UnityEngine" function main()
-- 创建Cube对象
local cube=UnityEngine.GameObject.CreatePrimitive(UnityEngine.PrimitiveType.Cube)
end在
main.txt文件内导入UnityEngine包并且创建空游戏对象import "UnityEngine" function main()
-- 创建空物体对象
local empty=UnityEngine.GameObject("HHEmptyObject")
end操作游戏对象上的组件
获取
Transform组件import "UnityEngine" function main()
-- 创建Cube对象
local cube=UnityEngine.GameObject.CreatePrimitive(UnityEngine.PrimitiveType.Cube) -- 获取transform组件上的position属性
local pos = cube.transform.position
pos.x = 10 -- 修改transform组件的position属性
cube.transform.position = pos
end- 添加
Rigidbody组件- 导入
import "UnityEngine" - 添加刚体组件
cube:AddComponent(Rigidbody)
- 导入
- 获取指定类型的组件,例如
BoxCollider- 导入
import "UnityEngine" - 自定类型字符串获取
local collider = cube:GetComponent("BoxCollider")
- 导入
由于使用Lua编写Unity项目时,无法与C#相比的是对象函数或者对象属性的智能提示不够健全.
所以在此贴出Slua框架中注入LuaState状态机对象内的一些UnityEngine的函数和属性.
UnityEngine.GameObject对象在LuaState中注入的内容static public void reg(IntPtr l) {
getTypeTable(l,"UnityEngine.GameObject");
addMember(l,GetComponent);
addMember(l,GetComponentInChildren);
addMember(l,GetComponentInParent);
addMember(l,GetComponents);
addMember(l,GetComponentsInChildren);
addMember(l,GetComponentsInParent);
addMember(l,SetActive);
addMember(l,CompareTag);
addMember(l,SendMessageUpwards);
addMember(l,SendMessage);
addMember(l,BroadcastMessage);
addMember(l,AddComponent);
addMember(l,CreatePrimitive_s);
addMember(l,FindGameObjectWithTag_s);
addMember(l,FindWithTag_s);
addMember(l,FindGameObjectsWithTag_s);
addMember(l,Find_s);
addMember(l,"transform",get_transform,null,true);
addMember(l,"layer",get_layer,set_layer,true);
addMember(l,"activeSelf",get_activeSelf,null,true);
addMember(l,"activeInHierarchy",get_activeInHierarchy,null,true);
addMember(l,"isStatic",get_isStatic,set_isStatic,true);
addMember(l,"tag",get_tag,set_tag,true);
addMember(l,"gameObject",get_gameObject,null,true);
createTypeMetatable(l,constructor, typeof(UnityEngine.GameObject),typeof(UnityEngine.Object));
}- 只有函数指针位置的部分,在Lua中定义成了Table变量内的函数,例如:
cube:AddCommponent - 在函数指针名的末尾部分以
_s结尾的,在Lua中定义成了元表变量内的函数,例如:GameObject.CreatePrimitive - 在添加成员时,包含了类似于
"transform"字符串的,在Lua中定义成了Table变量内的键值对属性,例如:cube.transform
- 只有函数指针位置的部分,在Lua中定义成了Table变量内的函数,例如:
UnityEngine.Transform对象在LuaState中注入的内容static public void reg(IntPtr l) {
getTypeTable(l,"UnityEngine.Transform");
addMember(l,SetParent);
addMember(l,Translate);
addMember(l,Rotate);
addMember(l,RotateAround);
addMember(l,LookAt);
addMember(l,TransformDirection);
addMember(l,InverseTransformDirection);
addMember(l,TransformVector);
addMember(l,InverseTransformVector);
addMember(l,TransformPoint);
addMember(l,InverseTransformPoint);
addMember(l,DetachChildren);
addMember(l,SetAsFirstSibling);
addMember(l,SetAsLastSibling);
addMember(l,SetSiblingIndex);
addMember(l,GetSiblingIndex);
addMember(l,Find);
addMember(l,IsChildOf);
addMember(l,FindChild);
addMember(l,GetChild);
addMember(l,"position",get_position,set_position,true);
addMember(l,"localPosition",get_localPosition,set_localPosition,true);
addMember(l,"eulerAngles",get_eulerAngles,set_eulerAngles,true);
addMember(l,"localEulerAngles",get_localEulerAngles,set_localEulerAngles,true);
addMember(l,"right",get_right,set_right,true);
addMember(l,"up",get_up,set_up,true);
addMember(l,"forward",get_forward,set_forward,true);
addMember(l,"rotation",get_rotation,set_rotation,true);
addMember(l,"localRotation",get_localRotation,set_localRotation,true);
addMember(l,"localScale",get_localScale,set_localScale,true);
addMember(l,"parent",get_parent,set_parent,true);
addMember(l,"worldToLocalMatrix",get_worldToLocalMatrix,null,true);
addMember(l,"localToWorldMatrix",get_localToWorldMatrix,null,true);
addMember(l,"root",get_root,null,true);
addMember(l,"childCount",get_childCount,null,true);
addMember(l,"lossyScale",get_lossyScale,null,true);
addMember(l,"hasChanged",get_hasChanged,set_hasChanged,true);
createTypeMetatable(l,null, typeof(UnityEngine.Transform),typeof(UnityEngine.Component));
}
转自:http://www.jianshu.com/p/fadb5dd59352
使用Slua框架开发Unity项目的重要步骤的更多相关文章
- YII框架开发一个项目的通用目录结构
YII框架开发一个项目的通用目录结构: 3 testdrive/ 4 index.php Web 应用入口脚本文件 5 assets/ 包含公开的资源文件 6 css/ 包含 CSS 文件 7 ima ...
- SSM框架开发web项目系列(二) MyBatis真正的力量
前言 上篇SSM框架环境搭建篇,演示了我们进行web开发必不可少的一些配置和准备工作,如果这方面还有疑问的地方,可以先参考上一篇“SSM框架开发web项目系列(一) 环境搭建篇”.本文主要介绍MyBa ...
- SSM框架开发web项目系列(三) MyBatis之resultMap及关联映射
前言 在上篇MyBatis基础篇中我们独立使用MyBatis构建了一个简单的数据库访问程序,可以实现单表的基本增删改查等操作,通过该实例我们可以初步了解MyBatis操作数据库需要的一些组成部分(配置 ...
- SSM框架开发web项目系列(五) Spring集成MyBatis
前言 在前面的MyBatis部分内容中,我们已经可以独立的基于MyBatis构建一个数据库访问层应用,但是在实际的项目开发中,我们的程序不会这么简单,层次也更加复杂,除了这里说到的持久层,还有业务逻辑 ...
- SSM框架开发web项目系列(一) 环境搭建篇
前言 开发环境:Eclipse Mars + Maven + JDK 1.7 + Tomcat 7 + MySQL 主要框架:Spring + Spring MVC + Mybatis 目的:快速上手 ...
- SSM框架开发web项目系列(六) SpringMVC入门
前言 我们最初的javaSE部分学习后,基本算是入门了,也熟悉了Java的语法和一些常用API,然后再深入到数据库操作.WEB程序开发,渐渐会接触到JDBC.Servlet/Jsp之类的知识,期间可能 ...
- Vue实例:vue2.0+ElementUI框架开发pc项目
开发前准备 vue.js2.0中文,项目所使用的js框架 vue-router,vue.js配套路由 vuex,状态管理 Element,UI框架 1,根据官方指引,构建项目框架 安装vue npm ...
- SSM框架开发web项目系列(七) SpringMVC请求接收
前言 在上篇Spring MVC入门篇中,我们初步了解了Spring MVC开发的基本搭建过程,本文将针对实际开发过程的着重点Controller部分,将常用的知识点罗列出来,并配以示例.在这之前,我 ...
- Vuejs+elementUI框架开发的项目结构及文件关系
项目结构|----- build #webpack编译相关文件目录,一般不用动 |----- config #配置目录| |------ dev.env.js #开发环境变量| |-- ...
随机推荐
- org.apache.log4j.Logger 详解
org.apache.log4j.Logger 详解 1. 概述 1.1. 背景 在应用程序中添加日志记录总的来说基于三个目的 :监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计 ...
- SAP HANA中创建分析权限(Analytic Privilege)
Demo Instruction: 假定CustomerID > 100的为VIP客户,我们的权限设置为只显示VIP客户 所使用的Attribute View: ATTR_CUSTOMER_FU ...
- Unity 发布到IOS,Android的各种坑
Unity 发布到IOS的注意事项1.开发环境MAC环境:Xcode环境 7.2.1Unity环境:Unity5.32.基本说明首先,我说一下,这是我在对Unity发布到IOS的实际使用中,总结出来的 ...
- 敏捷开发(六)- SCRUM全员会议
本文主要是为了检测你对SCRUM 全员会议的了解和使用程度,通过本文你可以检测一下 1.你们的SCRUM 全员会议的过程和步骤 2.SCRUM 全员会议的输出结果 一.会议目的 组成团 ...
- 《HTML5权威指南》
<HTML5权威指南> HTML元素: html字符实体 html全局属性 html base标签 用元数据元素说明文档 标记文字(第八章) 标记文字.组织内容.文档分节 表格元素 表单元 ...
- ajax使用json
json是什么什么的废话不说了,去百度吧.我这里介绍一下我为何要使用json.我使用ajax响应返回值时,项目中需求要返回多个值,不能只返回一个值.这时候就想起来用到json了.这可能只是json的一 ...
- 浙大pat1009题解
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 聚类算法K-Means, K-Medoids, GMM, Spectral clustering,Ncut
原文请戳:http://blog.csdn.net/abcjennifer/article/details/8170687 聚类算法是ML中一个重要分支,一般采用unsupervised learni ...
- nl2br()与nl2p()函数,php在字符串中的新行(\n)之前插入换行符
使用情景 很多场合我们只是简单用textarea获取用户的长篇输入,而没有用编辑器.用户输入的换行以“\n”的方式入库,输出的时候有时候会没有换行,一大片文字直接出来了.这个时候可以根据库里的“\n” ...
- tomcat设置http自动跳转为https访问
一.生成服务器端证书文件 可以使用Windows系统或者Linux系统 (1)Windows环境 条件:已经安装JDK 步骤: 1.在运行里输入cmd进入命令窗口 2.进入JDK安装目录 如D:/P ...