private void AddAssetData(string bundlePath, eAssetType assetType, Asset asset) { var type = Asset.GetValueType(assetType); var key = $"{bundlePath}_{type.Name}"; this._assetMap.Add(key, asset); }
public static void LoadAssetMap(List<string> assetPaths, eAssetType assetType, System.Action<Dictionary<string,Object>> callback) { Dictionary<string,Object> assetMap = new Dictionary<string,Object>(); if(assetPaths == null) { Debug.LogWarning("Trying to load a asset map with null asset paths!"); if(callback != null) { callback(assetMap); } return; } foreach (string assetPath in assetPaths) { LoadAsset(assetPath, assetType, delegate(Object asset) { if(asset != null) { assetMap.Add(assetPath, asset); } if(callback != null && assetPaths.Count - 1 == assetPaths.IndexOf(assetPath)) { callback(assetMap); } }); } }
//编辑器模式下,直接用AssetDataBase或者Resource private void LoadAssetFromDatabase(Asset asset, bool warning, eAssetType assetType) { #if UNITY_EDITOR //编辑器模式下,直接用AssetDataBase或者Resource //if (Application.isPlaying && asset.AssetName.StartsWith("AssetBundles")) Profiler.BeginSample(nameof(LoadAssetFromDatabase)); if (Application.isPlaying) { var path = FindAssetPath(asset.AssetName); UnityEngine.Object obj = null; if (assetType == eAssetType.SceneAsset) { } else { obj = UnityEditor.AssetDatabase.LoadAssetAtPath(path, asset.ValueType); /*if (GameUtility.HasMissingScript(obj as GameObject)) * CommonLog.Log($"{path} prefab missing Script!");*/ } this.SetLoadAssetValue(asset, obj, warning); } Profiler.EndSample(); #endif }
//有参构造函数 public Asset(string assetName, eAssetType assetType) { Value = null; IsDone = false; AssetName = assetName; AssetType = assetType; }
//public IEnumerator LoadAsset(eAssetType _Type, string _AssetName) //{ // GameObject go; // yield return StartCoroutine(InstantiateGameObjectAsync(_Type.ToString().ToLower(), _AssetName, _go => go =_go )); //} #region 리소스폴더 로드 방식 public string GetResourcesLoadPath(eAssetType _Type, string _AssetName) { switch (_Type) { case eAssetType.Unit: return(string.Format("Prefab/Unit/{0}", _AssetName)); case eAssetType.Structure: return(string.Format("Prefab/Structure/{0}", _AssetName)); case eAssetType.Texture: return(string.Format("Texture/{0}", _AssetName)); case eAssetType.Sound: return(string.Format("Prefab/Structure/{0}", _AssetName)); case eAssetType.Effect: return(string.Format("Effect/{0}", _AssetName)); case eAssetType.UIAtlas: return(string.Format("Prefab/Structure/{0}", _AssetName)); case eAssetType.Map: return(string.Format("Prefab/Map/{0}", _AssetName)); case eAssetType.UI: return(string.Format("Prefab/UI/{0}", _AssetName)); default: return(string.Format("Prefab/Structure/{0}", _AssetName)); } }
//包含bundlePath的构造函数 public Asset(string assetName, eAssetType assetType, string bundlePath) { Value = null; IsDone = false; AssetName = assetName; AssetType = assetType; BundlePathKey = bundlePath; }
public Object Get(eAssetType _Type, string _AssetName) { if (_cache.ContainsKey(_AssetName) == false) { Load(_Type, _AssetName); } return(_cache[_AssetName]); }
//附带完成回调的构造函数 public Asset(string assetName, eAssetType assetType, string bundlePath, AssetLoaded callBack) { Value = null; IsDone = false; AssetName = assetName; AssetType = assetType; BundlePathKey = bundlePath; OnAssetLoaded += callBack; }
//移除资源,不接受管理了,即为释放掉 public void DisposeAsset(string assetName, eAssetType assetType) { string bundlePathKey = GetBundlePath(assetName, assetType); if (_assetMap.ContainsKey(bundlePathKey)) { DisposeAsset(_assetMap[bundlePathKey]); } }
/// <summary> /// PS,即便不存在,也能返回Asset,只是内容为空 /// </summary> /// <param name="pathAndassetName">路径大小写敏感</param> /// <param name="assetType"></param> /// <param name="warning">如果不存在,是否Log</param> /// <returns></returns> public Asset LoadAsset(string pathAndassetName, eAssetType assetType, bool warning = true) { pathAndassetName = CheckAssetPath(pathAndassetName); #if UNITY_EDITOR if (!Application.isPlaying) { CommonLog.Error("非运行模式下,请勿调用AssetManager"); return(null); } if (pathAndassetName.Contains(@"\")) { CommonLog.Error("路径{0}格式错误,请使用/替换\\", pathAndassetName); return(null); } #endif string bundlePath = this.GetBundlePath(pathAndassetName, assetType); Asset asset = this.CheckAssetInDicCache(assetType, bundlePath); if (asset != null) { if (asset.IsDone) { //asset.OnAssetLoaded_CallbackOnce = null; //if (asset.IsFromBundle) //{ // //if (asset.checkAssetIn(pathAndassetName) == false) // { // var loader = AssetBundleManager.Instance.GetBundleInfoByResName(asset.AssetName); // var val = loader.LoadByName(AssetBundleManager.Instance.GetAssetInBundleName(asset.AssetName), // asset.getValueType(assetType)); // this.SetLoadAssetValue(asset, val, warning); // //asset.addAssetIn(pathAndassetName, val); // } //else //{ // var val1 = asset.allAssets[pathAndassetName]; // this.SetLoadAssetValue(asset, val1, warning); //} //} return(asset); } this.LoadAssetFromResources(asset, warning, assetType); return(asset); } else { string bundlename = null; bool isFromBundle = AssetBundleManager.Instance.CheckIsInBundle(pathAndassetName, out bundlename); asset = new Asset(pathAndassetName, assetType, bundlePath, isFromBundle); this.LoadAssetFromResources(asset, warning, assetType); this.AddAssetData(bundlePath, assetType, asset); return(asset); } }
public Asset(string assetName, eAssetType assetType, string bundlePath, bool isFromBundle) { this._assetValue = null; this.IsDone = false; this.AssetName = assetName; this.AssetType = assetType; this.ValueType = GetValueType(assetType); this.BundlePathKey = bundlePath; this.IsFromBundle = isFromBundle; }
//销毁Instantiate对应出来的对象 public void ReleaseAsset(string Path, eAssetType assetType, UnityEngine.Object obj) { string bundlePath = this.GetBundlePath(Path, assetType); Asset asset = this.CheckAssetInDicCache(assetType, bundlePath); if (asset != null) { asset.ReleaseInstance(obj); } }
public string GetBundlePath(string assetName, eAssetType assetType) { if (AssetBundleManager.Instance.CheckIsInBundle(assetName, out var bundleName)) { return(bundleName); } else { return(assetName); } }
private Asset CheckAssetInDicCache(eAssetType assetType, string bundlePath) { //增加Type.Name为了解决Texture和Sprite使用相同的缓存无法互相转换 var type = Asset.GetValueType(assetType); if (this._assetMap.TryGetValue($"{bundlePath}_{type.Name}", out _tmpAsset)) { return(_tmpAsset); } return(null); }
public GameObject CreateAssetFrom_ResourcesFolder(eAssetType _Type, string _AssetName) { GameObject _prefab = (GameObject)Get(_Type, _AssetName); if (_prefab == null) { return(null); } return(GameUtil.CreateObjectToParent(_prefab, null)); }
public void DisposeAsset(string assetName, eAssetType assetType, bool isClear = false) { string bundlePath = this.GetBundlePath(assetName, assetType); Asset asset = this.CheckAssetInDicCache(assetType, bundlePath); if (asset != null && asset.IsFromBundle == false) { asset.Dispose(isClear); this._assetMap.Remove(bundlePath); } }
public Asset(string assetName, Type type, string bundlePath, bool isFromBundle, EventT <Asset> callBack) { this._assetValue = null; this.IsDone = false; this.AssetName = assetName; this.AssetType = eAssetType.None; this.ValueType = type; this.BundlePathKey = bundlePath; this.OnAssetLoaded_CallbackOnce = callBack; this.IsFromBundle = isFromBundle; }
public Asset GetAsset(string assetName, eAssetType assetType) { string bundlePath = this.GetBundlePath(assetName, assetType); Asset asset = this.CheckAssetInDicCache(assetType, bundlePath); if (asset != null) { return(asset); } return(null); }
/// <summary> /// PS,即便不存在,也能返回Asset,只是内容为空 /// </summary> /// <param name="pathAndassetName">路径大小写敏感</param> /// <param name="assetType"></param> /// <param name="warning">如果不存在,是否Log</param> /// <returns></returns> public Asset LoadAssetAsync(string pathAndassetName, eAssetType assetType, EventT <Asset> loadedCallBack = null, bool warning = true) { pathAndassetName = CheckAssetPath(pathAndassetName); #if UNITY_EDITOR if (!Application.isPlaying) { CommonLog.Error("非运行模式下,请勿调用AssetManager"); return(new Asset(pathAndassetName, assetType, null, false)); } if (pathAndassetName.Contains(@"\")) { CommonLog.Error("路径{0}格式错误,请使用/替换\\", pathAndassetName); return(null); } #endif //CommonLog.Log($ "加载类型为:{0}的资源:{1}", assetType, assetName); string bundlePath = this.GetBundlePath(pathAndassetName, assetType); Asset asset = this.CheckAssetInDicCache(assetType, bundlePath); if (asset != null) { if (loadedCallBack != null) { if (asset.IsDone) { loadedCallBack(asset); } else { //asset.OnAssetLoaded_CallbackOnce += loadedCallBack; asset.OnAssetLoaded_CallBacks.Add(loadedCallBack); } } return(asset); } string bundlename; bool isFromBundle = AssetBundleManager.Instance.CheckIsInBundle(pathAndassetName, out bundlename); asset = new Asset(pathAndassetName, assetType, bundlePath, isFromBundle, loadedCallBack); this.AddAssetData(bundlePath, assetType, asset); if (USED_AB_MODE) { //var task = GameTaskManager.Instance.CreateTask(this.LoadAssetFromResourcesAsync(asset, warning, assetType)); } else { LoadAssetFromDatabase(asset, warning, assetType); } return(asset); }
public void Load(eAssetType _Type, string _AssetName) { string _Path = GetResourcesLoadPath(_Type, _AssetName); Object t1 = Resources.Load(_Path); if (t1 != null) { _cache[t1.name] = t1; } else { Debug.LogError(_AssetName + " 리소스를 찾지 못했습니다."); } }
//获取一个asset,不保证资源一定不为空,所以get后加上判断 public Asset GetAsset(string assetName, eAssetType assetType) { string bundlePathKey = GetBundlePath(assetName, assetType); //check asset Asset tmpAsset = CheckAsset(bundlePathKey); //资源已经被加载,直接调用加载完成回调并且返回 if (tmpAsset != null) { return(tmpAsset); } return(null); }
public static Type GetValueType(eAssetType _type) { Type _typeData; switch (_type) { case eAssetType.TextAsset: _typeData = typeof(TextAsset); return(_typeData); case eAssetType.Texture: _typeData = typeof(Texture); return(_typeData); case eAssetType.GameObject: _typeData = typeof(GameObject); return(_typeData); case eAssetType.Sprite: _typeData = typeof(Sprite); return(_typeData); case eAssetType.SpriteAtlas: _typeData = typeof(SpriteAtlas); return(_typeData); case eAssetType.AudioClip: _typeData = typeof(AudioClip); return(_typeData); case eAssetType.AnimClip: _typeData = typeof(AnimationClip); return(_typeData); case eAssetType.Material: _typeData = typeof(Material); return(_typeData); case eAssetType.Font: _typeData = typeof(Font); return(_typeData); //case eAssetType.SkeletonDataAsset: // _typeData = typeof(SkeletonDataAsset); // return _typeData; } return(typeof(UnityEngine.Object)); }
//加载一个Asset //assetName: 资源名称 //assetType: 资源类型 //loadedCallBack: 资源加载完成时回调函数 public Asset LoadAsset(string assetName, eAssetType assetType, AssetLoaded loadedCallBack = null) { //check asset Asset tmpAsset = GetAsset(assetName, assetType); //资源已经被加载,直接调用加载完成回调并且返回 if (tmpAsset != null) { if (tmpAsset.IsDone) { if (loadedCallBack != null) { loadedCallBack(tmpAsset); } } else { if (loadedCallBack != null) { tmpAsset.OnAssetLoaded += loadedCallBack; } } // 没有加载成功, 也返回Asset, 只不过IsDone是false return(tmpAsset); } string bundlePathKey = GetBundlePath(assetName, assetType); //资源未被加载 tmpAsset = new Asset(assetName, assetType, bundlePathKey, loadedCallBack); if (!_assetMap.ContainsKey(bundlePathKey)) { _assetMap.Add(bundlePathKey, tmpAsset); } //加上协同异步方式 int time = TimeHelper.CurrentTimeMeasured(); LoadAsset(tmpAsset); //Log.W(ELogTag.NewbieGuide,"-----------------------------------load asset " + assetName + " time=" + (TimeHelper.CurrentTimeMeasured() - time)); return(tmpAsset); }
public IEnumerator InstantiateGameObjectAsync(eAssetType _assetType, string assetName, System.Action <GameObject> callback) { // This is simply to get the elapsed time for this phase of AssetLoading. // 어셋 로드 시작 시각을 세팅 float startTime = Time.realtimeSinceStartup; Object prefab; if (ResourceLoadType == eResourceLoadType.UnityEditor) { #region 리소스 폴더 로드 방식 prefab = Get(_assetType, assetName); #endregion } else { #region 어셋번들 로드 방식 string assetBundleName = _assetType.ToString().ToLower(); // Load asset from assetBundle. //어셋을 비동기로 로드함 AssetBundleLoadAssetOperation request = AssetBundleManager.LoadAssetAsync(assetBundleName, assetName, typeof(GameObject)); if (request == null) { yield break; } yield return(StartCoroutine(request)); // Get the asset. //비동기 로드가 끝난후 어셋을 게임오브젝트 타입으로 로드함 prefab = request.GetAsset <GameObject>(); #endregion } GameObject go; if (prefab != null) { go = (GameObject)Instantiate(prefab); //생성 callback(go); } // Calculate and display the elapsed time. float elapsedTime = Time.realtimeSinceStartup - startTime; Debug.Log(assetName + (prefab == null ? " was not" : " was") + " loaded successfully in " + elapsedTime + " seconds"); }
//同步读取一个资源 public Asset LoadAssetSync(string assetName, eAssetType assetType) { //check asset Asset tmpAsset = GetAsset(assetName, assetType); //资源已经被加载,直接调用加载完成回调并且返回 if (tmpAsset != null) { if (!tmpAsset.IsDone) { Log.E(ELogTag.ResourceSys, "Sync load a aready Aync loading resource " + assetName); return(null); } return(tmpAsset); } string bundlePathKey = GetBundlePath(assetName, assetType); tmpAsset = new Asset(assetName, assetType, bundlePathKey); if (IsAssetInBundle(assetName)) { LoadAssetFromAssetBundleSync(tmpAsset); } else { LoadAssetFromResourcesSync(tmpAsset); } if (!_assetMap.ContainsKey(bundlePathKey)) { _assetMap.Add(bundlePathKey, tmpAsset); } return(tmpAsset); }
private IEnumerator PreLoad(eAssetType type, System.Action <GameObject> callback) { GameObject go = null; if (_loadType == eLoadType.Resources) { go = Resources.Load <GameObject>(string.Format("Prefab/{0}", type.ToString())); } else if (_loadType == eLoadType.PackageAssetBundle) { var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, type.ToString().ToLower())); if (myLoadedAssetBundle == null) { Debug.LogError("asset bundle load failed!"); yield break; } go = myLoadedAssetBundle.LoadAsset <GameObject>(type.ToString()); } else if (_loadType == eLoadType.AmazonS3) { string uri = string.Format("{0}/{1}", amazon_s3_url, type.ToString().ToLower()); UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri, 0); yield return(request.SendWebRequest()); AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request); go = bundle.LoadAsset <GameObject>(type.ToString()); } if (go == null) { Debug.LogError("resource is null!"); } callback?.Invoke(go); yield break; }
public static void LoadAsset(string path, eAssetType assetType, System.Action<Object> callback = null) { if(!references.ContainsKey(path)) { int watchId = Core.Stopwatch.Start(); Object reference = Resources.Load(path); double time = Core.Stopwatch.End(watchId); if(reference == null) { Debug.LogError("Unable to find asset: " + path); if(callback != null) { callback(null); } return; } else { references[path] = reference; Debug.Log("Loaded asset: " + path + " (" + time + " ms)"); } } int current = counts.ContainsKey(path) ? counts[path] : 0; counts[path] = current++; if(callback != null) { callback(references[path]); } }
public static void CreateObject(string path, eAssetType assetType, System.Action<Object> callback = null) { if(assetType == eAssetType.Scenes) { Application.LoadLevelAdditive(path); if(callback != null) { callback(GameObject.Find(path)); } } else { LoadAsset(path, assetType, delegate(Object resource) { Object instance = null; if(resource != null) { instance = Object.Instantiate(resource); instance.name = instance.name.Replace("(Clone)", ""); } if(callback != null) { callback(instance); } }); } }
public Asset GetAsset(eAssetType i_Type) { return r_Assets[i_Type]; }
private void addAsset(string i_Name, eAssetType i_Type) { Texture2D texture = r_Game.Content.Load<Texture2D>(i_Name); r_Assets.Add(i_Type, new Asset(texture, null)); }
public Asset LoadAsset(AssetRef assetRef, eAssetType assetType, bool warning = true) { return(LoadAsset(assetRef.AssetPath, assetType, warning)); }
public FusionOccupancySensorAsset(eAssetType type) { Type = type; InstanceId = Guid.NewGuid().ToString(); }
private IEnumerator LoadAssetFromResourcesAsync(Asset asset, bool warning, eAssetType assetType) { if (USED_AB_MODE) { if (asset.IsFromBundle) { var abinfo = AssetBundleManager.Instance.GetBundleInfoByResName(asset.AssetName); if (abinfo == null) { if (warning) { CommonLog.Error("Bundle资源加载失败,缺少资源:{0},类型:{1},BundleName:{2}" , asset.AssetName, asset.AssetType.ToString(), abinfo.bundle); } asset.IsDone = true; } else { asset.SetBundle(abinfo); //异步加载Bundle if (!abinfo.isReady) { var e1 = abinfo.LoadBundleAsync(); yield return(e1); if (e1.IsError()) { CommonLog.Error("加载资源{0}的bundle加载异常", asset.AssetName); } } UnityEngine.Object reqValue = null; if (assetType != eAssetType.SceneAsset) { var req = abinfo.GetLoadAsyncByName(AssetBundleManager.Instance.GetAssetInBundleName(asset.AssetName), asset.ValueType); yield return(req); reqValue = req.asset; } else { // abinfo.LoadBundle(); //var req = abinfo.LoadBundleAsync(); //yield return req; } this.SetLoadAssetValue(asset, reqValue, warning); } } else { var res = Resources.LoadAsync(asset.AssetName, asset.ValueType); yield return(res); this.SetLoadAssetValue(asset, res.asset); } } // else // { // #if UNITY_EDITOR // //编辑器模式下,直接用AssetDataBase或者Resource // //if (Application.isPlaying && asset.AssetName.StartsWith("AssetBundles")) // if (Application.isPlaying) // { // var path = FindAssetPath(asset.AssetName); // UnityEngine.Object obj = null; // if (assetType == eAssetType.SceneAsset) // { // // } // else // { // obj = UnityEditor.AssetDatabase.LoadAssetAtPath(path, asset.ValueType); // if (GameUtility.HasMissingScript(obj as GameObject)) // CommonLog.Log($"{path} prefab missing Script!"); // } // this.SetLoadAssetValue(asset, obj, warning); // yield return asset; // } // yield return null; // #endif // } }
private void LoadAssetFromResources(Asset asset, bool warning, eAssetType assetType) { if (USED_AB_MODE) { if (asset.IsFromBundle) { var abInfo = AssetBundleManager.Instance.GetBundleInfoByResName(asset.AssetName); if (abInfo == null) { if (warning) { CommonLog.Error("Bundle资源加载失败,缺少资源:{0},类型:{1},BundleName:{2}" , asset.AssetName, asset.AssetType.ToString(), abInfo.bundle); } asset.IsDone = true; return; } else { asset.SetBundle(abInfo); UnityEngine.Object val = null; if (assetType == eAssetType.SceneAsset) { abInfo.LoadBundle(); } else { val = abInfo.LoadByName(AssetBundleManager.Instance.GetAssetInBundleName(asset.AssetName), asset.ValueType); } this.SetLoadAssetValue(asset, val, warning); } } else { this.SetLoadAssetValue(asset, Resources.Load(asset.AssetName, asset.ValueType), warning); } } else { #if UNITY_EDITOR //编辑器模式下,直接用AssetDataBase或者Resource if (Application.isPlaying) { //if (asset.AssetName.StartsWith(ABAssetStart)) { var path = FindAssetPath(asset.AssetName); if (path != null) { UnityEngine.Object obj = null; if (assetType == eAssetType.SceneAsset) { } else { obj = UnityEditor.AssetDatabase.LoadAssetAtPath(path, asset.ValueType); } this.SetLoadAssetValue(asset, obj, warning); } else { this.SetLoadAssetValue(asset, null, warning); } return; } } #endif } }
public Asset LoadAssetAsync(AssetRef assetRef, eAssetType assetType, EventT <Asset> loadedCallBack = null, bool warning = true) { return(LoadAssetAsync(assetRef.AssetPath, assetType, loadedCallBack, warning)); }