01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | using UnityEngine; using System.Collections; using LuaInterface; public class HelloWorld : MonoBehaviour { / / Use this for initialization void Start ( ) { LuaState l = new LuaState ( ) ; string str = "print('hello world 世界')" ; l.DoString ( str ) ; } / / Update is called once per frame void Update ( ) { } } |
01 02 03 04 05 06 07 08 09 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 | using UnityEngine; using System.Collections; using LuaInterface; public class CreateGameObject 02 : MonoBehaviour { private string script = @ " luanet.load_assembly('UnityEngine') GameObject = UnityEngine.GameObject ParticleSystem = UnityEngine.ParticleSystem local newGameObj = GameObject('NewObj') newGameObj:AddComponent(ParticleSystem.GetClassType()) " ; / / 非反射调用 void Start ( ) { LuaScriptMgr lua = new LuaScriptMgr ( ) ; lua.Start ( ) ; lua.DoString ( script ) ; } / / Update is called once per frame void Update ( ) { } } |
01 02 03 04 05 06 07 08 09 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 | using UnityEngine; using System.Collections; using LuaInterface; public class AccessingLuaVariables 01 : MonoBehaviour { private string script = @ " luanet.load_assembly('UnityEngine') GameObject = luanet.import_type('UnityEngine.GameObject') ParticleSystem = luanet.import_type('UnityEngine.ParticleSystem') particles = {} for i = 1, Objs2Spawn, 1 do local newGameObj = GameObject('NewObj') local ps = newGameObj:AddComponent(luanet.ctype(ParticleSystem)) ps:Stop() table.insert(particles, ps) end var2read = 42 " ; void Start ( ) { LuaState l = new LuaState ( ) ; l[ "Objs2Spawn" ] = 5 ; / / 就是这里 l.DoString ( script ) ; print ( "Read from lua: " + l[ "var2read" ].ToString ( ) ) ; LuaTable particles = ( LuaTable ) l[ "particles" ]; foreach ( ParticleSystem ps in particles.Values ) { ps.Play ( ) ; } } void Update ( ) { } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | using UnityEngine; using System.Collections; using LuaInterface; public class ScriptsFromFile_ 01 : MonoBehaviour { public TextAsset scriptFile; / / Use this for initialization void Start ( ) { LuaState l = new LuaState ( ) ; l.DoString ( scriptFile. text ) ; } / / Update is called once per frame void Update ( ) { } } |
01 02 03 04 05 06 07 08 09 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 | using UnityEngine; using System.Collections; using LuaInterface; public class CallLuaFunction_ 01 : MonoBehaviour { private string script = @ " function luaFunc(message) print(message) return 42 end " ; void Start ( ) { LuaState l = new LuaState ( ) ; l.DoString ( script ) ; LuaFunction f = l.GetFunction ( "luaFunc" ) ; object[] r = f.Call ( "I called a lua function!" ) ; print ( r[ 0 ] ) ; } void Update ( ) { } } |
01 02 03 04 05 06 07 08 09 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | using UnityEngine; using System.Collections; using LuaInterface; public class LuaCoroutines : MonoBehaviour { private string script = @ " function fib(n) local a, b = 0, 1 while n > 0 do a, b = b, a + b n = n - 1 end return a end function CoFunc() print('Coroutine started') local i = 0 for i = 0, 10, 1 do print(fib(i)) coroutine.wait(1) end print('Coroutine ended') end function myFunc() coroutine.start(CoFunc) end " ; private LuaScriptMgr lua = null; void Awake ( ) { lua = new LuaScriptMgr ( ) ; lua.Start ( ) ; lua.DoString ( script ) ; LuaFunction f = lua.GetLuaFunction ( "myFunc" ) ; f.Call ( ) ; f.Release ( ) ; } void Update ( ) { lua.Update ( ) ; } void LateUpdate ( ) { lua.LateUpate ( ) ; } void FixedUpdate ( ) { lua.FixedUpdate ( ) ; } } |
01 02 03 04 05 06 07 08 09 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 | using UnityEngine; using System.Collections; namespace SimpleFramework { / / / < summary > / / / 全局构造器,每个场景里都有,所以每个场景都会初始化一遍,也会初始化游戏管理器一次 / / / 如果游戏管理器已经存在了,就跳过了,否则创建游戏管理器,来保证游戏里只有一个GameManager / / / < / summary > public class GlobalGenerator : MonoBehaviour { void Awake ( ) { InitGameMangager ( ) ; } / / / < summary > / / / 实例化游戏管理器 / / / < / summary > public void InitGameMangager ( ) { string name = "GameManager" ; gameObject.AddComponent < AppView > ( ) ; GameObject manager = GameObject.Find ( name ) ; if ( manager = = null ) { manager = new GameObject ( name ) ; manager. name = name ; AppFacade.Instance.StartUp ( ) ; / / 启动游戏 } } } } |
01 02 03 04 05 06 07 08 09 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 65 66 67 68 69 70 | using UnityEngine; using System; using System.Collections; using System.Collections.Generic; namespace SimpleFramework { public class AppConst { public const bool DebugMode = false ; / / 调试模式 - 用于内部测试 / / / < summary > / / / 如果想删掉框架自带的例子,那这个例子模式必须要 / / / 关闭,否则会出现一些错误。 / / / < / summary > public const bool ExampleMode = true ; / / 例子模式 / / / < summary > / / / 如果开启更新模式,前提必须启动框架自带服务器端。 / / / 否则就需要自己将StreamingAssets里面的所有内容 / / / 复制到自己的Webserver上面,并修改下面的WebUrl。 / / / < / summary > public const bool UpdateMode = false ; / / 更新模式 - 默认关闭 public const int TimerInterval = 1 ; public const int GameFrameRate = 30 ; / / 游戏帧频 public const bool UsePbc = true ; / / PBC public const bool UseLpeg = true ; / / LPEG public const bool UsePbLua = true ; / / Protobuff - lua - gen public const bool UseCJson = true ; / / CJson public const bool UseSproto = true ; / / Sproto public const bool LuaEncode = false ; / / 使用LUA编码 public const string AppName = "SimpleFramework" ; / / 应用程序名称 public const string AppPrefix = AppName + "_" ; / / 应用程序前缀 public const string ExtName = ".assetbundle" ; / / 素材扩展名 public const string AssetDirname = "StreamingAssets" ; / / 素材目录 public const string WebUrl = "http://localhost:6688/" ; / / 测试更新地址 public static string UserId = string .Empty; / / 用户ID public static int SocketPort = 0 ; / / Socket服务器端口 public static string SocketAddress = string .Empty; / / Socket服务器地址 } } |
01 02 03 04 05 06 07 08 09 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 | public void OnResourceInited ( ) { LuaManager.Start ( ) ; LuaManager.DoFile ( "Logic/Network" ) ; / / 加载游戏 LuaManager.DoFile ( "Logic/GameManager" ) ; / / 加载网络 initialize = true ; NetManager.OnInit ( ) ; / / 初始化网络 object[] panels = CallMethod ( "LuaScriptPanel" ) ; / / ---------------------Lua面板--------------------------- foreach ( object o in panels ) { string name = o.ToString ( ) .Trim ( ) ; if ( string .IsNullOrEmpty ( name ) ) continue ; name + = "Panel" ; / / 添加 LuaManager.DoFile ( "View/" + name ) ; Debug.LogWarning ( "LoadLua---->>>>" + name + ".lua" ) ; } / / ------------------------------------------------------------ CallMethod ( "OnInitOK" ) ; / / 初始化完成 } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 | require "3rd/pblua/login_pb" require "3rd/pbc/protobuf" local lpeg = require "lpeg" local json = require "cjson" local util = require "3rd/cjson.util" local sproto = require "3rd/sproto/sproto" local core = require "sproto.core" local print_r = require "3rd/sproto/print_r" require "Logic/luaclass" require "Logic/CtrlManager" require "Common/functions" require "Controller/PromptCtrl" |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | object[] panels = CallMethod ( "LuaScriptPanel" ) ; / / ---------------------Lua面板--------------------------- foreach ( object o in panels ) { string name = o.ToString ( ) .Trim ( ) ; if ( string .IsNullOrEmpty ( name ) ) continue ; name + = "Panel" ; / / 添加 LuaManager.DoFile ( "View/" + name ) ; Debug.LogWarning ( "LoadLua---->>>>" + name + ".lua" ) ; } |
01 02 03 04 05 06 07 08 09 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 | local transform; local gameObject; PromptPanel = { } ; local this = PromptPanel; --启动事件-- function PromptPanel.Awake ( obj ) gameObject = obj; transform = obj.transform; this.InitPanel ( ) ; warn ( "Awake lua--->>" ..gameObject. name ) ; end --初始化面板-- function PromptPanel.InitPanel ( ) this.btnOpen = transform : FindChild ( "Open" ) .gameObject; this.gridParent = transform : FindChild ( 'ScrollView / Grid' ) ; end --单击事件-- function PromptPanel.OnDestroy ( ) warn ( "OnDestroy---->>>" ) ; end |
01 02 03 04 05 06 07 08 09 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 | local transform; local gameObject; MessagePanel = { } ; local this = MessagePanel; --启动事件-- function MessagePanel.Awake ( obj ) gameObject = obj; transform = obj.transform; this.InitPanel ( ) ; warn ( "Awake lua--->>" ..gameObject. name ) ; end --初始化面板-- function MessagePanel.InitPanel ( ) this.btnClose = transform : FindChild ( "Button" ) .gameObject; end --单击事件-- function MessagePanel.OnDestroy ( ) warn ( "OnDestroy---->>>" ) ; end |
联系客服