? ? ”熱更新“ “熱修復”都是在程序應用中常見的詞匯 按照理解 就是動態下修改代碼 從而更新代碼 而不需要發布新的版本 修復或者增加新功能 常見的如:蘋果app應用中 開發者無需從新提交版本審核 直接就可以動態下增加功能與修改BUG 避免長時間應用審核以及多次被蘋果拒絕而造成的成本。
Step1:下載最新uLua插件 ulua_v1.25.unitypackage 導入工程
Step2:我們按照它的Demo來學習相關知識
Step3: 腳本里都要導入命名空間 LuaInterface
Demo1 : 輸出 hello world
demo的寫法: 這樣 unity輸出控制臺就能輸出hello world ?是不是一定要這樣寫呢? 我嘗試了下另外一種寫法?
void ?Start()
{
?LuaState l = new LuaState() ;
string ?str = @"print('hello world')"?
l .DoString(str)?
}
我的寫法:我是這么想的 為啥不可以直接輸出呢? 在單獨lua編寫中 寫一個字符串是var = 'hello world' ? 現在寫在untity里面 那么就要轉換 那么 我猜 I,DoString(str)的作用就是在unitu里執行lua代碼轉換成可以識別的unity代碼 ?然后根據內部的封裝規則 規定好 I["var"]是拿到這個字符串 注:string = @"" ; @的作用是自動換行陳列 沒有其他的含義
void Start()
{
LuaState l = new LuaState()
string str = "var = @'hello world'"
l.DoString(str)?
print(l["var"].ToString())?
}
Demo2: ?用非反射的方式創建一個粒子特效對象
demo寫法:uLua 都是將要執行的uLua代碼寫成string類型 然后用封裝的類對調用它 LuaScriptMgr 配套使用lua.Start() ; ?DoString(script)語法的含義是在c#中執行Lua語句 ? start()里面的語句我是這么理解 首先你要告訴lua 我要引入c#中unityEngine頭文件的對象 然后告訴它具體的類型?
然后創建對象 然后添加組件
public class CreatGameObject:MonBehaviour
{
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) ;
}
public class AccessingLuaVariables:MonoBehaviour
{
? // cstolua要求必須先定義變量才能使用
?private string var = @"Objs2Spawn = 0" ;
private string script = @"
?lucent.load_assembly('UnityEngine')
ParticleSystem = UnityEngine.ParticleSystem
particles = {}
for i = 1,Objs2Spawn,1 do
local newGameObj = GameObject('NewObj'..toString(i))
local ps = newGameObj:AddComponent(ParticleSystem.GetClassType())
ps.Stop();
table.insert(particle,ps)
end
var2read = 42
" ;
void Start()
{
? ?LuaScriptMgr mgr = new LuaScriptMgr()
? mgr.Start() ;
LuaState l = mgr.lua ;
l.DoString(var) ;
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() ;
}
}
}
持續更新