Unity3d 플러그인 - managed plugins

Mobile Programming/Unity3d 2015. 4. 20. 20:43

plugin

managed plugins

http://docs.unity3d.com/Manual/UsingDLL.html

dll 로 외부 컴파일러로 빌드, 스크립트를 컴파일 하는 것도 가능. 프로젝트에 추가되어 어떤 다른 오브젝트든, 보통 스크립트처럼 사용 가능하다.

  • Unity 에서 제공하지 않는 언어로 개발된 바이너리 로드
  • Unity 코드를 소스제공 없이 이용할 수 있게 배포

Creating a DLL

  • .NET 코드를 생성하는 컴파일러를 선택
  • Unity API 를 사용하지 않는다면 컴파일러 옵션을 적절하게 선택하는 것만으로 DLL 을 생성
  • Unity API 를 사용한다면 Unity 자체의 DLL 을 컴파일러에서 사용가능하도록 만들어야한다.
Unity API
  1. mac

    • UnityEngine.dll / UnityEditor.dll
    • /Applications/Unity/Unity.app/Contents/Frameworks/Managed/
    • contextual menu (right click on unity) > Show Package Contents command
  2. windows

    • UnityEngine.dll / UnityEditor.dll
    • C:\Program Files\Unity\Editor\Data\Managed
  3. compile option example

    1. mcs -r:/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEngine.dll -target:library ClassesForDLL.cs
    2. -r : included library path
    3. -target : build type
    4. (path) name
Tutorial
  1. Add Reference on Unity DLL

    1. mac (MonoDevelop)

      • References > Edit References > .Net Assembly tab > File System > select file.
    2. windows

      • References > Add Reference > Browse > select file
  2. Write DLL Code

  3. Using DLL in Unity
    1. copy builded dll into asset folder
  4. Setting UP Debugging
    1. mac
      • copy mdb into Assets/Plugins
      • Add-in Manager > Installed tab > Unity > select Mono Soft Debugger Support For Unity > Enable
    2. windows
      • pdb to mdb
      • Program Files\Unity\Editor\Data\Mono\lib\mono\2.0\pdb2mdb.exe


Daytime.2 - A synchronous TCP daytime server

Programming/Boost asio 2015. 4. 19. 13:05

다음에 기반함

http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/tutorial/tutdaytime2.html


이번엔 TCP asio 를 사용하여 어떻게 서버 응용프로그램을 구현할 수 있는지 살펴본다.

#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;


클라이언트로 다시 보내줄 문자열을 생성하는 make_daytime_string() 함수를 정의해보겠다.

이 함수는 daytime 서버 응용프로그램 내에서 재사용 될 것이다.

std::string make_daytime_string()
{
  using namespace std; // For time_t, time and ctime;
  time_t now = time(0);
  return ctime(&now);
}

int main()
{
  try
  {
    boost::asio::io_service io_service;


ip::tcp::acceptor 객체는 새로운 커넥션을 listen 하기 위해 생성하였다. IPv4, TCP 포트 13 을 listen 하도록 초기화 했다. 

    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));


한번에 하나의 커넥션을 다루는 iterative 서버가 될 것이다. 클라이언트의 커넥션을 표현할 socket 를 생성하고, 커넥션을 기다리도록 하자. 

 for (;;)
    {
      tcp::socket socket(io_service);
      acceptor.accept(socket);


클라이언트가 우리의 서비스에 접속했다. 현재 시간을 확인하고 클라이언트로 정보를 보낸다.

      std::string message = make_daytime_string();

      boost::system::error_code ignored_error;
      boost::asio::write(socket, boost::asio::buffer(message), ignored_error);
    }
  }


마지막으로, 예외를 처리한다. 

 catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}


- full_source code



- main



- 실행결과


클라이언트바이너리를 별도로 만들어놓은 후, ( 이전 포스트 ) 서버실행후 클라이언트를 실행했다.




Daytime.1 - A synchronous TCP daytime client

Programming/Boost asio 2015. 4. 13. 01:17

다음 내용에 기반함


Introduction to Sockets

The tutorial programs in this section show how to use asio to develop simple client and server programs. These tutorial programs are based around the daytimeprotocol, which supports both TCP and UDP.

The first three tutorial programs implement the daytime protocol using TCP.


이번 튜토리얼에서는 TCP 클라이언트를 만드는데 어떻게 이용 할 수 있을지 소개한다.

먼저 필요한 헤더파일들을 include 하는 것으로 시작한다.
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

이 응용프로그램은 daytime 서비스에 접근하는것이며, 따라서 유저가 서버를 특정할 필요가 있다.

using boost::asio::ip::tcp; int main(int argc, char* argv[]) { try { if (argc != 2) { std::cerr << "Usage: client <host>" << std::endl; return 1; }


asio 를 이용하는 모든 프로그램들은 적어도 하나이상의 io_service 객체를 필요로한다. 
    boost::asio::io_service io_service;


파라미터에 지정된 서버 이름을 TCP endpoint 로 변환할 필요가 있는데, 이는 ip::tcp::resolver 객체를 통한다.

  tcp::resolver resolver(io_service);


resolver 는 쿼리 객체를 받아 이를 endpoint 의 리스트로 변환한다. 쿼리이름을 서버이름을 이용하여 생성할 것이며, 이 서비스의 이름은 "daytime" 이다.

리스트는  ip::tcp::resolver::iterator 타입으로 리턴된다. (ip::tcp::resolver::iterator 의 기본 생성자를 통해 생성된 객체는 end iterator 로 사용 될 수 있다)

   tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);


이제 소켓을 생성하고 연결할 차례다. 

얻어진 리스트는 IPv4 와 IPv6 를 모두 포함할 수도 있으므로 동작하는 것을 찾기 위해 각각의 주소들에 대해 시도해야한다. 

이런 사용이, 클라이언트 프로그램이 특정 IP 주소체계와 독립적으로 프로그램을 구성할 수 있게 해준다. 

boost::asio::connect() 함수는 자동적으로 이를 수행해준다.

    tcp::socket socket(io_service);
    boost::asio::connect(socket, endpoint_iterator);


이제 연결이 수립되었다. daytime 서비스의 응답만을 기다리면 된다.


boost::array 를 전달받은 데이터를 저장하는데 사용하도록 하자. boost::asio::buffer() 함수는 자동적으로 배열의 사이즈를 결정해주어 버퍼 오버런이 발생하지 않게 도와준다. boost::array 대신에, char[] 나 std::vector 도 사용할 수 있다.

 for (;;)
    {
      boost::array<char, 128> buf;
      boost::system::error_code error;

      size_t len = socket.read_some(boost::asio::buffer(buf), error);


서버가 연결을 종료했을때, ip::tcp::socket::read_some 함수는 boost::asio::error::eof 에러와 함께 종료될 것이며, 이때 루프를 빠져나가면 된다.

if (error == boost::asio::error::eof) break; // Connection closed cleanly by peer. else if (error) throw boost::system::system_error(error); // Some other error. std::cout.write(buf.data(), len); }


마지막으로, thrown 된 예외를 처리하면 된다. 


full source code




main 


실행결과