コード例 #1
0
        /// <summary>
        /// 加载资源(异步)
        /// </summary>
        /// <typeparam name="T">资源类型</typeparam>
        /// <param name="info">资源信息标记</param>
        /// <param name="loadingAction">加载中事件</param>
        /// <param name="loadDoneAction">加载完成事件</param>
        /// <param name="isPrefab">是否是加载预制体</param>
        /// <param name="parent">预制体加载完成后的父级</param>
        /// <param name="isUI">是否是加载UI</param>
        /// <returns>加载协程迭代器</returns>
        public IEnumerator LoadAssetAsync <T>(ResourceInfoBase info, HTFAction <float> loadingAction, HTFAction <T> loadDoneAction, bool isPrefab, Transform parent, bool isUI) where T : UnityEngine.Object
        {
            DateTime beginTime = DateTime.Now;

            if (_isLoading)
            {
                yield return(_loadWait);
            }

            _isLoading = true;

            yield return(Main.Current.StartCoroutine(LoadDependenciesAssetBundleAsync(info.AssetBundleName)));

            DateTime waitTime = DateTime.Now;

            UnityEngine.Object asset = null;

            if (LoadMode == ResourceLoadMode.Resource)
            {
                ResourceRequest request = Resources.LoadAsync <T>(info.ResourcePath);
                while (!request.isDone)
                {
                    loadingAction?.Invoke(request.progress);
                    yield return(null);
                }
                asset = request.asset;
                if (asset)
                {
                    if (isPrefab)
                    {
                        asset = ClonePrefab(asset as GameObject, parent, isUI);
                    }
                }
                else
                {
                    throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:Resources文件夹中不存在资源 " + info.ResourcePath + "!");
                }
            }
            else
            {
#if UNITY_EDITOR
                if (IsEditorMode)
                {
                    loadingAction?.Invoke(1);
                    yield return(null);

                    asset = AssetDatabase.LoadAssetAtPath <T>(info.AssetPath);
                    if (asset)
                    {
                        if (isPrefab)
                        {
                            asset = ClonePrefab(asset as GameObject, parent, isUI);
                        }
                    }
                    else
                    {
                        throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:路径中不存在资源 " + info.AssetPath + "!");
                    }
                }
                else
                {
                    if (AssetBundles.ContainsKey(info.AssetBundleName))
                    {
                        loadingAction?.Invoke(1);
                        yield return(null);

                        asset = AssetBundles[info.AssetBundleName].LoadAsset <T>(info.AssetPath);
                        if (asset)
                        {
                            if (isPrefab)
                            {
                                asset = ClonePrefab(asset as GameObject, parent, isUI);
                            }
                        }
                        else
                        {
                            throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath + " !");
                        }
                    }
                    else
                    {
                        using (UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(AssetBundleRootPath + info.AssetBundleName, GetAssetBundleHash(info.AssetBundleName)))
                        {
                            request.SendWebRequest();
                            while (!request.isDone)
                            {
                                loadingAction?.Invoke(request.downloadProgress);
                                yield return(null);
                            }
                            if (!request.isNetworkError && !request.isHttpError)
                            {
                                AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
                                if (bundle)
                                {
                                    asset = bundle.LoadAsset <T>(info.AssetPath);
                                    if (asset)
                                    {
                                        if (isPrefab)
                                        {
                                            asset = ClonePrefab(asset as GameObject, parent, isUI);
                                        }
                                    }
                                    else
                                    {
                                        throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath + " !");
                                    }

                                    if (IsCacheAssetBundle)
                                    {
                                        if (!AssetBundles.ContainsKey(info.AssetBundleName))
                                        {
                                            AssetBundles.Add(info.AssetBundleName, bundle);
                                        }
                                    }
                                    else
                                    {
                                        bundle.Unload(false);
                                    }
                                }
                                else
                                {
                                    throw new HTFrameworkException(HTFrameworkModule.Resource, "请求:" + request.url + " 未下载到AB包!");
                                }
                            }
                            else
                            {
                                throw new HTFrameworkException(HTFrameworkModule.Resource, "请求:" + request.url + " 遇到网络错误:" + request.error + "!");
                            }
                        }
                    }
                }
#else
                if (AssetBundles.ContainsKey(info.AssetBundleName))
                {
                    loadingAction?.Invoke(1);
                    yield return(null);

                    asset = AssetBundles[info.AssetBundleName].LoadAsset <T>(info.AssetPath);
                    if (asset)
                    {
                        if (isPrefab)
                        {
                            asset = ClonePrefab(asset as GameObject, parent, isUI);
                        }
                    }
                    else
                    {
                        throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath + " !");
                    }
                }
                else
                {
                    using (UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(AssetBundleRootPath + info.AssetBundleName, GetAssetBundleHash(info.AssetBundleName)))
                    {
                        request.SendWebRequest();
                        while (!request.isDone)
                        {
                            loadingAction?.Invoke(request.downloadProgress);
                            yield return(null);
                        }
                        if (!request.isNetworkError && !request.isHttpError)
                        {
                            AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
                            if (bundle)
                            {
                                asset = bundle.LoadAsset <T>(info.AssetPath);
                                if (asset)
                                {
                                    if (isPrefab)
                                    {
                                        asset = ClonePrefab(asset as GameObject, parent, isUI);
                                    }
                                }
                                else
                                {
                                    throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath + " !");
                                }

                                if (IsCacheAssetBundle)
                                {
                                    if (!AssetBundles.ContainsKey(info.AssetBundleName))
                                    {
                                        AssetBundles.Add(info.AssetBundleName, bundle);
                                    }
                                }
                                else
                                {
                                    bundle.Unload(false);
                                }
                            }
                            else
                            {
                                throw new HTFrameworkException(HTFrameworkModule.Resource, "请求:" + request.url + " 未下载到AB包!");
                            }
                        }
                        else
                        {
                            throw new HTFrameworkException(HTFrameworkModule.Resource, "请求:" + request.url + " 遇到网络错误:" + request.error + "!");
                        }
                    }
                }
#endif
            }

            DateTime endTime = DateTime.Now;

            Log.Info(string.Format("异步加载资源{0}[{1}模式]:\r\n{2}\r\n等待耗时:{3}秒  加载耗时:{4}秒"
                                   , asset ? "成功" : "失败"
                                   , LoadMode.ToString()
                                   , LoadMode == ResourceLoadMode.Resource ? info.GetResourceFullPath() : info.GetAssetBundleFullPath(AssetBundleRootPath)
                                   , (waitTime - beginTime).TotalSeconds
                                   , (endTime - waitTime).TotalSeconds));

            if (asset)
            {
                DataSetInfo dataSet = info as DataSetInfo;
                if (dataSet != null && dataSet.Data != null)
                {
                    asset.Cast <DataSetBase>().Fill(dataSet.Data);
                }

                loadDoneAction?.Invoke(asset as T);
            }
            asset = null;

            _isLoading = false;
        }
コード例 #2
0
        /// <summary>
        /// 加载资源(异步)
        /// </summary>
        /// <typeparam name="T">资源类型</typeparam>
        /// <param name="info">资源信息标记</param>
        /// <param name="loadingAction">加载中事件</param>
        /// <param name="loadDoneAction">加载完成事件</param>
        /// <param name="isPrefab">是否是加载预制体</param>
        /// <param name="parent">预制体加载完成后的父级</param>
        /// <param name="isUI">是否是加载UI</param>
        /// <returns>加载协程迭代器</returns>
        public IEnumerator LoadAssetAsync <T>(ResourceInfoBase info, HTFAction <float> loadingAction, HTFAction <T> loadDoneAction, bool isPrefab, Transform parent, bool isUI) where T : UnityEngine.Object
        {
            float beginTime = Time.realtimeSinceStartup;

            //单线加载,如果其他地方在加载资源,则等待
            if (_isLoading)
            {
                yield return(_loadWait);
            }

            //轮到本线路加载资源
            _isLoading = true;

            //等待相关依赖资源的加载
            yield return(LoadDependenciesAssetBundleAsync(info.AssetBundleName));

            float waitTime = Time.realtimeSinceStartup;

            UnityEngine.Object asset = null;

            if (LoadMode == ResourceLoadMode.Resource)
            {
                ResourceRequest request = Resources.LoadAsync <T>(info.ResourcePath);
                while (!request.isDone)
                {
                    loadingAction?.Invoke(request.progress);
                    yield return(null);
                }
                asset = request.asset;
                if (asset)
                {
                    if (isPrefab)
                    {
                        asset = ClonePrefab(asset as GameObject, parent, isUI);
                    }
                }
                else
                {
                    throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:Resources文件夹中不存在资源 " + info.ResourcePath + "!");
                }
            }
            else
            {
#if UNITY_EDITOR
                if (IsEditorMode)
                {
                    loadingAction?.Invoke(1);
                    yield return(null);

                    asset = AssetDatabase.LoadAssetAtPath <T>(info.AssetPath);
                    if (asset)
                    {
                        if (isPrefab)
                        {
                            asset = ClonePrefab(asset as GameObject, parent, isUI);
                        }
                    }
                    else
                    {
                        throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:路径中不存在资源 " + info.AssetPath + "!");
                    }
                }
                else
                {
                    yield return(LoadAssetBundleAsync(info.AssetBundleName, loadingAction));

                    if (AssetBundles.ContainsKey(info.AssetBundleName))
                    {
                        asset = AssetBundles[info.AssetBundleName].LoadAsset <T>(info.AssetPath);
                        if (asset)
                        {
                            if (isPrefab)
                            {
                                asset = ClonePrefab(asset as GameObject, parent, isUI);
                            }
                        }
                        else
                        {
                            throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath + " !");
                        }
                    }
                }
#else
                yield return(LoadAssetBundleAsync(info.AssetBundleName, loadingAction));

                if (AssetBundles.ContainsKey(info.AssetBundleName))
                {
                    asset = AssetBundles[info.AssetBundleName].LoadAsset <T>(info.AssetPath);
                    if (asset)
                    {
                        if (isPrefab)
                        {
                            asset = ClonePrefab(asset as GameObject, parent, isUI);
                        }
                    }
                    else
                    {
                        throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath + " !");
                    }
                }
#endif
            }

            float endTime = Time.realtimeSinceStartup;

            //Log.Info(string.Format("异步加载资源{0}[{1}模式]:\r\n{2}\r\n等待耗时:{3}秒  加载耗时:{4}秒"
            //    , asset ? "成功" : "失败"
            //    , LoadMode.ToString()
            //    , LoadMode == ResourceLoadMode.Resource ? info.GetResourceFullPath() : info.GetAssetBundleFullPath(AssetBundleRootPath)
            //    , waitTime - beginTime
            //    , endTime - waitTime));

            if (asset)
            {
                DataSetInfo dataSet = info as DataSetInfo;
                if (dataSet != null && dataSet.Data != null)
                {
                    asset.Cast <DataSetBase>().Fill(dataSet.Data);
                }

                loadDoneAction?.Invoke(asset as T);
            }
            else
            {
                loadDoneAction?.Invoke(null);
            }
            asset = null;

            //本线路加载资源结束
            _isLoading = false;
        }
コード例 #3
0
        internal static void AddBreakDownComponent(GameObject gameObject, BreakDownDefinition objDef)
        {
            BreakDown breakDown = gameObject.AddComponent <BreakDown>();

            BreakDown.m_BreakDownObjects.Add(breakDown);

            RCUtils.SetLayer(gameObject, vp_Layer.InteractiveProp);

            //Object yields
            if (objDef.yield != null && objDef.yield.Length > 0 && Settings.options.objectYields)
            {
                List <GameObject> itemYields = new List <GameObject>();
                List <int>        numYield   = new List <int>();

                foreach (BreakDownYield yield in objDef.yield)
                {
                    if (yield.item.Trim() != "")
                    {
                        //GameObject yieldItem = Resources.Load("GEAR_" + yield.item).Cast<GameObject>();

                        GameObject         yieldItem    = null;
                        UnityEngine.Object yieldItemObj = Resources.Load("GEAR_" + yield.item);

                        if (yieldItemObj != null)
                        {
                            yieldItem = yieldItemObj.Cast <GameObject>();
                            itemYields.Add(yieldItem);
                            numYield.Add(yield.num);
                        }
                        else
                        {
                            Debug.Log("[remove-clutter] Yield  GEAR_" + yield.item + " couldn't be loaded.");
                        }
                    }
                }

                breakDown.m_YieldObject      = itemYields.ToArray();
                breakDown.m_YieldObjectUnits = numYield.ToArray();
            }
            else
            {
                breakDown.m_YieldObject      = new GameObject[0];
                breakDown.m_YieldObjectUnits = new int[0];
            }


            //Time to harvest
            if (objDef.minutesToHarvest > 0 && !Settings.options.fastBreakDown)
            {
                breakDown.m_TimeCostHours = objDef.minutesToHarvest / 60;
            }
            else
            {
                breakDown.m_TimeCostHours = 1f / 60;
            }

            //Harvest sound

            /*MetalSaw
             * WoodSaw
             * Outerwear
             * MeatlSmall
             * Generic
             * Metal
             * MeatlMed
             * Cardboard
             * WoodCedar
             * NylonCloth
             * Plants
             * Paper
             * Wood
             * Wool
             * Leather
             * WoodReclaimedNoAxe
             * WoodReclaimed
             * Cloth
             * MeatLarge
             * WoodSmall
             * WoodFir
             * WoodAxe		*/
            if (objDef.sound.Trim() != "" && objDef.sound != null)
            {
                breakDown.m_BreakDownAudio = "Play_Harvesting" + objDef.sound;
            }
            else
            {
                breakDown.m_BreakDownAudio = "Play_HarvestingGeneric";
            }

            //Display name

            if (Settings.options.showObjectNames)
            {
                String   rawName  = objDef.filter.Replace("_", string.Empty);
                String[] objWords = Regex.Split(rawName, @"(?<!^)(?=[A-Z])");
                String   objName  = String.Join(" ", objWords);

                breakDown.m_LocalizedDisplayName = new LocalizedString()
                {
                    m_LocalizationID = objName
                };
            }
            else
            {
                breakDown.m_LocalizedDisplayName = new LocalizedString()
                {
                    m_LocalizationID = "GAMEPLAY_BreakDown"
                };
            }


            //Required Tools
            if (objDef.requireTool == true && Settings.options.toolsNeeded)
            {
                breakDown.m_RequiresTool = true;
            }

            if (objDef.tools != null && objDef.tools.Length > 0 && Settings.options.toolsNeeded)
            {
                Il2CppSystem.Collections.Generic.List <GameObject> itemTools = new Il2CppSystem.Collections.Generic.List <GameObject>();

                foreach (String tool in objDef.tools)
                {
                    GameObject selectedTool = null;

                    if (tool.ToLower() == "knife")
                    {
                        selectedTool = Resources.Load("GEAR_Knife").Cast <GameObject>();
                    }
                    else if (tool.ToLower() == "hacksaw")
                    {
                        selectedTool = Resources.Load("GEAR_Hacksaw").Cast <GameObject>();
                    }
                    else if (tool.ToLower() == "hatchet")
                    {
                        selectedTool = Resources.Load("GEAR_Hatchet").Cast <GameObject>();
                    }
                    else if (tool.ToLower() == "hammer")
                    {
                        selectedTool = Resources.Load("GEAR_Hammer").Cast <GameObject>();
                    }

                    if (selectedTool != null)
                    {
                        itemTools.Add(selectedTool);
                    }
                    else
                    {
                        Debug.Log("[remove-clutter] Tool " + tool + " couldn't be loaded or doesn't exist.");
                    }
                }

                UnhollowerBaseLib.Il2CppReferenceArray <GameObject> toolsArray = new UnhollowerBaseLib.Il2CppReferenceArray <GameObject>(itemTools.ToArray());

                if (toolsArray.Length > 0)
                {
                    breakDown.m_UsableTools = toolsArray;
                }
                else
                {
                    Debug.Log("[remove-clutter] Tools array is empty.");
                    breakDown.m_RequiresTool = false;
                    breakDown.m_UsableTools  = new GameObject[0];
                }
            }
            else
            {
                breakDown.m_UsableTools = new GameObject[0];
            }
        }