您的位置:首页 > 编程语言 > Lua

C#如何访问Lua中的属性(3)

2016-08-19 00:37 429 查看
参考:
http://www.myexception.cn/c-sharp/1881698.html
Lua如何访问C#中的属性

1) .LUA如何访问Unity提供的对象?

  a.如何new系统对象?

  b.如何访问对象的属性?

  c.如何访问对象的函数?

2) .LUA如何访问在C#中自定义的对象?

  a.如何new自定义对象?

  b.如何访问对象的属性?

  c.如何访问对象的函数?

/*
Lua如何访问C#中的属性
1) .LUA如何访问Unity提供的对象?
a.如何new系统对象?
b.如何访问对象的属性?
c.如何访问对象的函数?
2) .LUA如何访问在C#中自定义的对象?
a.如何new自定义对象?
b.如何访问对象的属性?
c.如何访问对象的函数?
*/

using UnityEngine;
using System.Collections;

public class Hero{

private string _name;
private int _hp, _ap, _mp;

public Hero(string name, int hp, int ap, int mp)
{
this._name = name;
this._hp = hp;
this._ap = ap;
this._mp = mp;
}

public int Hp
{
set { }
get { return this._hp; }
}

public int Ap
{
set { }
get { return this._ap; }
}

public int Mp
{
set { }
get { return this._mp; }
}

public void PrintInfor()
{
Debug.Log("hp:" + this._hp + "__ap:" + this._ap + "__mp:" + this._mp);
}

public int AddHp(int add)
{
Debug.Log("Lua调用CS的AddHp Lua 传的参数 add:" + add);
this._hp += add;
return _hp;
}

public int AddAp(int add)
{
Debug.Log("Lua调用CS的AddAp Lua传的参数 add:" + add);
this._ap += add;
return _ap;
}

public int AddMp(int add)
{
Debug.Log("Lua调用CS的AddMp Lua传递的参数 add:" + add);
this._mp += add;
return _mp;
}

public void Destroy()
{
this._name = null;
}
}


/*
C#如何访问Lua中的属性
1) .C#如何访问LUA中的属性?
2) .C#如何访问LUA中的函数?
3) .C#如何访问LUA中的表?
*/

using UnityEngine;
using System.Collections;
//
using LuaInterface;

public class Main : MonoBehaviour {

private static Main instance;
public string ss;
public TextAsset tt;

// Use this for initialization
void Start () {
instance = this;
}
// Update is called once per frame
void Update()
{
}

//C#调用LUA
void testCSharp_GoLua()
{
LuaState lua = new LuaState();
lua.DoString("print'hello world'");
}

//C#调用LUAFile
void testCSharp_GoLuaFile()
{
LuaState lua = new LuaState();
//lua.DoFileFromAge(this, "Test0.lua");
//TextAsset file = (TextAsset)Resources.Load("test0", typeof(TextAsset));
//if (file == null)
//{
//    Debug.Log("sdfds");
//}
//else {
//    Debug.Log("加载成功");
//}
lua.DoFile("test0");
}

void testCSharp_GoLuaInfor()
{
LuaState lua = new LuaState();
lua.DoFile("test0");
// 访问LUA中的表
LuaTable configTable = lua.GetTable("config");
Debug.Log("name:" + configTable["name"]);
Debug.Log("age:" + configTable["age"]);
Debug.Log("qq:" + configTable["qq"]);
// 访问Lua 中的基础属性
Debug.Log("Name:" + lua.GetString("Name"));
Debug.Log("Age" + lua.GetNumber("Age"));
Debug.Log("isBoy" + lua["isBoy"]);
//访问Lua中的函数
LuaFunction luaFun = lua.GetFunction("PrintFromLua");
if (luaFun != null)
{
System.Object[] obResult = luaFun.Call(100);
Debug.Log("obResult" + obResult[0]);
}
}

// Lua调用 c#
void testLua_GOCSharp()
{
LuaState lua = new LuaState();
lua.DoFile("test1");
GameObject.Find("ageObj").AddComponent<Animation>();
}

void OnGUI()
{
if (GUILayout.Button("第一节.C#调用LUA"))
{
testCSharp_GoLua();
}

if (GUILayout.Button("第二节.C#调用LUA File"))
{
testCSharp_GoLuaFile();
}
if (GUILayout.Button("第三节.C#调用LUA 信息"))
{
testCSharp_GoLuaInfor();
}
if (GUILayout.Button("第四节.LUA调用C# 信息"))
{
testLua_GOCSharp();
}
}
}



--[[ http://www.myexception.cn/c-sharp/1881698.html @author:涛涛
@des:测试Lua访问c#的一些东东
@date:2016-8-18
--]]

--访问c#中的系统的对象
luanet.load_assembly("UnityEngine");
GameObject = luanet.import_type("UnityEngine.GameObject");
Vector3 = luanet.import_type("UnityEngine.Vector3");

luanet.load_assembly('Assembly-CSharp');

local newObj = GameObject('ageObj');
--访问C#中的系统的对象的函数
--nimation = luanet.import_type("UnityEngine.Animation");
--newObj:AddComponent<span style="font-family:Arial, Helvetica, sans-serif;">(</span>Animation);
--访问C#中的系统的对象的属性
newObj.transform.position = Vector3(10, 20, 30);

--访问C#中的自定义的对象
Hero=luanet.import_type("Hero");
--newC#中的自定义的对象
local heroa = Hero("大哥", 100, 200, 300);
--访问C#中的自定义的对象的函数
heroa:PrintInfor();

--访问C#中的自定义的对象的函数 来回传值
print("LUA hp:", heroa:AddHp(10));
print("LUA ap:", heroa:AddAp(20));
print("LUA mp:", heroa:AddMp(30));

print("test1.lua执行完毕。。。");




单击“第四节。。。。。” 以下是输出内容 



--newObj:AddComponent<span style="font-family:Arial, Helvetica, sans-serif;">(</span>Animation);
 在 test1.txt  中这句有错误,这还是较老版本的,语法 ,新的版本 是  gameObject.AddComponent<Animation>();

但是在Lua语言中,我实在没有找到 怎么用Lua代码重写,我只好写在其他c#函数里,实现相同的效果。若哪位大神能帮助小弟,小弟必须谢谢你哈!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: