public void Build(SpaceNode rootNode, DisposableList <HLODBuildInfo> infos, GameObject root,
                          float cullDistance, float lodDistance, bool writeNoPrefab, bool extractMaterial, Action <float> onProgress)
        {
            dynamic options = m_streamingOptions;
            string  path    = options.OutputDirectory;

            HLODTreeNodeContainer container         = new HLODTreeNodeContainer();
            HLODTreeNode          convertedRootNode = ConvertNode(container, rootNode);

            //create settings if there is no settings.
            if (AddressableAssetSettingsDefaultObject.Settings == null)
            {
                AddressableAssetSettings.Create(AddressableAssetSettingsDefaultObject.kDefaultConfigFolder, AddressableAssetSettingsDefaultObject.kDefaultConfigAssetName, true, true);
            }


            var settings = AddressableAssetSettingsDefaultObject.GetSettings(true);
            var group    = GetGroup(settings, options.AddressablesGroupName);

            m_shaderGuids.Clear();

            if (onProgress != null)
            {
                onProgress(0.0f);
            }

            HLODData.TextureCompressionData compressionData;
            compressionData.PCTextureFormat      = options.PCCompression;
            compressionData.WebGLTextureFormat   = options.WebGLCompression;
            compressionData.AndroidTextureFormat = options.AndroidCompression;
            compressionData.iOSTextureFormat     = options.iOSCompression;
            compressionData.tvOSTextureFormat    = options.tvOSCompression;


            string filenamePrefix = $"{path}{root.name}";

            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }

            Dictionary <int, HLODData> hlodDatas = new Dictionary <int, HLODData>();

            for (int i = 0; i < infos.Count; ++i)
            {
                if (hlodDatas.ContainsKey(infos[i].ParentIndex) == false)
                {
                    HLODData newData = new HLODData();
                    newData.CompressionData = compressionData;
                    hlodDatas.Add(infos[i].ParentIndex, newData);
                }

                HLODData data = hlodDatas[infos[i].ParentIndex];
                data.AddFromWokringObjects(infos[i].Name, infos[i].WorkingObjects);
                data.AddFromWorkingColliders(infos[i].Name, infos[i].Colliders);

                if (writeNoPrefab)
                {
                    if (hlodDatas.ContainsKey(i) == false)
                    {
                        HLODData newData = new HLODData();
                        newData.CompressionData = compressionData;
                        hlodDatas.Add(i, newData);
                    }

                    HLODData prefabData = hlodDatas[i];
                    var      spaceNode  = infos[i].Target;

                    for (int oi = 0; oi < spaceNode.Objects.Count; ++oi)
                    {
                        if (PrefabUtility.IsAnyPrefabInstanceRoot(spaceNode.Objects[oi]) == false)
                        {
                            prefabData.AddFromGameObject(spaceNode.Objects[oi]);
                        }
                    }
                }


                if (onProgress != null)
                {
                    onProgress((float)i / (float)infos.Count);
                }
            }


            if (extractMaterial)
            {
                ExtractMaterial(hlodDatas, filenamePrefix);
            }

            Dictionary <int, RootData> rootDatas = new Dictionary <int, RootData>();

            foreach (var item in hlodDatas)
            {
                string filename = $"{filenamePrefix}_group{item.Key}.hlod";
                using (Stream stream = new FileStream(filename, FileMode.Create))
                {
                    HLODDataSerializer.Write(stream, item.Value);
                    stream.Close();
                }

                AssetDatabase.ImportAsset(filename, ImportAssetOptions.ForceUpdate);
                RootData rootData = AssetDatabase.LoadAssetAtPath <RootData>(filename);
                m_manager.AddGeneratedResource(rootData);
                AddAddress(settings, group, rootData);

                rootDatas.Add(item.Key, rootData);
            }



            var addressableController = root.AddComponent <AddressableHLODController>();

            for (int i = 0; i < infos.Count; ++i)
            {
                var spaceNode    = infos[i].Target;
                var hlodTreeNode = convertedTable[infos[i].Target];

                for (int oi = 0; oi < spaceNode.Objects.Count; ++oi)
                {
                    int        highId = -1;
                    GameObject obj    = spaceNode.Objects[oi];

                    if (PrefabUtility.IsPartOfAnyPrefab(obj) == false)
                    {
                        GameObject rootGameObject = null;

                        if (rootDatas.ContainsKey(i))
                        {
                            rootGameObject = rootDatas[i].GetRootObject(obj.name);
                        }

                        if (rootGameObject != null)
                        {
                            GameObject go = PrefabUtility.InstantiatePrefab(rootGameObject) as GameObject;
                            go.transform.SetParent(obj.transform.parent);
                            go.transform.localPosition = obj.transform.localPosition;
                            go.transform.localRotation = obj.transform.localRotation;
                            go.transform.localScale    = obj.transform.localScale;

                            if (m_manager.IsGeneratedResource(obj))
                            {
                                m_manager.AddGeneratedResource(go);
                            }
                            else
                            {
                                m_manager.AddConvertedPrefabResource(go);
                            }

                            spaceNode.Objects.Add(go);

                            Object.DestroyImmediate(obj);
                            continue;
                        }
                    }

                    var address = GetAddress(spaceNode.Objects[oi]);
                    if (string.IsNullOrEmpty(address) && PrefabUtility.IsAnyPrefabInstanceRoot(spaceNode.Objects[oi]))
                    {
                        AddAddress(settings, group, spaceNode.Objects[oi]);
                        address = GetAddress(spaceNode.Objects[oi]);
                    }

                    if (address != null)
                    {
                        highId = addressableController.AddHighObject(address, spaceNode.Objects[oi]);
                    }
                    else
                    {
                        highId = addressableController.AddHighObject(spaceNode.Objects[oi]);
                    }

                    hlodTreeNode.HighObjectIds.Add(highId);
                }

                {
                    if (rootDatas[infos[i].ParentIndex].GetRootObject(infos[i].Name) != null)
                    {
                        string filename = $"{filenamePrefix}_group{infos[i].ParentIndex}.hlod";
                        int    lowId    = addressableController.AddLowObject(filename + "[" + infos[i].Name + "]");
                        hlodTreeNode.LowObjectIds.Add(lowId);
                    }
                }
            }

            var shaderEntriesAdded = new List <AddressableAssetEntry>();

            foreach (var shaderGuid in m_shaderGuids)
            {
                if (IsExistsInAddressables(shaderGuid) == false)
                {
                    shaderEntriesAdded.Add(settings.CreateOrMoveEntry(shaderGuid, group, false, false));
                }
            }
            settings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, shaderEntriesAdded, true);
            m_shaderGuids.Clear();

            addressableController.Container    = container;
            addressableController.Root         = convertedRootNode;
            addressableController.CullDistance = cullDistance;
            addressableController.LODDistance  = lodDistance;
        }
Beispiel #2
0
        public void Build(SpaceNode rootNode, DisposableList <HLODBuildInfo> infos, GameObject root,
                          float cullDistance, float lodDistance, bool writeNoPrefab, bool extractMaterial, Action <float> onProgress)
        {
            dynamic options = m_streamingOptions;
            string  path    = options.OutputDirectory;

            HLODTreeNodeContainer container         = new HLODTreeNodeContainer();
            HLODTreeNode          convertedRootNode = ConvertNode(container, rootNode);

            if (onProgress != null)
            {
                onProgress(0.0f);
            }

            HLODData.TextureCompressionData compressionData;
            compressionData.PCTextureFormat      = options.PCCompression;
            compressionData.WebGLTextureFormat   = options.WebGLCompression;
            compressionData.AndroidTextureFormat = options.AndroidCompression;
            compressionData.iOSTextureFormat     = options.iOSCompression;
            compressionData.tvOSTextureFormat    = options.tvOSCompression;

            string filename = $"{path}{root.name}.hlod";

            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }

            using (Stream stream = new FileStream(filename, FileMode.Create))
            {
                HLODData data = new HLODData();
                data.CompressionData = compressionData;

                for (int i = 0; i < infos.Count; ++i)
                {
                    data.AddFromWokringObjects(infos[i].Name, infos[i].WorkingObjects);
                    data.AddFromWorkingColliders(infos[i].Name, infos[i].Colliders);
                    if (onProgress != null)
                    {
                        onProgress((float)i / (float)infos.Count);
                    }
                }

                if (writeNoPrefab)
                {
                    for (int ii = 0; ii < infos.Count; ++ii)
                    {
                        var spaceNode = infos[ii].Target;

                        for (int oi = 0; oi < spaceNode.Objects.Count; ++oi)
                        {
                            if (PrefabUtility.IsAnyPrefabInstanceRoot(spaceNode.Objects[oi]) == false)
                            {
                                data.AddFromGameObject(spaceNode.Objects[oi]);
                            }
                        }
                    }
                }

                if (extractMaterial == true)
                {
                    ExtractMaterial(data, $"{path}{root.name}");
                }

                HLODDataSerializer.Write(stream, data);
            }

            AssetDatabase.ImportAsset(filename, ImportAssetOptions.ForceUpdate);
            RootData rootData = AssetDatabase.LoadAssetAtPath <RootData>(filename);

            m_manager.AddGeneratedResource(rootData);

            var        defaultController = root.AddComponent <DefaultHLODController>();
            GameObject hlodRoot          = new GameObject("HLODRoot");

            hlodRoot.transform.SetParent(root.transform, false);
            m_manager.AddGeneratedResource(hlodRoot);

            for (int ii = 0; ii < infos.Count; ++ii)
            {
                var spaceNode    = infos[ii].Target;
                var hlodTreeNode = convertedTable[infos[ii].Target];

                for (int oi = 0; oi < spaceNode.Objects.Count; ++oi)
                {
                    GameObject obj            = spaceNode.Objects[oi];
                    GameObject rootGameObject = rootData.GetRootObject(obj.name);
                    if (rootGameObject != null)
                    {
                        GameObject go = PrefabUtility.InstantiatePrefab(rootGameObject) as GameObject;
                        go.transform.SetParent(obj.transform.parent);
                        go.transform.localPosition = obj.transform.localPosition;
                        go.transform.localRotation = obj.transform.localRotation;
                        go.transform.localScale    = obj.transform.localScale;

                        int highId = defaultController.AddHighObject(go);
                        hlodTreeNode.HighObjectIds.Add(highId);

                        if (m_manager.IsGeneratedResource(obj))
                        {
                            m_manager.AddGeneratedResource(go);
                        }
                        else
                        {
                            m_manager.AddConvertedPrefabResource(go);
                        }

                        Object.DestroyImmediate(obj);
                    }
                    else
                    {
                        int highId = defaultController.AddHighObject(obj);
                        hlodTreeNode.HighObjectIds.Add(highId);
                    }
                }


                if (infos[ii].WorkingObjects.Count > 0)
                {
                    GameObject prefab = rootData.GetRootObject(infos[ii].Name);
                    if (prefab == null)
                    {
                        Debug.LogError("Prefab not found: " + infos[ii].Name);
                    }
                    else
                    {
                        GameObject go = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
                        go.transform.SetParent(hlodRoot.transform, false);
                        go.SetActive(false);
                        int lowId = defaultController.AddLowObject(go);
                        hlodTreeNode.LowObjectIds.Add(lowId);
                        m_manager.AddGeneratedResource(go);
                    }
                }
            }

            defaultController.Container    = container;
            defaultController.Root         = convertedRootNode;
            defaultController.CullDistance = cullDistance;
            defaultController.LODDistance  = lodDistance;
        }