Skip to content

Commit

Permalink
feat: Prefab Template Processors (#453)
Browse files Browse the repository at this point in the history
* Added OnPrefabProcessor method to PrefabTemplates

* Singular prefab processor bug

Fixed a bug where you could only register one prefab processor
  • Loading branch information
Metious committed Aug 31, 2023
1 parent e6e2809 commit 7ada413
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Nautilus/Assets/CustomPrefab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,11 @@ public void AddOnUnregister(Action onUnregisterCallback)
/// Sets a prefab template as the game object constructor of this custom prefab.
/// </summary>
/// <param name="prefabTemplate">The prefab template object to set.</param>
public void SetGameObject(PrefabTemplate prefabTemplate) => Prefab = prefabTemplate.GetPrefabAsync;
public void SetGameObject(PrefabTemplate prefabTemplate)
{
Prefab = prefabTemplate.GetPrefabAsync;
SetPrefabPostProcessor(prefabTemplate.OnPrefabPostProcessor);
}

/// <summary>
/// Sets a game object as the prefab of this custom prefab.
Expand All @@ -282,13 +286,13 @@ public void SetGameObject(GameObject prefab)
/// Sets a post processor for the <see cref="Prefab"/>. This is an asynchronous version.
/// </summary>
/// <param name="postProcessorAsync">The post processor to set.</param>
public void SetPrefabPostProcessor(Func<GameObject, IEnumerator> postProcessorAsync) => OnPrefabPostProcess = obj => postProcessorAsync(obj);
public void SetPrefabPostProcessor(Func<GameObject, IEnumerator> postProcessorAsync) => OnPrefabPostProcess += obj => postProcessorAsync(obj);

/// <summary>
/// Sets a post processor for the <see cref="Prefab"/>. This is a synchronous version.
/// </summary>
/// <param name="postProcessor">The post processor to set.</param>
public void SetPrefabPostProcessor(Action<GameObject> postProcessor) => OnPrefabPostProcess = obj => SyncPostProcessor(obj, postProcessor);
public void SetPrefabPostProcessor(Action<GameObject> postProcessor) => OnPrefabPostProcess += obj => SyncPostProcessor(obj, postProcessor);

/// <summary>
/// Registers this custom prefab into the game.
Expand Down
10 changes: 10 additions & 0 deletions Nautilus/Assets/PrefabTemplates/PrefabTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@ public PrefabTemplate(PrefabInfo info)
/// If the provided task result already has a game object set to it, it will try to set the necessary components first. Otherwise; sets a default implementation of this entity type.</param>
/// <returns>A coroutine operation. Must be used with either <c>yield return</c>, or <see cref="MonoBehaviour.StartCoroutine(IEnumerator)"/>.</returns>
public abstract IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject);

/// <summary>
/// Use this method to make changes to the prefab after the Nautilus' prefab processing is completed. Can be used to override or add more features to a prefab once it's settled.
/// </summary>
/// <param name="prefab">The prefab to process.</param>
/// <returns>A coroutine operation.</returns>
public virtual IEnumerator OnPrefabPostProcessor(GameObject prefab)
{
yield break;
}
}

0 comments on commit 7ada413

Please sign in to comment.