- Unity 2018+
- .Net 4.x enabled in project
- Elements running at an accessible URL
- To generate custom application code: Must also have an application created within Elements with the code already uploaded. See the docs here for more info on how to prepare your Element code for client code generation.
Elements Codegen is a tool that will convert your Elements and application APIs and model definitions and into C# code that is immediately usable.
In addition, there are some convenience classes generated so that you can hit the ground running. These are optional to use, however, so feel free to ignore them if you want to manage everything yourself.
Go to Window -> Elements -> Elements Codegen to get started.
You'll need to enter the credentials of an admin/SUPERUSER. If you haven't changed the defaults yet, you should be able to just use root/example as the username/password. See here for more information on accessing the CMS and how to change the default SUPERUSER.
Important
You must have Elements running at the target root URL. If running locally, then by default this will be http://localhost:8080
Warning
The tool might not be available if it is imported with active compiler errors. If this is the case, please resolve the errors and check again for the tool window.
After generating your code, you can use {packageName}.Client.ElementsClient to initialize the API with the server URL root, and then make any API call. Most properties can be overridden if you prefer to write your own implementation, including object (de)serialization, credentials storage, etc. You can also use the APIs directly if you prefer to manage the requests yourself, or if you prefer a DI based architecture.
When you initialize, you will need to specify the root API path. Locally, this will be http://localhost:8080/api/rest. Optionally, you can tell ElementsClient to not cache the session if you prefer to do that yourself.
By default, the generated code will use JSON, and store the session in Application.PersistentDataPath. Encryption is not enabled by default.
Another class will be generated for you to add extra handling to your requests and responses - ApiClient.partial.cs. This contains two methods - InterceptRequest and InterceptResponse.
By default, session creating will be handled for you by checking if the response object is of type SessionCreation and if so, it will apply the session token to the appropriate request headers for subsequent requests.
See ElementsCodegen/Tests/ElementsTest.cs for an example on how to log in and get the current user (this might be commented out to avoid compiler errors before you generate the Elements API code).
Enjoy!
See https://namazustudios.com/docs/ for more information.