Unity3D LuaComponent(基于ulua)
LuaComponent可以支持配一个需要执行在这个gameObject上的lua脚本,并且每个gameObject上的lua都是一个实例
using UnityEngine;
using LuaInterface;
using System.Collections.Generic; //Lua组件 public class LuaComponent : MonoBehaviour
{
//lua环境,需要在使用前给其赋值
public static LuaState s_luaState; //函数名字定义
protected static class FuncName
{
public static readonly string Awake = "Awake";
public static readonly string OnEnable = "OnEnable";
public static readonly string Start = "Start";
public static readonly string Update = "Update";
public static readonly string OnDisable = "OnDisable";
public static readonly string OnDestroy = "OnDestroy";
}; //lua路径,不用填缀名,可以是bundle
[Tooltip("script path")]
public string LuaPath; //预存函数提高效率
protected Dictionary<string, LuaFunction> mDictFunc = new Dictionary<string, LuaFunction>(); //lua表,当gameObject销毁时要释放
private LuaTable mSelfTable = null; //初始化函数,可以被重写,已添加其他
protected virtual bool Init()
{
if (string.IsNullOrEmpty(LuaPath))
{
return false;
} object[] luaRet = s_luaState.DoFile(LuaPath);
if (luaRet == null || luaRet.Length < )
{
Debug.LogError("Lua must return a table " + LuaPath);
return false;
} mSelfTable = luaRet[] as LuaTable;
if (null == mSelfTable)
{
Debug.LogError("null == luaTable " + LuaPath);
return false;
} AddFunc(FuncName.Awake);
AddFunc(FuncName.OnEnable);
AddFunc(FuncName.Start);
AddFunc(FuncName.Update);
AddFunc(FuncName.OnDisable);
AddFunc(FuncName.OnDestroy); return true;
} //保存函数
protected bool AddFunc(string name)
{
var func = mSelfTable.GetLuaFunction(name);
if (null == func)
{
return false;
}
mDictFunc.Add(name, func);
return true;
} //调用函数
protected void CallLuaFunction(string name, params object[] args)
{
LuaFunction func = null;
if (mDictFunc.TryGetValue(name, out func))
{
func.BeginPCall();
foreach (var o in args)
{
func.Push(o);
}
func.PCall();
func.EndPCall();
}
} void Awake()
{
Init();
CallLuaFunction(FuncName.Awake,mSelfTable,gameObject);
} void OnEnable()
{
CallLuaFunction(FuncName.OnEnable, mSelfTable, gameObject);
} void Start()
{
CallLuaFunction(FuncName.Start, mSelfTable, gameObject);
} void Update()
{
CallLuaFunction(FuncName.Update, mSelfTable, gameObject);
} void OnDisable()
{
CallLuaFunction(FuncName.OnDisable, mSelfTable, gameObject);
} void OnDestroy()
{
CallLuaFunction(FuncName.OnDestroy, mSelfTable, gameObject); //记得释放资源
foreach (var pair in mDictFunc)
{
pair.Value.Dispose();
}
mDictFunc.Clear();
if (null != mSelfTable)
{
mSelfTable.Dispose();
mSelfTable = null;
}
} }
lua脚本形如,记得最后一定要return 这个表 而且每个变量都得是local的
local Player = {}
local transform = nil;
local characterController = nil;
local moveDirection = Vector3.zero;
function Player:Awake(gameObject)
print("Awake");
transform = gameObject.transform;
characterController = gameObject:GetComponent('CharacterController');
end
function Player:Start( gameObject )
print("Start")
--gameObject.transform.localPosition = Vector3.New(200,100);
end
function Player:OnDestroy( gameObject )
print("OnDestroy")
end
function Player:Update(gameObject)
if (characterController.isGrounded) then
moveDirection = Vector3.New(Input.GetAxis("Horizontal"), , );
moveDirection = transform:TransformDirection(moveDirection);
moveDirection = moveDirection * ;
end
-- Apply gravity
moveDirection.y =moveDirection.y- * Time.deltaTime;
characterController:Move(moveDirection * Time.deltaTime);
end
return Player;
Unity3D LuaComponent(基于ulua)的更多相关文章
- Thinking in Unity3D:基于物理着色(PBS)的材质系统
关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的引擎 ...
- Unity3d 开发之 ulua 坑的总结
相同的 lua 代码在安卓上能正常运行,但在 IOS 上可能不会正常运行而导致报红,崩溃等,我在使用 lua 编程时遇到的一些坑总结如下: 1. File.ReadAllText, 诸如以下代码在 i ...
- Unity3D LuaBundleLoader(基于cslua)
说明:异步加载lua的bundle,会优先加载cache目录下bundle(一般更新的资源都在cache下) using System; using UnityEngine; using System ...
- unity3d热更新插件uLua学习整理
前言 IOS不能热更新,不是因为不能用反射,是因为System.Reflection.Assembly.Load 无法使用System.Reflection.Emit 无法使用System.CodeD ...
- Thinking in Unity3D
关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的引擎 ...
- Unity3D游戏开发初探—1.跨平台的游戏引擎让.NET程序员新生
一.Unity3D平台简介 Unity是由Unity Technologies开发的一个让轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的 ...
- 【Unity3D】AR应用中,关于东南西北方位的判断。
这篇文章的应用场景是这样子的: 首先我们要做的是一个带有LBS定位服务(比如高德地图.百度地图等)AR功能,在这个场景中,会有一些地图上的”点“(如派出所.学校)是我们需要显示在我们的AR镜头上的,如 ...
- Unity3D 装备系统学习Inventory Pro 2.1.2 总结
前言 写在最前面,本文未必适合纯新手,但有一些C#开发经验的还是可以看懂的,虽然本人也是一位Unity3D新人,但是本文只是自己在学习Inventory Pro的学习总结,而不是教程,本人觉得要读懂理 ...
- 在Unity3D的网络游戏中实现资源动态加载
用Unity3D制作基于web的网络游戏,不可避免的会用到一个技术-资源动态加载.比如想加载一个大场景的资源,不应该在游戏的开始让用户长时间等待全部资源的加载完毕.应该优先加载用户附近的场景资源,在游 ...
随机推荐
- js 检测页面刷新或关闭
window.onbeforeunload=function(){ //要提交的内容 return "随意写";//必须有return ,不然只有ie有效,chrome无效 }
- Docker-compose
docker-compose:未找到命令 安装: 须切到root用户: curl -L https://github.com/docker/compose/releases/download/1.7. ...
- php 字符串和数字比较一些问题
本文章来给大家介绍关于php 字符串和数字比较一些问题,因为数字与字符在php中是不同的数据类型,所以在比较时可能会有很多的问题. ,1,2等等,其中0标示成功,其他表示不同的错误代码.程序通过 if ...
- Android 在非Activity的类中调用startActivityForResult
http://www.360doc.com/content/11/0720/10/7322578_134657348.shtml
- 关于Deprecated: mysql_result: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
require_once('connect.php'); $sql = "select * from introduce"; \(query = mysql_query(\)sql ...
- 分布式数据库Hbase
HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. HBase是Goog ...
- the pipeline of call SNP
######################################## ############### Mapping ################ ################## ...
- bzoj2083【Poi2010】Intelligence test
听说正解是链表,然而被我暴力水过了 先开vector记录每个数在原串中出现的位置 之后对于每个匹配串的每一位,找比当前位置大的第一个当前元素是哪个,有就更新,没有就"NIE" #i ...
- java中Integer,String判断相等与integer的比较大小
package sfk.bbs.test.springjsbctempletTest; import static org.junit.Assert.*; import org.junit.Test; ...
- mysql 函数 GROUP_CONCAT 单元格中最长字符串和excel导出问题
GROUP_CONCAT 使用方式GROUP_CONCAT ([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符']) SELECT ...