jacksondunstan / UnityNativeScripting

Unity Scripting in C++
https://jacksondunstan.com/articles/3938
MIT License
1.34k stars 135 forks source link

can't use std::string? #26

Closed sonygod closed 5 years ago

sonygod commented 5 years ago

can't use std::string?

how to implement Debug.Log("hello world"+2019)?

jacksondunstan commented 5 years ago

Hi @sonygod, you're free to use std::string if you like. Debug.Log takes an System.Object in C#, so you'll need to pass a System::Object to Debug::Log in C++. System::String derives from System::Object, so you can pass one of them. The most useful constructor for creating the System::String on the C++ side, as in your example, is probably the one that take a const char* (a.k.a. C string). You can easily get one of those from std::string by calling the c_str member function. Putting this all together, here's how your example could be written in C++:

std::stringstream ss;
ss << "hello world" << 2019;
System::String message(ss.str().c_str());
Debug::Log(message);