문서의 선택한 두 판 사이의 차이를 보여줍니다.
— |
kb:luacsharpbinding [2014/11/08 13:04] (현재) |
||
---|---|---|---|
줄 1: | 줄 1: | ||
+ | ====== Lua C# Binding ====== | ||
+ | [[Lua]]와 [[CSharp]] 간의 바인딩. | ||
+ | |||
+ | ====== Lua Interface 준비 ====== | ||
+ | 바인딩 라이브러리가 얼마나 있는지는 모르겠지만, 일단 [[http://luaforge.net/projects/luainterface/ | Lua Interface]]가 거의 표준(?)으로 자리잡은 모양이다. 배포본 받아서 어딘가에 적당히 풀어둔다. | ||
+ | |||
+ | C# 프로젝트를 만든 다음에, 실행 파일이 생기는 디렉토리에다 배포본 안의 bin 디렉토리에 있는 DLL 파일들을 복사한다. 아래의 그림에서 보다시피 복사해야할 DLL 파일은 lua50.dll, luainterface.dll, luanet.dll 이다. | ||
+ | |||
+ | {{luacsharpbinding_bin.gif}} | ||
+ | |||
+ | |||
+ | 그 다음 참조 항목으로 가서 LuaInterface.dll을 추가한다. "참조" 노드를 오른쪽 클릭해서, "참조 추가"를 선택하면 대화창이 뜨는데, "찾아보기" 탭에서 해당 DLL 파일을 지정하면 된다. | ||
+ | |||
+ | {{luacsharpbinding_ref.gif}} | ||
+ | |||
+ | 이걸로 준비 끝이다. 허무할 정도로 쉽다. | ||
+ | |||
+ | |||
+ | ====== 샘플 ====== | ||
+ | 버튼을 클릭하면 스크립트 파일을 실행하고, 스크립트에서는 C# 쪽에 있는 클래스 객체를 생성해서 다시 C# 쪽으로 돌려주고, C# 쪽에서는 루아 쪽에서 받은 객체에 대한 정보를 화면에 출력하는 샘플. | ||
+ | |||
+ | ==== MainForm.cs ==== | ||
+ | <code c#> | ||
+ | ...생략... | ||
+ | using LuaInterface; | ||
+ | |||
+ | namespace LuaTest | ||
+ | { | ||
+ | public partial class MainForm : Form | ||
+ | { | ||
+ | Lua LuaState = new Lua(); | ||
+ | List<Person> PeopleList = new List<Person>(); | ||
+ | bool FirstTime = true; | ||
+ | |||
+ | public void AddPersonToParty(Person p) | ||
+ | { | ||
+ | PeopleList.Add(p); | ||
+ | } | ||
+ | |||
+ | public MainForm() | ||
+ | { | ||
+ | InitializeComponent(); | ||
+ | LuaState.RegisterFunction("JoinParty", this, GetType().GetMethod("AddPersonToParty")); | ||
+ | } | ||
+ | |||
+ | private void TestButton_Click(object sender, EventArgs e) | ||
+ | { | ||
+ | if (FirstTime) | ||
+ | { | ||
+ | LuaState.DoFile("script.lua"); | ||
+ | Person p = PeopleList[0]; | ||
+ | |||
+ | ListViewItem item = new ListViewItem(); | ||
+ | item.Text = p.Name; | ||
+ | item.SubItems.Add(p.Description); | ||
+ | PeopleListView.Items.Add(item); | ||
+ | |||
+ | FirstTime = false; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public class Person | ||
+ | { | ||
+ | public string Name = ""; | ||
+ | public string Description = ""; | ||
+ | }; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | ==== Script.lua ==== | ||
+ | <code lua> | ||
+ | luanet.load_assembly("LuaTest"); | ||
+ | Person = luanet.import_type("LuaTest.Person"); | ||
+ | |||
+ | p = Person() | ||
+ | p.Name = "steve" | ||
+ | p.Description = "There's Steve drinking something brown and frothy from a shoe." | ||
+ | JoinParty(p) | ||
+ | </code> | ||
+ | |||
+ | ====== 링크 ====== | ||
+ | * [[http://luaforge.net/projects/luainterface/ | LuaInterface]], [[http://www.lua.inf.puc-rio.br/luanet/]] \\ LuaInterface is a library for integration between the Lua language and Microsoft .NET platform's Common Language Runtime (CLR). Lua scripts can use it to instantiate CLR objects, access properties, call methods, and even handle events with Lua functions. | ||
+ | * [[http://einfall.blogspot.com/]] \\ Game Scripting 항목 참고 | ||
+ | * [[http://www.devmafia.com/Tutorial-C-Sharp-Lua]] | ||
+ | ---- | ||
+ | * see also [[Lua]], [[CSharp]], [[LuaCppBinding]], [[LuaBinaryExtension]] |