Skip to content

Commit f774291

Browse files
Move C++ source files from the Unity project's Assets directory to a new CppSource directory in the Unity project.
1 parent 20e51eb commit f774291

13 files changed

Lines changed: 40 additions & 112 deletions

File tree

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ One of the project's goals is to make it just as easy to work with C++ as it is
8282

8383
1. Download or clone this repo
8484
2. Copy everything in `Unity/Assets` directory to your Unity project's `Assets` directory
85-
3. Edit `NativeScriptTypes.json` and specify what parts of the Unity API you want access to from C++. Some examples are provided, but feel free to delete them if you're not using those features.
86-
4. Edit `CppSource/Game.cpp` to create your game. Some example code is provided, but feel free to delete it. You can add more C++ source (`.cpp`) and header (`.h`) files here as your game grows.
85+
3. Copy the `Unity/CppSource` directory to your Unity project directory
86+
4. Edit `NativeScriptTypes.json` and specify what parts of the Unity API you want access to from C++. Some examples are provided, but feel free to delete them if you're not using those features.
87+
5. Edit `Unity/CppSource/Game/Game.cpp` to create your game. Some example code is provided, but feel free to delete it. You can add more C++ source (`.cpp`) and header (`.h`) files here as your game grows.
8788

8889
# Building the C++ Plugin
8990

@@ -93,7 +94,7 @@ One of the project's goals is to make it just as easy to work with C++ as it is
9394
2. Create a directory for build files. Anywhere is fine.
9495
3. Open the Terminal app in `/Applications/Utilities`
9596
4. Execute `cd /path/to/your/build/directory`
96-
5. Execute `cmake -G MyGenerator -DCMAKE_TOOLCHAIN_FILE=/path/to/your/project/Assets/iOS.cmake /path/to/your/project/Assets`. Replace `MyGenerator` with the generator of your choice. To see the options, execute `cmake --help` and look at the list at the bottom. Common choices include "Unix Makefiles" to build from command line or "Xcode" to use Apple's IDE.
97+
5. Execute `cmake -G MyGenerator -DCMAKE_TOOLCHAIN_FILE=/path/to/your/project/CppSource/iOS.cmake /path/to/your/project/CppSource`. Replace `MyGenerator` with the generator of your choice. To see the options, execute `cmake --help` and look at the list at the bottom. Common choices include "Unix Makefiles" to build from command line or "Xcode" to use Apple's IDE.
9798
6. The build scripts or IDE project files are now generated in your build directory
9899
7. Build as appropriate for your generator. For example, execute `make` if you chose `Unix Makefiles` as your generator or open `NativeScript.xcodeproj` and click `Product > Build` if you chose Xcode.
99100

@@ -103,7 +104,7 @@ One of the project's goals is to make it just as easy to work with C++ as it is
103104
2. Create a directory for build files. Anywhere is fine.
104105
3. Open the Terminal app in `/Applications/Utilities`
105106
4. Execute `cd /path/to/your/build/directory`
106-
5. Execute `cmake -G "MyGenerator" -DEDITOR=TRUE /path/to/your/project/Assets`. Replace `MyGenerator` with the generator of your choice. To see the options, execute `cmake --help` and look at the list at the bottom. Common choices include "Unix Makefiles" to build from command line or "Xcode" to use Apple's IDE. Remove `-DEDITOR=TRUE` for standalone builds.
107+
5. Execute `cmake -G "MyGenerator" -DEDITOR=TRUE /path/to/your/project/CppSource`. Replace `MyGenerator` with the generator of your choice. To see the options, execute `cmake --help` and look at the list at the bottom. Common choices include "Unix Makefiles" to build from command line or "Xcode" to use Apple's IDE. Remove `-DEDITOR=TRUE` for standalone builds.
107108
6. The build scripts or IDE project files are now generated in your build directory
108109
7. Build as appropriate for your generator. For example, execute `make` if you chose `Unix Makefiles` as your generator or open `NativeScript.xcodeproj` and click `Product > Build` if you chose Xcode.
109110

@@ -113,7 +114,7 @@ One of the project's goals is to make it just as easy to work with C++ as it is
113114
2. Create a directory for build files. Anywhere is fine.
114115
3. Open a Command Prompt by clicking the Start button, typing "Command Prompt", then clicking the app
115116
4. Execute `cd /path/to/your/build/directory`
116-
5. Execute `cmake -G "Visual Studio VERSION YEAR Win64" -DEDITOR=TRUE /path/to/your/project/Assets`. Replace `VERSION` and `YEAR` with the version of Visual Studio you want to use. To see the options, execute `cmake --help` and look at the list at the bottom. For example, use `"`Visual Studio 15 2017 Win64` for Visual Studio 2017. Any version, including Community, works just fine. Remove `-DEDITOR=TRUE` for standalone builds.
117+
5. Execute `cmake -G "Visual Studio VERSION YEAR Win64" -DEDITOR=TRUE /path/to/your/project/CppSource`. Replace `VERSION` and `YEAR` with the version of Visual Studio you want to use. To see the options, execute `cmake --help` and look at the list at the bottom. For example, use `"`Visual Studio 15 2017 Win64` for Visual Studio 2017. Any version, including Community, works just fine. Remove `-DEDITOR=TRUE` for standalone builds.
117118
6. The project files are now generated in your build directory
118119
7. Open `NativeScript.sln` and click `Build > Build Solution`.
119120

@@ -123,7 +124,7 @@ One of the project's goals is to make it just as easy to work with C++ as it is
123124
2. Create a directory for build files. Anywhere is fine.
124125
3. Open a terminal as appropriate for your Linux distribution
125126
4. Execute `cd /path/to/your/build/directory`
126-
5. Execute `cmake -G "MyGenerator" -DEDITOR=TRUE /path/to/your/project/Assets`. Replace `MyGenerator` with the generator of your choice. To see the options, execute `cmake --help` and look at the list at the bottom. The most common choice is "Unix Makefiles" to build from command line, but there are IDE options too. Remove `-DEDITOR=TRUE` for standalone builds.
127+
5. Execute `cmake -G "MyGenerator" -DEDITOR=TRUE /path/to/your/project/CppSource`. Replace `MyGenerator` with the generator of your choice. To see the options, execute `cmake --help` and look at the list at the bottom. The most common choice is "Unix Makefiles" to build from command line, but there are IDE options too. Remove `-DEDITOR=TRUE` for standalone builds.
127128
6. The build scripts or IDE project files are now generated in your build directory
128129
7. Build as appropriate for your generator. For example, execute `make` if you chose `Unix Makefiles` as your generator.
129130

@@ -133,13 +134,15 @@ One of the project's goals is to make it just as easy to work with C++ as it is
133134
2. Create a directory for build files. Anywhere is fine.
134135
3. Open a terminal (macOS, Linux) or Command Prompt (Windows)
135136
4. Execute `cd /path/to/your/build/directory`
136-
5. Execute `cmake -G MyGenerator -DANDROID_NDK=/path/to/android/ndk /path/to/your/project/Assets`. Replace `MyGenerator` with the generator of your choice. To see the options, execute `cmake --help` and look at the list at the bottom. To make a build for any platform other than Android, omit the `-DANDROID_NDK=/path/to/android/ndk` part.
137+
5. Execute `cmake -G MyGenerator -DANDROID_NDK=/path/to/android/ndk /path/to/your/project/CppSource`. Replace `MyGenerator` with the generator of your choice. To see the options, execute `cmake --help` and look at the list at the bottom. To make a build for any platform other than Android, omit the `-DANDROID_NDK=/path/to/android/ndk` part.
137138
6. The build scripts or IDE project files are now generated in your build directory
138139
7. Build as appropriate for your generator. For example, execute `make` if you chose `Unix Makefiles` as your generator.
139140

140-
# Configuring the Code Generator
141+
# The Code Generator
141142

142-
Open `NativeScriptTypes.json` and notice the existing examples. Add on to this file to expose more C# APIs from Unity, .NET, or custom DLLs to your C++ code.
143+
To run the code generator, choose `NativeScript > Generate Bindings` from the Unity editor.
144+
145+
To configure the code generator, open `NativeScriptTypes.json` and notice the existing examples. Add on to this file to expose more C# APIs from Unity, .NET, or custom DLLs to your C++ code.
143146

144147
The code generator supports:
145148

@@ -182,7 +185,7 @@ The JSON file is laid out as follows:
182185

183186
# Updating To A New Version
184187

185-
To update to a new version of this project, overwrite your Unity project's `Assets/NativeScript` directory with this project's `Unity/Assets/NativeScript` directory.
188+
To update to a new version of this project, overwrite your Unity project's `Assets/NativeScript` directory with this project's `Unity/Assets/NativeScript` directory and re-run the code generator.
186189

187190
# Reference
188191

Unity/Assets/CMakeLists.txt.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

Unity/Assets/CppSource.meta

Lines changed: 0 additions & 9 deletions
This file was deleted.

Unity/Assets/NativeScript/Bindings.cpp.meta

Lines changed: 0 additions & 27 deletions
This file was deleted.

Unity/Assets/NativeScript/Bindings.h.meta

Lines changed: 0 additions & 27 deletions
This file was deleted.

Unity/Assets/NativeScript/Editor/GenerateBindings.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,25 @@ public MessageInfo(
207207
const string PostCompileWorkPref = "NativeScriptGenerateBindingsPostCompileWork";
208208
const string DryRunPref = "NativeScriptGenerateBindingsDryRun";
209209

210+
static readonly string CppDirPath =
211+
Path.Combine(
212+
Path.Combine(
213+
new DirectoryInfo(Application.dataPath)
214+
.Parent
215+
.FullName,
216+
"CppSource"),
217+
"NativeScript");
210218
static readonly string CsharpPath = Path.Combine(
211219
Application.dataPath,
212220
Path.Combine(
213221
"NativeScript",
214222
"Bindings.cs"));
215223
static readonly string CppHeaderPath = Path.Combine(
216-
Application.dataPath,
217-
Path.Combine(
218-
"NativeScript",
219-
"Bindings.h"));
224+
CppDirPath,
225+
"Bindings.h");
220226
static readonly string CppSourcePath = Path.Combine(
221-
Application.dataPath,
222-
Path.Combine(
223-
"NativeScript",
224-
"Bindings.cpp"));
227+
CppDirPath,
228+
"Bindings.cpp");
225229

226230
// Restore unused field types
227231
#pragma warning restore CS0649

Unity/Assets/iOS.cmake.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,33 @@ endif()
3636

3737
# Set output path
3838
if (ANDROID_NDK)
39-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Plugins/Android)
40-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/Plugins/Android)
41-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/Plugins/Android)
39+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../Assets/Plugins/Android)
40+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/../Assets/Plugins/Android)
41+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/../Assets/Plugins/Android)
4242
elseif (IOS)
43-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Plugins/iOS)
44-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/Plugins/iOS)
45-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/Plugins/iOS)
43+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../Assets/Plugins/iOS)
44+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/../Assets/Plugins/iOS)
45+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/../Assets/Plugins/iOS)
4646
elseif (WIN32 OR (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") OR (${CMAKE_SYSTEM_NAME} MATCHES "Linux"))
4747
if (EDITOR)
48-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Plugins/Editor)
49-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/Plugins/Editor)
50-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/Plugins/Editor)
48+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../Assets/Plugins/Editor)
49+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/../Assets/Plugins/Editor)
50+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/../Assets/Plugins/Editor)
5151
else()
52-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Plugins)
53-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/Plugins)
54-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/Plugins)
52+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../Assets/Plugins)
53+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/../Assets/Plugins)
54+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/../Assets/Plugins)
5555
endif()
5656
endif()
5757

5858
# Set the directories for NativeScript and game C++ as include directories
5959
include_directories(
6060
${CMAKE_SOURCE_DIR}/NativeScript
61-
${CMAKE_SOURCE_DIR}/CppSource)
61+
${CMAKE_SOURCE_DIR}/Game)
6262

6363
# Set all .cpp files for NativeScript and the game as sources
64-
file(GLOB GAMESOURCEFILES ${CMAKE_SOURCE_DIR}/CppSource/*.cpp)
65-
file(GLOB GAMEHEADERFILES ${CMAKE_SOURCE_DIR}/CppSource/*.h)
64+
file(GLOB GAMESOURCEFILES ${CMAKE_SOURCE_DIR}/Game/*.cpp)
65+
file(GLOB GAMEHEADERFILES ${CMAKE_SOURCE_DIR}/Game/*.h)
6666
file(GLOB NATIVESCRIPTSOURCEFILES ${CMAKE_SOURCE_DIR}/NativeScript/*.cpp)
6767
file(GLOB NATIVESCRIPTHEADERFILES ${CMAKE_SOURCE_DIR}/NativeScript/*.h)
6868
set(

0 commit comments

Comments
 (0)