SLua框架安装
1.https://github.com/pangweiwei/slua/wiki下载最新版,将Assets目录里的所有内容复制到工程中,对于最终产品,可以删除例子,文档等内容,如果是开发阶段则无所谓。
2.等待unity编译完毕,如果一切顺利的话,将出现SLua菜单,点击SLua菜单中All->Make命令,手动生成针对当前版本的U3d接口文件。
3.每次更新SLua版本,务必记得All->Clear,然后All->Make,否则可能运行不正确。
效果:
C#代码如下:
using UnityEngine;
using System.IO;
using SLua;
using UnityEngine.UI;
//public delegate byte[] LuaReourcesFileLoader(string fn, ref string absoluteFn);
public class LoadLua : MonoBehaviour
{
LuaSvr svr;
LuaTable self;
LuaFunction update;
[CustomLuaClass]
public delegate void UpdateDelegate(object self);
UpdateDelegate ud;
void Start()
{
svr = new LuaSvr();
LuaSvr.mainState.loaderDelegate += LuaFileLoader;
svr.init(null, () => // 如果不用init方法初始化的话,在Lua中是不能import的
{
// 这里为了测试就不先判断为空,开发的时候再加上
self = (LuaTable)svr.start( Application.streamingAssetsPath + "/Lua/SLuaTest.txt");
update = (LuaFunction)self["update"];
ud = update.cast<UpdateDelegate>();
});
}
//public float i=0.01f;
//public Color color_ = new Color(0, 1, 1);
void Update()
{
//RGB颜色渐变效果
//Color(0,1,1)
//-G +R -B +G -R +B
//if (color_.r == 0 && color_.g > 0 && color_.b == 1)
//{
// color_.g -= i;
// if (color_.g<0) {
// color_.g = 0;
// }
//}
//else if (color_.g == 0 && color_.r < 1 && color_.b == 1)
//{
// color_.r += i;
// if (color_.r > 1)
// {
// color_.r = 1;
// }
//}
//else if (color_.r == 1 && color_.g == 0 && color_.b > 0)
//{
// color_.b -= i;
// if (color_.b < 0)
// {
// color_.b = 0;
// }
//}
//else if (color_.b == 0 && color_.r == 1 && color_.g < 1)
//{
// color_.g += i;
// if (color_.g > 1)
// {
// color_.g = 1;
// }
//}
//else if (color_.r > 0 && color_.g == 1 && color_.b == 0)
//{
// color_.r -= i;
// if (color_.r < 0)
// {
// color_.r = 0;
// }
//}
//else if (color_.r == 0 && color_.g == 1 && color_.b < 1)
//{
// color_.b += i;
// if (color_.b > 1)
// {
// color_.b = 1;
// }
//}
//A.transform.GetComponent<Renderer>().material.color = color_;
if (ud != null) ud(self);
}
private static byte[] LuaFileLoader(string strFile, ref string fn)
{
string filename = strFile;
return File.ReadAllBytes(filename);
}
public Sprite ResourcesSprite(string path)
{
return Resources.Load<Sprite>(path);
}
}
Lua代码如下:
--lua代码文件名称:SLuaTest.txt
import "UnityEngine"
import "UnityEngine.UI"
local class={}
IsRunning=true;
function main() --程序启动自动调用该方法 类似Unity中的Start方法
cs = GameObject.Find("LoadLuaManager") --Lua调用CS代码
camera=Camera.main
camera.clearFlags = CameraClearFlags.SolidColor
camera.backgroundColor = Color.black
print("Lua创建了一个Cube")
cube = GameObject.CreatePrimitive(PrimitiveType.Cube)
cube:AddComponent(UnityEngine.AudioSource).clip=Resources.Load("One Last Kiss(Instrumental)")
cube.transform:GetComponent(AudioSource).loop = true
cube.transform:GetComponent(AudioSource):Play()
--cube.transform:GetComponent(Renderer).material=Resources.Load("Material") 从Resources获取材质球的方法
cube.transform:GetComponent(Renderer).material.color=Color.red; --直接设置材质的颜色
cube.transform.localPosition =Vector3(0, 1, -5)
MainTransform = GameObject.Find("Canvas/MainPlant").transform
GameObject.Instantiate(Resources.Load("Button"),Vector3(960, 1000, 0),Quaternion.identity,MainTransform).name="Button1"
But=GameObject.Find("Canvas/MainPlant/Button1")
But.transform.localScale= Vector3(2, 2, 2);
GameObject.Find("Canvas/MainPlant/Button1/Text"):GetComponent(UI.Text).text="切换画面填充模式"
GameObject.Instantiate(Resources.Load("Button"),Vector3(1250, 1000, 0),Quaternion.identity,MainTransform).name="Button2"
But2=GameObject.Find("Canvas/MainPlant/Button2")
But2.transform.localScale= Vector3(1.5, 1.5, 1.5);
GameObject.Find("Canvas/MainPlant/Button2/Text"):GetComponent(UI.Text).text="切换颜色"
GameObject.Instantiate(Resources.Load("Button"),Vector3(360, 700, 0),Quaternion.identity,MainTransform).name="Button3"
But3=GameObject.Find("Canvas/MainPlant/Button3")
But3.transform.localScale= Vector3(2, 2, 2);
GameObject.Destroy( GameObject.Find("Canvas/MainPlant/Button3/Text")) --在Lua中如何删除物体
But3:GetComponent(RectTransform).sizeDelta=Vector2(60,60);
But3:GetComponent(UI.Image).sprite= cs:GetComponent("LoadLua"):ResourcesSprite("播放")
But:GetComponent(Button).onClick:AddListener(function ()
print("Lua中的Button监听事件触发");
if( camera.clearFlags==CameraClearFlags.SolidColor)
then
But2.transform.localScale= Vector3(1.5, 1.5, 1.5);
camera.clearFlags =CameraClearFlags.Skybox
elseif(camera.clearFlags==CameraClearFlags.Skybox)
then
But2.transform.localScale= Vector3(0,0,0);
camera.clearFlags =CameraClearFlags.Nothing
elseif(camera.clearFlags==CameraClearFlags.Nothing)
then
But2.transform.localScale= Vector3(0,0,0);
camera.clearFlags =CameraClearFlags.Depth
elseif(camera.clearFlags==CameraClearFlags.Depth)
then
But2.transform.localScale= Vector3(1.5, 1.5, 1.5);
camera.clearFlags=CameraClearFlags.SolidColor
end
end);
But2:GetComponent(Button).onClick:AddListener(function ()
if(camera.backgroundColor == Color.black)
then
camera.backgroundColor =Color.white
elseif(camera.backgroundColor==Color.white)
then
camera.backgroundColor = Color.blue
elseif(camera.backgroundColor==Color.blue)
then
camera.backgroundColor = Color.red
elseif(camera.backgroundColor==Color.red)
then
camera.backgroundColor = Color.green
elseif(camera.backgroundColor==Color.green)
then
camera.backgroundColor =Color(1,1,0)
elseif(camera.backgroundColor==Color(1,1,0))
then
camera.backgroundColor = Color.clear
elseif(camera.backgroundColor==Color.clear)
then
camera.backgroundColor = Color.cyan
elseif(camera.backgroundColor==Color.cyan)
then
camera.backgroundColor = Color(0.5,0.5,0.5)
elseif(camera.backgroundColor==Color(0.5,0.5,0.5))
then
camera.backgroundColor = Color.magenta
elseif(camera.backgroundColor==Color.magenta)
then
camera.backgroundColor = Color.black
end
--Color.white Color.clear Color.cyan Color.red Color.green Color.gray Color.grey Color.magenta Color.yellow
end);
But3:GetComponent(Button).onClick:AddListener(function ()
if(cube.transform:GetComponent(AudioSource).isPlaying)
then
cube.transform:GetComponent(AudioSource):Pause()
But3:GetComponent(UI.Image).sprite= cs:GetComponent("LoadLua"):ResourcesSprite("暂停")
IsRunning=false
else
cube.transform:GetComponent(AudioSource):Play()
But3:GetComponent(UI.Image).sprite= cs:GetComponent("LoadLua"):ResourcesSprite("播放")
IsRunning=true
end
end);
Text = GameObject.Instantiate(Resources.Load("Text"),Vector3(1600, 650, 0),Quaternion.identity,MainTransform)
Text:GetComponent(UI.Text).color = Color.white;
Text:GetComponent(UI.Text).fontSize = 35;
Text:GetComponent(RectTransform).sizeDelta=Vector2(600,300);
Text1 = GameObject.Instantiate(Resources.Load("Text"),Vector3(1600, 450, 0),Quaternion.identity,MainTransform)
Text1:GetComponent(UI.Text).color = Color.white;
Text1:GetComponent(UI.Text).fontSize = 35;
Text1:GetComponent(RectTransform).sizeDelta=Vector2(600,300);
Text2 = GameObject.Instantiate(Resources.Load("Text"),Vector3(1600, 250, 0),Quaternion.identity,MainTransform)
Text2:GetComponent(UI.Text).color = Color.white;
Text2:GetComponent(UI.Text).fontSize = 35;
Text2:GetComponent(RectTransform).sizeDelta=Vector2(600,300);
Text3 = GameObject.Instantiate(Resources.Load("Text"),Vector3(600, 800, 0),Quaternion.identity,MainTransform)
Text3:GetComponent(UI.Text).color = Color.white;
Text3:GetComponent(UI.Text).fontSize = 35;
Text3:GetComponent(RectTransform).sizeDelta=Vector2(600,100);
Slider_= GameObject.Instantiate(Resources.Load("Slider"),Vector3(960, 100, 0),Quaternion.identity,MainTransform)
Slider_.transform.localScale= Vector3(2, 2, 2);
Slider_:GetComponent(UI.Slider).value=1;
Slider_:GetComponent(UI.Slider).onValueChanged:AddListener(
function(v)
cube.transform.localScale= Vector3(v, v, v);
cube.transform:GetComponent(AudioSource).volume=v
end
)
--if you want to use update function,you need return class
--如果你想去使用Update函数,你需要在main方法返回class
return class
end
ColorTimeSpeed=0.001
color_ =Color(0,1,1)
minute_=60
function class:update() --Lua中使用update 需要继承在Main 返回的Class
--print("Lua Update 正在执行...")
if (IsRunning)
then
cube.transform.localRotation= Quaternion.Euler(45,45,math.sin(Time.time)*180)
if (color_.r == 0 and color_.g > 0 and color_.b == 1)
then
color_.g =color_.g - ColorTimeSpeed
if (color_.g<0)
then
color_.g = 0
end
elseif (color_.g == 0 and color_.r < 1 and color_.b == 1)
then
color_.r =color_.r + ColorTimeSpeed
if (color_.r > 1)
then
color_.r = 1
end
elseif (color_.r == 1 and color_.g == 0 and color_.b > 0)
then
color_.b =color_.b - ColorTimeSpeed
if (color_.b < 0)
then
color_.b = 0
end
elseif (color_.b == 0 and color_.r == 1 and color_.g < 1)
then
color_.g =color_.g + ColorTimeSpeed
if (color_.g > 1)
then
color_.g = 1
end
elseif (color_.r > 0 and color_.g == 1 and color_.b == 0)
then
color_.r = color_.r - ColorTimeSpeed
if (color_.r < 0)
then
color_.r = 0
end
elseif (color_.r == 0 and color_.g == 1 and color_.b < 1)
then
color_.b =color_.b + ColorTimeSpeed
if (color_.b > 1)
then
color_.b = 1
end
end
cube.transform:GetComponent(Renderer).material.color= color_
Text:GetComponent(UI.Text).text= "<color=red>COLOR:</color>\nR:"..color_.r.."\nG:"..color_.g.."\nB:"..color_.b
Text1:GetComponent(UI.Text).text= "<color=red>ROTATION:</color>\nx:"..cube.transform.localRotation.x.."\ny:"..cube.transform.localRotation.y.."\nz:"..cube.transform.localRotation.z
Text2:GetComponent(UI.Text).text= "<color=red>SCALE:</color>\nx:"..cube.transform.localScale.x.."\ny:"..cube.transform.localScale.y.."\nz:"..cube.transform.localScale.z
if(cube.transform:GetComponent(AudioSource).isPlaying)
then
Text3:GetComponent(UI.Text).text="<color=red>MUSIC TIME:</color>\n"..cube.transform:GetComponent(AudioSource).time / minute_.."/"..cube.transform:GetComponent(AudioSource).clip.length / minute_
end
end
end