Esempio n. 1
0
        /// <summary>
        /// Instantiates a NIF file. Must be called from the main thread.
        /// </summary>
        public GameObject InstantiateNIF(string filePath)
        {
            // Create a container for the NIF prefabs if it doesn't yet exist.
            if (prefabContainerObj == null)
            {
                prefabContainerObj = new GameObject("NIF Prefabs");
                prefabContainerObj.SetActive(false);
            }

            // Find an existing prefab for the NIF file, or create a new prefab object.
            GameObject prefab;

            lock (dictionariesLock)
            {
                NIFPrefabs.TryGetValue(filePath, out prefab);
            }

            if (prefab == null)
            {
                // Load the NIF file and instantiate it.
                var file = LoadNIFAndRemoveFromCache(filePath);

                var objBuilder = new NIFObjectBuilder(file, dataReader, materialManager);
                prefab = objBuilder.BuildObject();

                prefab.transform.parent = prefabContainerObj.transform;

                // Add LOD support to the prefab.
                var LODComponent = prefab.AddComponent <LODGroup>();

                var LODs = new LOD[1]
                {
                    new LOD(0.015f, prefab.GetComponentsInChildren <Renderer>())
                };

                LODComponent.SetLODs(LODs);

                // Cache the prefab.
                lock (dictionariesLock)
                {
                    NIFPrefabs[filePath] = prefab;
                }
            }

            return(GameObject.Instantiate(prefab));
        }
Esempio n. 2
0
        private GameObject LoadNifPrefabDontAddToPrefabCache(string filePath)
        {
            Debug.Assert(!nifPrefabs.ContainsKey(filePath));

            PreloadNifFileAsync(filePath);
            var file = nifFilePreloadTasks[filePath].Result;

            nifFilePreloadTasks.Remove(filePath);

            // Start pre-loading all the NIF's textures.
            foreach (var anNiObject in file.blocks)
            {
                if (anNiObject is NIF.NiSourceTexture)
                {
                    var anNiSourceTexture = (NIF.NiSourceTexture)anNiObject;

                    if ((anNiSourceTexture.fileName != null) && (anNiSourceTexture.fileName != ""))
                    {
                        materialManager.TextureManager.PreloadTextureFileAsync(anNiSourceTexture.fileName);
                    }
                }
            }

            var objBuilder = new NIFObjectBuilder(file, materialManager);
            var prefab     = objBuilder.BuildObject();

            prefab.transform.parent = prefabContainerObj.transform;

            // Add LOD support to the prefab.
            var LODComponent = prefab.AddComponent <LODGroup>();
            var LODs         = new LOD[1]
            {
                new LOD(0.015f, prefab.GetComponentsInChildren <Renderer>())
            };

            LODComponent.SetLODs(LODs);

            return(prefab);
        }