/// <summary> /// Registers the specified asset identifier to this asset manager. /// If the passed in asset has an <see cref="IManagedAsset.identifier"/> that is already known, the known asset will be overridden! /// This can be used to implement modding or override specific assets purpusefully. /// </summary> public void RegisterAsset(IManagedAsset asset) { if (registeredAssets.Contains(asset)) { return; } int idx; if (identifierAssetMap.TryGetValue(asset.identifier, out idx)) { Debug.Log("Replaced existing asset with identifier " + asset.identifier + " with new asset! It was overridden!"); // Overwrite prefab var registeredAsset = _registeredAssets[idx]; registeredAssets.Remove(registeredAsset); registeredAssets.Add(asset); _registeredAssets[idx] = asset; } else { // Register prefab Debug.Log("Registered asset " + asset); registeredAssets.Add(asset); _registeredAssets.Add(asset); identifierAssetMap.Add(asset.identifier, _registeredAssets.Count - 1); } }
/// <summary> /// Deregisters the specified asset from this asset manager. /// Must have previously been registered by either asset bundle loading or <see cref="RegisterAsset(IManagedAsset)"/> /// </summary> public void DeregisterAsset(IManagedAsset asset) { if (!registeredAssets.Contains(asset)) { return; } identifierAssetMap.Remove(asset.identifier); _registeredAssets.Remove(asset); registeredAssets.Remove(asset); }
/// <summary> /// Called in order to determine whether or not the specified asset fits the criterias. /// </summary> public virtual bool MatchesCriterias(IManagedAsset asset) { if (!ReferenceEquals(this.tags, null) && this.tags.Count > 0) { var tags = asset.tags; for (int i = 0; i < this.tags.Count; i++) { // Look for the tag bool didntHaveTag = true; for (int j = 0; j < tags.Length; j++) { if (tags[j].Equals(this.tags[i])) { didntHaveTag = false; break; } } // Didnt have the tag? if (didntHaveTag) { return(false); } } } // Look for name if (!ReferenceEquals(this.names, null) && this.names.Count > 0) { if (!this.names.Contains(asset.name)) { return(false); } } // Look for type foreach (var t in this.types) { if (ReferenceEquals(asset, null) || !t.IsAssignableFrom(asset.GetType())) { return(false); } } return(true); }
/// <summary> /// Will retrieve the object registered with the specified identifier (<see cref="IManagedAsset.identifier"/>). /// </summary> /// <typeparam name="T">The type the objects must have to end up in the result set. Scriptable objects are being checked if they are assignable to the specified type. /// GameObjects will be checked whether or not they have a component of the specified type. /// If T is GameObject, the IManagedAsset implementation will be casted to Component to retrieve the gameobject.</typeparam> /// <param name="identifier"></param> /// <param name="throwCastException">whether or not an <see cref="System.InvalidCastException"/> will be thrown if the type T cannot be retrieved from an asset that has the specified tag.</param> /// <returns>The object, or null if no object was found.</returns> public T GetObject <T>(string identifier, bool throwCastException = false) where T : UnityEngine.Object { int assetIdx; if (!this.identifierAssetMap.TryGetValue(identifier, out assetIdx)) { return(null); } // Try casting and throw exception if requested IManagedAsset asset = this._registeredAssets[assetIdx]; T obj = asset.GetAs <T>(); if (Essentials.UnityIsNull(obj) && throwCastException) { throw new System.InvalidCastException("Object " + asset.name + " wasnt castable to " + typeof(T)); } return(obj); }