public T LoadResource <T>(string path, bool cached = true) where T : UnityEngine.Object { T ret = null; if (!string.IsNullOrEmpty(path)) { ret = mGOPool.GetPoolPrefab(path) as T; if (ret == null) { ret = mAssetPool.Get <T>(path); } if (ret == null) { ret = _LoadResPrefab <T>(path); if (ret != null && cached) { GameObject prefab = ret as GameObject; if (prefab != null) { mGOPool.CachePoolPrefab(path, prefab, true); } else { mAssetPool.AddAsset(path, ret); } } } } return(ret); }
/// <summary> /// Create grass edge items /// </summary> private void CreateGrassObstacles() { // TODO: Handle possible special obstacles // Compute the maximum number of obstacles // [- m_activeLaneCount] accounts for a single-tile open path int itemCount = (m_activeLaneCount * m_activeTileCount) - m_activeLaneCount; // Prepare the pool if (m_grassObstacles == null) { m_grassObstacles = new AssetPool <Transform>(itemCount); } else { m_grassObstacles.ClearAllAssets(); m_grassObstacles.Resize(itemCount); } float[] prob = { m_tallTreeObstacleProb, m_mediumTreeObstacleProb, m_shortTreeObstacleProb, m_treeTrunkObstacleProb, m_rockObstacleProb }; // Populate the pool for (int i = 0; i < itemCount; ++i) { // Determine item type based on probability float rand = Random.value; float cumulative = prob[0]; MapItemType itemType = MapItemType.Tree_Tall; for (int j = 0; rand > cumulative; ++j) { if (j + 1 == prob.Length) { break; } cumulative += prob[j + 1]; itemType = (MapItemType)((int)itemType + 1); } // Create a new instance GameObject prefab = m_mapResources.GetItemPrefab(itemType); GameObject newItem = Instantiate <GameObject>(prefab); newItem.transform.parent = m_grassObstaclesRoot; newItem.SetActive(false); m_grassObstacles.AddAsset(newItem.transform); } }
/// <summary> /// Creates shared assets /// </summary> private void CreateSharedAssets() { // Create coins // Prepare the pool if (m_coins == null) { // Create a new one if it hasn't been created yet m_coins = new AssetPool <Coin>(m_coinCountMax); } else { // Clear the pool if it already exists m_coins.ClearAllAssets(); m_coins.Resize(m_coinCountMax); } // Populate the pool GameObject prefab = m_mapResources.GetCoinPrefab(); for (int i = 0; i < m_coinCountMax; ++i) { GameObject coinObj = Instantiate <GameObject>(prefab); Coin coinScript = coinObj.AddComponentNoDupe <Coin>(); coinObj.SetActive(false); coinObj.transform.parent = m_coinsRoot; coinScript.SetCoinRoot(m_coinsRoot); coinScript.Initialize(); m_coins.AddAsset(coinScript); } // Create the eagle if (m_eagle == null) { prefab = m_mapResources.GetEaglePrefab(); GameObject eagleObj = Instantiate <GameObject>(prefab); m_eagle = eagleObj.AddComponentNoDupe <Eagle>(); eagleObj.transform.parent = m_sharedItemsRoot; eagleObj.SetActive(false); } // Create the water splash if (m_waterSplash == null) { prefab = m_mapResources.GetWaterSplashPrefab(); GameObject waterSplashObj = Instantiate <GameObject>(prefab); m_waterSplash = waterSplashObj.GetComponent <ParticleSystem>(); waterSplashObj.transform.parent = m_sharedItemsRoot; waterSplashObj.SetActive(false); } }
public T TryLoadFromCache <T>(WWWType type, string url) where T : UnityEngine.Object { if (mAssetPool != null) { T t = mAssetPool.Get <T>(url); if (t != null) { return(t); } } if (HasResource(type, url)) { WWWFileInfo fileInfo = GetFileInfo(type, url); if (fileInfo != null) { string filePath = fileInfo.localPath; byte[] bytes = GameUtils.ReadAllBytesFromFile(filePath); if (typeof(T) == typeof(Texture2D)) { Texture2D tex = GameUtils.GetTextureFromBytes(bytes); tex.Apply(false, true); if (tex != null && mAssetPool != null) { tex.name = url; mAssetPool.AddAsset(url, tex); } return(tex as T); } } } return(default(T)); }