internal void InstantiatePrefab(string prefabGuid, AssetPack assetPack) { #if UNITY_EDITOR if (!Application.isPlaying) { var path = AssetDatabase.GUIDToAssetPath(prefabGuid); if (string.IsNullOrEmpty(path)) { Debug.LogWarning($"Cannot instantiate prefab with guid {prefabGuid}"); return; } var asset = AssetDatabase.LoadAssetAtPath <GameObject>(path); if (asset == null) { Debug.LogWarning($"Failed to load prefab asset at path {path}"); return; } m_GameObject = (GameObject)PrefabUtility.InstantiatePrefab(asset, Parent); return; } #endif m_GameObject = assetPack.TryInstantiatePrefab(prefabGuid, Parent); if (m_GameObject == null) { Debug.LogWarning($"Failed to instantiate prefab with guid {prefabGuid}"); m_GameObject = new GameObject(); m_GameObject.transform.parent = Parent; m_GameObject.name = $"Prefab Placeholder {prefabGuid}"; } }
// Initialize global content public static void Init(AssetPack assets) { sJump = assets.GetSample("jump"); sTongue = assets.GetSample("tongue"); sHurt = assets.GetSample("hurt"); sDie = assets.GetSample("die"); }
// Initialize global content static public void Init(AssetPack assets) { bmpFont = assets.GetBitmap("font"); sSelect = assets.GetSample("select"); sAccept = assets.GetSample("accept"); }
/// <summary> /// Returns a deep copy of the specified <see cref="AssetPackConfig"/>. /// </summary> /// <param name="assetPackConfig">The AssetPackConfig to copy.</param> /// <returns>A new copy of the original AssetPackConfig.</returns> public static AssetPackConfig DeepCopy(AssetPackConfig assetPackConfig) { // We copy the object's fields explicitly instead of calling Deserialize(Serialize(config)) because // Deserialize performs checks and transformations that can produce an inexact copy. // See https://github.com/google/play-unity-plugins/issues/143 for one such case. var copy = new AssetPackConfig { SplitBaseModuleAssets = assetPackConfig.SplitBaseModuleAssets, DefaultTextureCompressionFormat = assetPackConfig.DefaultTextureCompressionFormat, }; foreach (var pair in assetPackConfig.AssetPacks) { var assetPack = pair.Value; var assetPackCopy = new AssetPack { DeliveryMode = assetPack.DeliveryMode, AssetBundleFilePath = assetPack.AssetBundleFilePath, AssetPackDirectoryPath = assetPack.AssetPackDirectoryPath, CompressionFormatToAssetBundleFilePath = CopyDictionary(assetPack.CompressionFormatToAssetBundleFilePath), CompressionFormatToAssetPackDirectoryPath = CopyDictionary(assetPack.CompressionFormatToAssetPackDirectoryPath), }; copy.AssetPacks.Add(pair.Key, assetPackCopy); } return(copy); }
/// <summary> /// 添加bundle信息 /// </summary> /// <param name="bundleInfo"></param> public static void AddAssetBundleInfo(AssetBundleInfo bundleInfo) { _bundles[bundleInfo.id] = bundleInfo; IEnumerator <string> enumerator = bundleInfo.paths.GetEnumerator(); while (enumerator.MoveNext()) { string path = enumerator.Current; //因为英雄的partitions模型,业务内读取只给名字,所以这里存的时候key要只留名字 if (path.Contains("characterpartitions")) { string charPartitionsKey = Path.GetFileNameWithoutExtension(path); _pathToBundle[charPartitionsKey] = bundleInfo; } else { _pathToBundle[path] = bundleInfo; } } enumerator.Dispose(); enumerator = null; if (!_packs.ContainsKey(bundleInfo.packId)) { AssetPack pack = new AssetPack(); pack.id = bundleInfo.packId; pack.folder = Loader.GetBaseURL(bundleInfo.packId); pack.bundles = new List <AssetBundleInfo>(); pack.folders = Johny.HashtablePool.Claim(); _packs.Add(pack.id, pack); } _packs[bundleInfo.packId].bundles.Add(bundleInfo); }
static void ImportScene(string sceneName, string jsonText, AssetPack assetPack, string path = null) { var sceneModule = MARSSceneModule.instance; var wasBlockingEnsureSession = sceneModule.BlockEnsureSession; sceneModule.BlockEnsureSession = true; if (string.IsNullOrEmpty(path)) { var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene); scene.name = sceneName; } else { var scene = EditorSceneManager.OpenScene(path); foreach (var gameObject in scene.GetRootGameObjects().ToList()) { UnityObject.DestroyImmediate(gameObject); } } // TODO: preload prefabs CompanionSceneUtils.ImportScene(jsonText, assetPack); sceneModule.BlockEnsureSession = wasBlockingEnsureSession; }
private AssetPack _LoadAssetFromResource(string assetName, bool isKeepInMemory, System.Type type) { AssetPack ret = null; UnityEngine.Object asset = null; if (type != null) { asset = Resources.Load(assetName, type); } else { asset = Resources.Load(assetName); } if (asset != null) { if (!m_dicAsset.ContainsKey(assetName)) { ret = new AssetPack(asset, isKeepInMemory); m_dicAsset.Add(assetName, ret); } else { ret = m_dicAsset[assetName]; Debug.logger.LogWarning("ResMgr", "ResMgr._LoadAssetFromResource: asset name->" + assetName + ", already in asset pool"); } } else { Debug.logger.LogError("ResMgr", "ResMgr._LoadAssetFromResource: assetName->" + assetName + ", finds no asset!"); } return(ret); }
/// <summary> /// 释放栈内资源 /// </summary> public void PopAssetStack() { if (mAssetStack.Count == 0) { return; } List <string> list = mAssetStack.Pop(); List <string> removeList = new List <string>(); AssetPack info = null; for (int i = 0; i < list.Count; i++) { if (mAssetPackDic.TryGetValue(list[i], out info)) { info.Count--; if (info.Count < 1 && !info.IsKeepInMemory) { removeList.Add(list[i]); } } } for (int i = 0; i < removeList.Count; i++) { if (mAssetPackDic.ContainsKey(removeList[i])) { mAssetPackDic.Remove(removeList[i]); } } GC(); }
/// <summary> /// 卸载所有资源; /// </summary> public void ReleaseAllAsset() { List <string> delAsset = new List <string>(); foreach (KeyValuePair <string, AssetPack> kvp in m_AllAssets) { if (!kvp.Value.m_IsResident) //fufeng ni lao na yang? { delAsset.Add(kvp.Key); } } foreach (string assetName in delAsset) { ReleaseDownLoadCoroutine(assetName); //立即停止Coroutine; WWWDownLoader.StopLoad(assetName + m_strAssetExtension); WWWDownLoader.RemoveDownLoad(assetName + m_strAssetExtension, null); //AssetLoader内部的 www调用逻辑上来说没有回调.函数内部有非空判断; AssetPack assetPack = m_AllAssets[assetName]; m_AllAssets.Remove(assetName); assetPack.m_AssetRefer--; assetPack.AssetRelease = true; if (assetPack.m_AssetRefer > 0) { Debug.LogError("release asset still used: " + assetPack.m_AssetName); } assetPack = null; } delAsset.Clear(); }
// Loads an Asset Pack private static AssetPack LoadPack(string name) { AssetPack pack = InternalLogger.HandleThrow(() => AssetLoader.LoadPack(Path.Combine(ASSETS_FOLDER, name))); LOGGER.Log($"- Loaded '{Path.GetFileNameWithoutExtension(name)}'"); return(pack); }
// Constructor public HUD(AssetPack assets) { // Get bitmaps bmpFont = assets.GetBitmap("font"); bmpGuide = assets.GetBitmap("guide"); guideTimer = GUIDE_TIME; }
// Initialize global content static public void Init(AssetPack assets, AudioManager audio) { // Bitmaps bmpEnemy = assets.GetBitmap("enemy"); // Samples sThwomp = assets.GetSample("thwomp"); Enemy.audio = audio; }
public void LoadAssetAndInitialize(string assetName, Transform parent, Vector3 pos, System.Action <GameObject> call = null) { UnityEngine.Object ret = LoadAssetFromResource(assetName); if (ret == null) { AssetBundleReader reader = AssetBundleReaderManager.Instance.LoadAssetBundle(assetName); AssetPack ap = null; CoroutineAgent.WaitOperation( () => reader.IsLoaded, () => { if (reader.IsLoaded && reader.AssetBundleData != null) { ret = reader.AssetBundleData.LoadAsset(assetName.AssetFileName().NoExtension()); reader.UnLoadAssetBundle(); } if (ret != null) { if (!m_dicAsset.ContainsKey(assetName)) { ap = new AssetPack(ret, false); m_dicAsset.Add(assetName, ap); } else { ap = m_dicAsset[assetName]; Debug.logger.LogWarning("ResMgr", "ResMgr.LoadAssetAndInitialize: asset name->" + assetName + ", already in asset pool"); } } else { Debug.logger.LogError("ResMgr", "ResMgr.LoadAssetAndInitialize: assetName->" + assetName + ", finds no asset!"); } //targetObj.GetComponent<UITexture>().mainTexture = (Texture)ret; GameObject go = GameObject.Instantiate(ret) as GameObject; go.transform.parent = parent; go.transform.localPosition = pos; go.transform.localScale = Vector3.one; if (call != null) { call(go); } }, this); } else { GameObject go = GameObject.Instantiate(ret) as GameObject; go.transform.parent = parent; go.transform.localPosition = pos; go.transform.localScale = Vector3.one; if (call != null) { call(go); } } }
public override void Setup() { base.Setup(); progress = 0; rewards = 1; word = gm.words[Random.Range(0, gm.words.Length - 1)]; ap = gm.gameObject.GetComponent <AssetPack>(); board = Instantiate(ap.WordGameObject, gm.canvas); GameObject buttonTemplate = board.transform.Find("WordGameButton").gameObject; for (int i = 0; i < 64; i++) { buttons[i] = Instantiate(buttonTemplate, board.transform); GameObject button = buttons[i]; button.SetActive(true); Coordinates coords = GetCoordinates(i); button.GetComponent <RectTransform>().anchoredPosition = new Vector2(coords.x * 40 - 141, coords.y * -40 + 140); } bool horizontalWord = Random.Range(0, 2) == 0; int start = Random.Range(0, 7); int x = horizontalWord ? 0 : start; int y = horizontalWord ? start : 0; for (int i = 0; i < 8; i++) { int index = GetIndex(x, y); keys[i] = index; matrix[index] = word[i]; if (horizontalWord) { x++; } else { y++; } } for (int i = 0; i < 64; i++) { if (matrix[i] == (char)0) { matrix[i] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()[Random.Range(0, 25)]; } buttons[i].transform.Find("Text").GetComponent <Text>().text = matrix[i].ToString(); int index = i; buttons[i].GetComponent <Button>().onClick.AddListener(() => { Click(index); }); } }
private bool ChechLoadReady(string assetName) { if (m_AllAssets.ContainsKey(assetName)) { AssetPack assetPack = m_AllAssets[assetName]; return(assetPack.AssetReady); } return(true); }
// Constructor public Spiral(AssetPack assets) { bmpSpiral = assets.GetBitmap("spiral"); exist = false; scale = 1.0f; angle = 0.0f; pos = Vector2.Zero; }
public PrefabMetadataContainer(GameObject gameObject, SerializationMetadata metadata) { PropertyOverrides = new List <RuntimePrefabPropertyOverride>(); AssetPack.GetPrefabMetadata(gameObject, out var guid, metadata.AssetPack); m_Guid = guid; var overrides = PrefabUtility.GetObjectOverrides(gameObject, true); foreach (var over in overrides) { var instanceObject = over.instanceObject; if (instanceObject == null) { continue; } SerializationUtils.GetTransformPathAndComponentIndex(gameObject.transform, instanceObject, out var transformPath, out var componentIndex); RuntimePrefabPropertyOverride.GetOverrides(instanceObject, PropertyOverrides, transformPath, componentIndex, metadata); } RemovedComponents = new List <RuntimeRemovedComponent>(); var removedComponents = PrefabUtility.GetRemovedComponents(gameObject); foreach (var component in removedComponents) { // TODO: optimize get component index var assetComponent = component.assetComponent; // Stale overrides can have a null reference on assetComponent if (assetComponent == null) { continue; } assetComponent.gameObject.GetComponents(k_Components); var index = k_Components.IndexOf(assetComponent); if (index < 0) { Debug.LogWarning("Could not find removed component " + assetComponent); continue; } RemovedComponents.Add(new RuntimeRemovedComponent { TransformPath = gameObject.transform.GetTransformPath(component.containingInstanceGameObject.transform), ComponentIndex = index }); } AddedGameObjects = new List <RuntimeAddedGameObject>(); AddedComponents = new List <RuntimeAddedComponent>(); GetMetadataRecursively(gameObject, metadata); }
// Constructor public ObjectManager(AssetPack assets) { // Create spiral spiral = new Spiral(assets); // Create list of enemies enemies = new List <Enemy>(); // Get assets sSpawn = assets.GetSample("spawn"); }
public void UnloadAssetBundle(string assetName) { if (m_AllAssets.ContainsKey(assetName)) { AssetPack assetPack = m_AllAssets[assetName]; if (assetPack.AssetReady) { assetPack.UnloadAssetBundle(false); } } }
/// <summary> /// 获取下载内容的资源; /// </summary> public AssetBundle GetMainAssetBundle(string assetName) { if (m_AllAssets.ContainsKey(assetName)) { AssetPack assetPack = m_AllAssets[assetName]; if (assetPack.AssetReady) { return(assetPack.m_AssetBundle); } } return(null); }
// Initialize public override void Init() { // Get global components Global gs = (Global)globalScene; AssetPack assets = gs.GetAssets(); trans = gs.GetTransition(); // Get assets bmpFont = assets.GetBitmap("font"); sAccept = assets.GetSample("accept"); timeString = ""; }
/// <summary> /// 内部下载接口; /// </summary> private void LoadAsset(string assetName, bool bResident, Callback <string> callBack, bool isOnlyDownLoad, DownLoadOrderType downLoadOrderType, bool isKeepLoading) { AssetPack assetPack = null; if (m_AllAssets.ContainsKey(assetName)) { assetPack = m_AllAssets[assetName]; if (!assetPack.m_IsResident) { if (bResident) { assetPack.m_IsResident = true; assetPack.m_AssetRefer = 0; } else { if (isKeepLoading) //如果外部设置为KeepLoading,那么资源会切换为KeepLoading状态; { assetPack.m_nKeepLoadingRefer++; } assetPack.m_AssetRefer++; } } assetPack.m_CallBack += callBack; if (assetPack.AssetReady) { if (callBack != null) { callBack(assetName); } } } else { assetPack = new AssetPack(assetName, bResident, isKeepLoading); m_AllAssets.Add(assetName, assetPack); assetPack.m_CallBack += callBack; DownLoadCoroutine gtDLC = GetAssetLoadCoroutine(assetName); if (gtDLC != null) { gtDLC.StartCoroutine(LoadingAsset(assetName, isOnlyDownLoad, downLoadOrderType)); } else { Debug.LogError("AssetLoader LoadAssetAsync,Coroutine Obj can not be null."); } } }
/// <summary> /// 获取下载内容的资源; /// </summary> public Object GetMainAsset(string assetName) { if (m_AllAssets.ContainsKey(assetName)) { AssetPack assetPack = m_AllAssets[assetName]; if (assetPack.AssetReady) { if (assetPack.m_AssetObject == null && assetPack.m_AssetBundle != null) { assetPack.m_AssetObject = assetPack.m_AssetBundle.mainAsset; } return(assetPack.m_AssetObject); } } return(null); }
public UnityEngine.Object LoadAssetFromResource(string assetName, bool isKeepInMemory = false, System.Type type = null) { AssetPack assetPack = null; if (!m_dicAsset.TryGetValue(assetName, out assetPack) || assetPack == null) { assetPack = _LoadAssetFromResource(assetName, isKeepInMemory, type); if (assetPack != null) { AddAssetToTile(assetName); } } //如果指明了要keepInMemory// if (assetPack != null) { assetPack.isKeepInMemory = isKeepInMemory ? isKeepInMemory : assetPack.isKeepInMemory; } return(assetPack.asset); }
public void LoadAssetTexture(string assetName, GameObject targetObj, bool isKeepInMemory = false) { UnityEngine.Object ret = LoadAssetFromResource(assetName); if (ret == null) { AssetBundleReader reader = AssetBundleReaderManager.Instance.LoadAssetBundle(assetName); AssetPack ap = null; CoroutineAgent.WaitOperation( () => reader.IsLoaded, () => { if (reader.IsLoaded && reader.AssetBundleData != null) { ret = reader.AssetBundleData.LoadAsset(assetName.AssetFileName().NoExtension()); reader.UnLoadAssetBundle(); } if (ret != null) { if (!m_dicAsset.ContainsKey(assetName)) { ap = new AssetPack(ret, isKeepInMemory); m_dicAsset.Add(assetName, ap); } else { ap = m_dicAsset[assetName]; Debug.logger.LogWarning("ResMgr", "ResMgr.LoadAssetTexture: asset name->" + assetName + ", already in asset pool"); } } else { Debug.logger.LogError("ResMgr", "ResMgr.LoadAssetTexture: assetName->" + assetName + ", finds no asset!"); } targetObj.GetComponent <UITexture>().mainTexture = (Texture)ret; }, this); } else { targetObj.GetComponent <UITexture>().mainTexture = (Texture)ret; } }
/// <summary> /// 卸载资源; /// </summary> public void ReleaseAsset(string assetName, Callback <string> callBack, bool isKeepLoading) { if (m_AllAssets.ContainsKey(assetName)) { AssetPack assetPack = m_AllAssets[assetName]; if (assetPack != null) { if (assetPack.m_CallBack != null) { assetPack.m_CallBack -= callBack; } if (!assetPack.m_IsResident) { assetPack.m_AssetRefer--; if (isKeepLoading) { if (assetPack.State != AssetState.DownLoading && assetPack.State != AssetState.LocalLoading) //KeepLoading的任务, 只有在加载完成后,才能进行卸载; { assetPack.m_nKeepLoadingRefer--; } } if (assetPack.m_AssetRefer <= 0 && assetPack.m_nKeepLoadingRefer <= 0) //非常驻资源,没有外部使用,并且下载完成的情况下; { assetPack.m_CallBack = null; WWWDownLoader.StopLoad(assetName + m_strAssetExtension); WWWDownLoader.RemoveDownLoad(assetName + m_strAssetExtension, null); //AssetLoader内部的 www调用逻辑上来说没有回调.函数内部有非空判断; ReleaseDownLoadCoroutine(assetName); //立即停止Coroutine; assetPack.AssetRelease = true; m_AllAssets.Remove(assetName); } } } else { Debug.LogError("AssetLoader ReleaseAsset,AssetPack can not be null. " + assetName); } } }
/// <summary> /// AB异步加载 /// </summary> /// <param name="assetBundle"></param> /// <param name="p1"></param> /// <param name="loadListener"></param> /// <param name="p2"></param> /// <returns></returns> IEnumerator LoadABAsync(AssetBundle assetBundle, string assetName, ILoadListener loadListener, bool isKeepInMemory) { AssetBundleRequest abreq = assetBundle.LoadAssetAsync(GetABInfo(assetName).FullName); yield return(abreq); yield return(1); AssetPack asset = new AssetPack(assetName, abreq.asset, isKeepInMemory); if (mAssetPackDic.ContainsKey(asset.AssetName)) { mAssetPackDic[asset.AssetName] = asset; } else { mAssetPackDic.Add(asset.AssetName, asset); } loadListener.LoadFinish(abreq.asset); AddAssetToStackTop(asset.AssetName); assetBundle.Unload(false); }
public static UnityObject GetAsset(string guid, long fileId, AssetPack assetPack) { #if UNITY_EDITOR if (!Application.isPlaying) { var asset = GetAsset(guid, fileId); if (asset != null) { return(asset); } Debug.LogWarning("Cannot find a local asset with " + guid + ". Falling back to asset pack."); } #endif if (assetPack == null) { Debug.LogWarning("Cannot import asset with guid: " + guid + " and no asset pack"); return(null); } return(assetPack.GetAsset(guid, fileId)); }
/// <summary> /// AB加载完成回调 /// </summary> /// <param name="req"></param> private void LoadABFinish(ABReq req) { AssetBundle assetBundle = req.ABWww.assetBundle; // req.WWWDispose(); if (assetBundle == null) { if (req.LoadListener != null) { req.LoadListener.LoadFailure(req.AssetName); } } else { if (req.IsAsync) { StartCoroutine(LoadABAsync(assetBundle, req.AssetName, req.LoadListener, req.IsKeepInMemory)); } else { ABInfo abInfo = GetABInfo(req.AssetName); object obj = assetBundle.LoadAsset(abInfo.FullName); AssetPack asset = new AssetPack(abInfo.AssetName, obj, req.IsKeepInMemory); if (mAssetPackDic.ContainsKey(asset.AssetName)) { mAssetPackDic[asset.AssetName] = asset; } else { mAssetPackDic.Add(asset.AssetName, asset); } req.LoadListener.LoadFinish(obj); AddAssetToStackTop(asset.AssetName); assetBundle.Unload(false); } } }
// Constructor public Stage(AssetPack assets, bool shallow = false) { if (!shallow) { // Get maps Tilemap m; maps = new List <Tilemap>(); int index = 0; while ((m = assets.GetTilemap((index++).ToString())) != null) { maps.Add(m); mapHeight += m.GetHeight() * TILE_SIZE; } checkpoints = new List <Vector2>(); // Compute y translation transY = -maps[0].GetHeight() * TILE_SIZE; // Get bitmaps bmpTileset = assets.GetBitmap("tileset"); bmpWater = assets.GetBitmap("water"); } // Get background bitmaps bmpCloud = assets.GetBitmap("cloud"); bmpSky = assets.GetBitmap("sky"); // Create cloud positions cloudPos = new float[3]; int xoff = CLOUD_WIDTH / 3; for (int i = 0; i < 3; ++i) { cloudPos[i] += i * xoff; } }
/// <summary> /// 资源加载完毕 /// </summary> /// <param name="request"></param> private void LoadFinish(ResLoadRequest request) { if (request != null) { ILoadListent listen = null; for (int i = 0; i < request.listents.Count; i++) { listen = request.listents[i]; if (request.asset != null) { AssetPack pack = new AssetPack(request.type, request.isKeepInMemory); pack.asset = request.asset; mAssetPacksDic.Add(request.assetName,pack); listen.Succeed(request.asset); } else { listen.Failed(); } } } }