/// <summary> /// Set the sprite for a given object /// </summary> /// <param name="bullet"></param> /// <param name="bankEntry"></param> /// <param name="bulletGameObject"></param> /// <returns></returns> public BulletScript SetBulletSettings(BulletObject bullet, BulletBankEntry bankEntry, GameObject bulletGameObject) { // Change sprite if the field has been filled if (bankEntry.sprite != null) { SpriteRenderer spriteRenderer = bulletGameObject.GetComponentInChildren <SpriteRenderer>(); if (spriteRenderer != null) { spriteRenderer.sprite = bankEntry.sprite; } } // Make sure we have the appropriate script BulletScript bulletScript = bulletGameObject.GetComponent <BulletScript>(); if (bulletScript == null) { bulletScript = bulletGameObject.AddComponent <BulletScript>(); } bulletScript.DestroyWhenOutOfScreen = bankEntry.DestroyWhenOutOfScreen; // Auto destruction if (bankEntry.TimeToLiveInSeconds > 0) { StartCoroutine(DestroyAfterTimeOut(bullet, bankEntry.TimeToLiveInSeconds)); } return(bulletScript); }
/// <summary> /// Find the bullet prefab in the bank /// </summary> /// <param name="bulletName"></param> /// <returns></returns> public BulletBankEntry GetBulletPrefabFromBank(string bulletName) { BulletBankEntry bankEntry = null; if (string.IsNullOrEmpty(bulletName) == false) { // Match bullet names foreach (var entry in bulletBank.bullets) { if (entry.name.ToLower() == bulletName.ToLower()) { bankEntry = entry; } } } if (bankEntry == null) { if (useDefaultBulletIfMissing || string.IsNullOrEmpty(bulletName)) { bankEntry = bulletBank.bullets.FirstOrDefault(); } if (bankEntry == null) { Debug.LogError("Missing bank entry for bullet: " + bulletName + "!"); return(null); } } return(bankEntry); }
/// <summary> /// Create a bullet using the bullet bank /// </summary> /// <param name="bullet"></param> /// <param name="bulletName"></param> /// <returns></returns> public BulletScript CreateBulletFromBank(BulletObject bullet, string bulletName) { // Look in the bank BulletBankEntry bankEntry = GetBulletPrefabFromBank(bulletName); // Instatiate a prefab GameObject bulletGameObject = Instantiate(bankEntry.prefab, bullet.position, Quaternion.identity) as GameObject; return(SetBulletSettings(bullet, bankEntry, bulletGameObject)); }
/// <summary> /// Set the sprite for a given object /// </summary> /// <param name="bullet"></param> /// <param name="bankEntry"></param> /// <param name="bulletGameObject"></param> /// <returns></returns> public BulletScript SetBulletSettings(BulletObject bullet, BulletBankEntry bankEntry, GameObject bulletGameObject) { // Change sprite if the field has been filled if (bankEntry.sprite != null) { SpriteRenderer spriteRenderer = bulletGameObject.GetComponentInChildren<SpriteRenderer>(); if (spriteRenderer != null) { spriteRenderer.sprite = bankEntry.sprite; } } // Make sure we have the appropriate script BulletScript bulletScript = bulletGameObject.GetComponent<BulletScript>(); if (bulletScript == null) { bulletScript = bulletGameObject.AddComponent<BulletScript>(); } bulletScript.DestroyWhenOutOfScreen = bankEntry.DestroyWhenOutOfScreen; // Auto destruction if (bankEntry.TimeToLiveInSeconds > 0) { StartCoroutine(DestroyAfterTimeOut(bullet, bankEntry.TimeToLiveInSeconds)); } return bulletScript; }