#pragma warning disable 649 using System.Collections.Generic; using TriLibCore.Extensions; using UnityEngine; using UnityEngine.UI; #if UNITY_EDITOR using UnityEditor; #endif namespace TriLibCore.Samples { /// /// Represents a sample that allows users to load a Model using the built-in File-Picker and displays the Model User Properties. /// public class UserPropertiesLoadingSample : MonoBehaviour { /// /// The Dictionaries containing the Model Properties sorted by type. /// private Dictionary _floatValues; private Dictionary _intValues; private Dictionary _vector2Values; private Dictionary _vector3Values; private Dictionary _vector4Values; private Dictionary _colorValues; private Dictionary _boolValues; private Dictionary _stringValues; /// /// The last loaded GameObject. /// private GameObject _loadedGameObject; /// /// The load Model Button. /// [SerializeField] private Button _loadModelButton; /// /// The progress indicator Text; /// [SerializeField] private Text _progressText; /// /// The properties listing Text; /// [SerializeField] private Text _propertiesText; #if UNITY_EDITOR /// /// The Model asset used to locate the filename when running in Unity Editor. /// [SerializeField] private Object ModelAsset; #endif /// /// Returns the path to the "TriLibSample.obj" Model. /// private string ModelPath { get { #if UNITY_EDITOR return AssetDatabase.GetAssetPath(ModelAsset); #else return "Models/TriLibSampleModel.obj"; #endif } } /// /// The callback passed to our custom UserPropertiesMapper, called for every Model and its every User Property. /// /// The GameObject containing the User Property. /// The User Property name. /// The User Property value. private void OnUserDataProcessed(GameObject gameObject, string propertyName, object propertyValue) { var propertyKey = $"{gameObject.name}.{propertyName}"; switch (propertyValue) { case float floatValue: if (!_floatValues.ContainsKey(propertyKey)) { _floatValues.Add(propertyKey, floatValue); } break; case int intValue: if (!_intValues.ContainsKey(propertyKey)) { _intValues.Add(propertyKey, intValue); } break; case Vector2 vector2Value: if (!_vector2Values.ContainsKey(propertyKey)) { _vector2Values.Add(propertyKey, vector2Value); } break; case Vector3 vector3Value: if (!_vector3Values.ContainsKey(propertyKey)) { _vector3Values.Add(propertyKey, vector3Value); } break; case Vector4 vector4Value: if (!_vector4Values.ContainsKey(propertyKey)) { _vector4Values.Add(propertyKey, vector4Value); } break; case Color colorValue: if (!_colorValues.ContainsKey(propertyKey)) { _colorValues.Add(propertyKey, colorValue); } break; case bool boolValue: if (!_boolValues.ContainsKey(propertyKey)) { _boolValues.Add(propertyKey, boolValue); } break; case string stringValue: if (!_stringValues.ContainsKey(propertyKey)) { _stringValues.Add(propertyKey, stringValue); } break; } } /// /// Creates the AssetLoaderOptions instance and displays the Model file-picker. /// It also creates our custom UserPropertiesMapper (SampleUserPropertiesMapper) instance and passes a callback function to it. /// /// /// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions". /// public void LoadModel() { var assetLoaderOptions = CreateAssetLoaderOptions(); var assetLoaderFilePicker = AssetLoaderFilePicker.Create(); assetLoaderFilePicker.LoadModelFromFilePickerAsync("Select a Model file", OnLoad, OnMaterialsLoad, OnProgress, OnBeginLoad, OnError, null, assetLoaderOptions); } /// /// Creates an AssetLoaderOptions with the sample UserPropertiesMapper. /// /// The created AssetLoaderOptions. private AssetLoaderOptions CreateAssetLoaderOptions() { var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(); var userPropertiesMapper = ScriptableObject.CreateInstance(); userPropertiesMapper.OnUserDataProcessed += OnUserDataProcessed; assetLoaderOptions.UserPropertiesMapper = userPropertiesMapper; return assetLoaderOptions; } /// /// Called when the the Model begins to load, configuring the scene. /// /// Indicates if any file has been selected. private void OnBeginLoad(bool filesSelected) { _floatValues = new Dictionary(); _intValues = new Dictionary(); _vector2Values = new Dictionary(); _vector3Values = new Dictionary(); _vector4Values = new Dictionary(); _colorValues = new Dictionary(); _boolValues = new Dictionary(); _stringValues = new Dictionary(); _loadModelButton.interactable = !filesSelected; _progressText.enabled = filesSelected; _progressText.text = string.Empty; } /// /// Called when any error occurs. /// /// The contextualized error, containing the original exception and the context passed to the method where the error was thrown. private void OnError(IContextualizedError obj) { Debug.LogError($"An error occurred while loading your Model: {obj.GetInnerException()}"); } /// /// Called when the Model loading progress changes. /// /// The context used to load the Model. /// The loading progress. private void OnProgress(AssetLoaderContext assetLoaderContext, float progress) { _progressText.text = $"Progress: {progress:P}"; } /// /// Called when the Model (including Textures and Materials) has been fully loaded. /// /// The loaded GameObject is available on the assetLoaderContext.RootGameObject field. /// The context used to load the Model. private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext) { if (assetLoaderContext.RootGameObject != null) { Debug.Log("Model fully loaded."); ListProperties(); } else { Debug.Log("Model could not be loaded."); } _loadModelButton.interactable = true; _progressText.enabled = false; } /// /// Updates the User Properties Text content with the loaded Model User Properties, categorizing the Properties by type. /// private void ListProperties() { var text = string.Empty; if (_stringValues.Count > 0) { text += "String\n"; foreach (var kvp in _stringValues) { text += $"{kvp.Key}=\"{kvp.Value}\"\n"; } } if (_floatValues.Count > 0) { text += "\nFloat\n"; foreach (var kvp in _floatValues) { text += $"{kvp.Key}={kvp.Value}\n"; } } if (_intValues.Count > 0) { text += "\nInteger\n"; foreach (var kvp in _intValues) { text += $"{kvp.Key}={kvp.Value}\n"; } } if (_boolValues.Count > 0) { text += "\nBoolean\n"; foreach (var kvp in _boolValues) { text += $"{kvp.Key}={kvp.Value}\n"; } } if (_vector2Values.Count > 0) { text += "\nVector2\n"; foreach (var kvp in _vector2Values) { text += $"{kvp.Key}={kvp.Value}\n"; } } if (_vector3Values.Count > 0) { text += "\nVector3\n"; foreach (var kvp in _vector3Values) { text += $"{kvp.Key}={kvp.Value}\n"; } } if (_vector4Values.Count > 0) { text += "\nVector4\n"; foreach (var kvp in _vector4Values) { text += $"{kvp.Key}={kvp.Value}\n"; } } if (_colorValues.Count > 0) { text += "\nColor\n"; foreach (var kvp in _colorValues) { text += ""; text += $"{kvp.Key}={kvp.Value}\n"; text += ""; } } _propertiesText.text = string.IsNullOrEmpty(text) ? "The model has no user properties" : $"Model User Properties\n\n{text}"; } /// /// Called when the Model Meshes and hierarchy are loaded. /// /// The loaded GameObject is available on the assetLoaderContext.RootGameObject field. /// The context used to load the Model. private void OnLoad(AssetLoaderContext assetLoaderContext) { if (_loadedGameObject != null) { Destroy(_loadedGameObject); } _loadedGameObject = assetLoaderContext.RootGameObject; if (_loadedGameObject != null) { Camera.main.FitToBounds(assetLoaderContext.RootGameObject, 4f); } } /// /// Loads the sample Model. /// private void Start() { var assetLoaderOptions = CreateAssetLoaderOptions(); OnBeginLoad(true); //Workaround to create lists AssetLoader.LoadModelFromFile(ModelPath, OnLoad, OnMaterialsLoad, OnProgress, OnError, null, assetLoaderOptions); } } }