Skip to content

Commit

Permalink
feat: Extend the CloneTemplate (#489)
Browse files Browse the repository at this point in the history
* Add Overload to pass a AssetReferenceGameObject to clone by GUID or Asset Path.

* Accept Code Readability Suggestion.

Co-authored-by: Metious <71298690+Metious@users.noreply.github.com>

---------

Co-authored-by: Metious <71298690+Metious@users.noreply.github.com>
  • Loading branch information
MrPurple6411 and Metious authored Oct 13, 2023
1 parent de757bf commit 86412ec
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions Nautilus/Assets/PrefabTemplates/CloneTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
namespace Nautilus.Assets.PrefabTemplates;

using System.Collections;
using System.Collections.Generic;
using Nautilus.Utility;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UWE;

namespace Nautilus.Assets.PrefabTemplates;

/// <summary>
/// Represents a prefab clone template.
/// </summary>
public class CloneTemplate : PrefabTemplate
{
private string _classIdToClone;
private AssetReferenceGameObject _prefabToClone;
private TechType _techTypeToClone;
private SpawnType _spawnType;

Expand All @@ -35,24 +37,33 @@ public class CloneTemplate : PrefabTemplate
/// </summary>
/// <param name="info">The prefab info to base this template off of.</param>
/// <param name="techTypeToClone">The tech type to clone and use for this template.</param>
public CloneTemplate(PrefabInfo info, TechType techTypeToClone) : this(info, techTypeToClone, null) {}
public CloneTemplate(PrefabInfo info, TechType techTypeToClone) : this(info, techTypeToClone, null, null) {}

/// <summary>
/// Creates a <see cref="CloneTemplate"/> instance.
/// </summary>
/// <param name="info">The prefab info to base this template off of.</param>
/// <param name="classIdToClone">The class ID to clone and use for this template.</param>
public CloneTemplate(PrefabInfo info, string classIdToClone) : this(info, TechType.None, classIdToClone) {}
public CloneTemplate(PrefabInfo info, string classIdToClone) : this(info, TechType.None, classIdToClone, null) {}

/// <summary>
/// Creates a <see cref="CloneTemplate"/> instance.
/// </summary>
/// <param name="info">The prefab info to base this template off of.</param>
/// <param name="prefabToClone">The AssetReferenceGameObject with a valid key to clone and use for this template.</param>
public CloneTemplate(PrefabInfo info, AssetReferenceGameObject prefabToClone) : this(info, TechType.None, null, prefabToClone) { }


private CloneTemplate(PrefabInfo info, TechType techTypeToClone, string classIdToClone) : base(info)

private CloneTemplate(PrefabInfo info, TechType techTypeToClone, string classIdToClone, AssetReferenceGameObject prefabToClone) : base(info)
{
_techTypeToClone = techTypeToClone;
_classIdToClone = classIdToClone;
_spawnType = techTypeToClone switch
{
default(TechType) => SpawnType.ClassId,
_ => SpawnType.TechType
};
_prefabToClone = prefabToClone;
_spawnType = techTypeToClone is not TechType.None ? SpawnType.TechType
: !string.IsNullOrWhiteSpace(classIdToClone) ? SpawnType.ClassId
: prefabToClone is not null ? SpawnType.Prefab
: throw new System.Exception("CloneTemplate Missing valid identifier.");
}

/// <summary>
Expand Down Expand Up @@ -81,7 +92,20 @@ public override IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject)
yield return CraftData.InstantiateFromPrefabAsync(_techTypeToClone, gameObject);
obj = gameObject.Get();
}
else
else if(_spawnType == SpawnType.Prefab)
{
var task = _prefabToClone.InstantiateAsync();
yield return task;

if (task.Status != UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded)
{
InternalLogger.Error($"Couldn't find prefab with key: '{_prefabToClone.RuntimeKey}'.");
yield break;
}

obj = task.Result;
}
else if(_spawnType == SpawnType.ClassId)
{
var task = PrefabDatabase.GetPrefabAsync(_classIdToClone);
yield return task;
Expand Down Expand Up @@ -144,6 +168,7 @@ private void ApplySkin(GameObject obj)
private enum SpawnType
{
TechType,
ClassId
ClassId,
Prefab
}
}

0 comments on commit 86412ec

Please sign in to comment.