Importing Models from Code
A model can be imported thanks to the Importer class.
An Importer takes the file to import and ImportSettings as parameters.
How to use
Create a new Importer and pass the file to import and parameters to use.
Then, to start the import process, call importer.run()
NOTE To create ImportSettings at runtime, use Unity's ScriptableObject.CreateInstance<>() (new ImportSettings() won't work)
WARNING An Importer can only run once. To import multiple files, create an Importer for each file.
Example
public PiXYZ.Plugin.Unity.ImportSettings importSettings;
public string filePath;
void import() {
var importer = new PiXYZ.Plugin.Unity.Importer(filePath, importSettings);
importer.progressed += onProgressChanged;
importer.completed += onImportEnded;
importer.run();
}
void onProgressChanged(float progress, string message) {
Debug.Log("Progress : " + 100f * progress + "%");
}
void onImportEnded(GameObject gameObject) {
Debug.Log("Model Imported");
}
WARNING When an Importer is running, it is not possible anymore to change its parameters.
Asynchronous / Synchronous
The Importer class can run Synchronously or Asynchronously.
By default, it will run Asynchronously, meaning that the import process will freeze as little as possible the main thread (so that you can maintain 60 fps while importing small to medium sized models). In this case, a callback function has to be connected to importer.progressed so that you can get notified when the model has been successfully imported.
If isAsyncronous is false, then the main thread will be frozen until the file has been imported. In this case, a callback function is not required.
Example 1 : Asynchronous Import
void import() {
var importer = new PiXYZ.Plugin.Unity.Importer(filePath, importSettings);
importer.isAsynchronous = true;
importer.completed += onImportEnded;
importer.run();
Debug.Log("A");
}
void onImportEnded(GameObject gameObject) {
Debug.Log("B");
}
>>> A
>>> B
Example 2: Synchronous Import
void import() {
var importer = new PiXYZ.Plugin.Unity.Importer(filePath, importSettings);
importer.isAsynchronous = false;
importer.completed += onImportEnded;
importer.run();
Debug.Log("A");
}
void onImportEnded(GameObject gameObject) {
Debug.Log("B");
}
>>> B
>>> A