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

参考:

http://www.myexception.cn/c-sharp/1881698.html

  1. 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#函数里,实现相同的效果。若哪位大神能帮助小弟,小弟必须谢谢你哈!






评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值