Skip to content

Commit

Permalink
Remove animator instantiate workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Aug 15, 2024
1 parent 8004f9b commit 00d5c1a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Source/CustomAvatar/Avatar/AvatarLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ private async Task<AvatarPrefab> LoadAssetBundle(string fullPath, IProgress<floa
throw new AvatarLoadException("Could not load asset from asset bundle");
}

GameObject instance = UnityEngine.Object.Instantiate(prefabObject);
avatarPrefab = _container.InstantiateComponent<AvatarPrefab>(prefabObject, [fullPath]);

avatarPrefab = _container.InstantiateComponent<AvatarPrefab>(instance, new object[] { fullPath });
prefabObject.hideFlags |= HideFlags.DontUnloadUnusedAsset;
prefabObject.name = $"AvatarPrefab({avatarPrefab.descriptor.name})";

instance.name = $"AvatarPrefab({avatarPrefab.descriptor.name})";
instance.SetActive(false);
await ShaderRepair.FixShadersOnGameObjectAsync(prefabObject);

await ShaderRepair.FixShadersOnGameObjectAsync(instance);
prefabObject.hideFlags &= ~HideFlags.DontUnloadUnusedAsset;

return avatarPrefab;
}
Expand Down
71 changes: 71 additions & 0 deletions Source/CustomAvatar/Utilities/AsyncInstantiateOperationAwaiter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Beat Saber Custom Avatars - Custom player models for body presence in Beat Saber.
// Copyright © 2018-2024 Nicolas Gnyra and Beat Saber Custom Avatars Contributors
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

using System;
using System.Runtime.CompilerServices;
using UnityEngine;

#pragma warning disable IDE1006
namespace CustomAvatar.Utilities
{
internal struct AsyncInstantiateOperationAwaiter<T> : ICriticalNotifyCompletion where T : UnityEngine.Object
{
private AsyncInstantiateOperation<T> asyncOperation;
private Action continuationAction;

public AsyncInstantiateOperationAwaiter(AsyncInstantiateOperation<T> asyncOperation)
{
this.asyncOperation = asyncOperation;
this.continuationAction = null;
}

public bool IsCompleted => asyncOperation.isDone;

public AsyncInstantiateOperation<T> GetResult()
{
if (continuationAction != null)
{
asyncOperation.completed -= Continue;
continuationAction = null;
}

AsyncInstantiateOperation<T> op = asyncOperation;
asyncOperation = null;
return op;
}

public void OnCompleted(Action continuation)
{
UnsafeOnCompleted(continuation);
}

public void UnsafeOnCompleted(Action continuation)
{
if (continuationAction != null)
{
throw new InvalidOperationException("Continuation is already registered");
}

continuationAction = continuation;
asyncOperation.completed += Continue;
}

private void Continue(AsyncOperation _)
{
continuationAction?.Invoke();
}
}
}
2 changes: 2 additions & 0 deletions Source/CustomAvatar/Utilities/AsyncOperationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ namespace CustomAvatar.Utilities
internal static class AsyncOperationExtensions
{
public static AsyncOperationAwaiter<T> GetAwaiter<T>(this T asyncOperation) where T : AsyncOperation => new(asyncOperation);

public static AsyncInstantiateOperationAwaiter<T> GetAwaiter<T>(this AsyncInstantiateOperation<T> asyncOperation) where T : Object => new(asyncOperation);
}
}

0 comments on commit 00d5c1a

Please sign in to comment.