Пример #1
0
        private void LoadObject(ObjectData data)
        {
            GameObject gobject;
            string     absoluteMeshPath;
            Transform  importedParent = null;

            // Check for import
            if (data.isImported)
            {
                try
                {
                    importedParent   = new GameObject("__VRtist_tmp_load__").transform;
                    absoluteMeshPath = data.meshPath;
                    // Don't use async import since we may reference the game object for animations or constraints
                    // and the object must be loaded before we do so
                    GlobalState.GeometryImporter.ImportObject(absoluteMeshPath, importedParent, true);
                    if (importedParent.childCount == 0)
                    {
                        return;
                    }
                    gobject = importedParent.GetChild(0).gameObject;
                }
                catch (System.Exception e)
                {
                    Debug.LogError("Failed to load external object: " + e.Message);
                    return;
                }
            }
            else
            {
                absoluteMeshPath = GetSaveFolderPath(currentProjectName) + data.meshPath;
                gobject          = new GameObject(data.name);
            }

            LoadCommonData(gobject, data);
            gobject.name = data.name;

            // Mesh
            if (null != data.meshPath && data.meshPath.Length > 0)
            {
                if (!data.isImported && !data.isSkinMesh)
                {
                    if (!loadedMeshes.TryGetValue(absoluteMeshPath, out Mesh mesh))
                    {
                        MeshData meshData = new MeshData();
                        SerializationManager.Load(absoluteMeshPath, meshData);
                        mesh = meshData.CreateMesh();
                        loadedMeshes.Add(absoluteMeshPath, mesh);
                    }
                    gobject.AddComponent <MeshFilter>().sharedMesh  = mesh;
                    gobject.AddComponent <MeshRenderer>().materials = LoadMaterials(data);
                    gobject.AddComponent <MeshCollider>();
                }
                if (data.isSkinMesh)
                {
                    SkinMeshData skinData = new SkinMeshData();
                    SerializationManager.Load(absoluteMeshPath, skinData);
                    loadedSkinMeshes.Add(gobject, skinData);
                    skinMeshMaterials.Add(gobject, LoadMaterials(data));
                }

                if (!data.visible)
                {
                    foreach (Component component in gobject.GetComponents <Component>())
                    {
                        Type componentType = component.GetType();
                        var  prop          = componentType.GetProperty("enabled");
                        if (null != prop)
                        {
                            prop.SetValue(component, data.visible);
                        }
                    }
                }
            }

            SceneManager.AddObject(gobject);

            if (data.parentPath.Length > 0)
            {
                SceneManager.SetObjectParent(gobject, rootTransform.Find(data.parentPath).gameObject);
            }

            if (data.isImported)
            {
                if (!gobject.TryGetComponent <ParametersController>(out ParametersController controller))
                {
                    controller = gobject.AddComponent <ParametersController>();
                }
                controller.isImported = true;
                controller.importPath = data.meshPath;

                if (null != importedParent)
                {
                    Destroy(importedParent.gameObject);
                }
            }
        }