Skip to content

Commit

Permalink
Merge pull request #30 from AkiKurisu/dev
Browse files Browse the repository at this point in the history
Merge Dev 1.1.1 features and bug fix
  • Loading branch information
AkiKurisu authored Oct 19, 2024
2 parents 54adc3f + e7ff12c commit 8d46fc1
Show file tree
Hide file tree
Showing 24 changed files with 1,106 additions and 779 deletions.
50 changes: 50 additions & 0 deletions Docs/Animations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Animations

Create dynamic animation sequence and cutscene from script based on Playables.

## Features

- Crossfade multi `RuntimeAnimatorController` and `AnimationClip`.

## AnimationProxy Example

```C#
public class MontageExample : MonoBehaviour
{
public Animator animator;
private AnimationProxy animationProxy;
public RuntimeAnimatorController controllerA;
public RuntimeAnimatorController controllerB;
private IEnumerator Start()
{
animationProxy = new AnimationProxy(animator);
animationProxy.LoadAnimator(controllerA, 0.5f); /* Crossfade animator to controllerA in 0.5s */
yield return new WaitForSeconds(1f);
animationProxy.LoadAnimator(controllerB, 0.5f); /* Crossfade controllerA to controllerB in 0.5s */
yield return new WaitForSeconds(1f);
chaAnimationProxy.Stop(0.5f); /* Crossfade controllerB to animator in 0.5s */
}
}
```

## SequenceBuilder Example

```C#
public class SequenceExample : MonoBehaviour
{
public Animator animator;
private AnimationProxy animationProxy;
public AnimationClip[] clips;
private void Start()
{
animationProxy = new AnimationProxy(animator);
using var builder = chaAnimationProxy.CreateSequenceBuilder();
foreach (var clip in clips)
{
builder.Append(clip, clip.length * 3 /* Play 3 loop */, 0.25f /* BlendIn duration */);
}
builder.SetBlendOut(0.5f);
builder.Build().Run();
}
}
```
7 changes: 7 additions & 0 deletions Docs/Animations.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Docs/Scheduler.md → Docs/Schedulers.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Scheduler
# Schedulers

Zero allocation timer/frame counter.

Expand Down
File renamed without changes.
7 changes: 6 additions & 1 deletion Modules/Resource/Editor/SoftAssetReferenceEditorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using UObject = UnityEngine.Object;
using Kurisu.Framework.Serialization;
using UnityEditor.AddressableAssets.Settings.GroupSchemas;
namespace Kurisu.Framework.Resource.Editor
{
public static class SoftAssetReferenceEditorUtils
Expand Down Expand Up @@ -130,7 +131,11 @@ public static AddressableAssetGroup GetOrCreateAssetGroup(string groupName)
{
var group = AddressableAssetSettingsDefaultObject.Settings.groups.FirstOrDefault(x => x.name == groupName);
if (group != null) return group;
return AddressableAssetSettingsDefaultObject.Settings.CreateGroup(groupName, false, false, true, AddressableAssetSettingsDefaultObject.Settings.DefaultGroup.Schemas);
group = AddressableAssetSettingsDefaultObject.Settings.CreateGroup(groupName, false, false, true, AddressableAssetSettingsDefaultObject.Settings.DefaultGroup.Schemas);
// Ensure address is included in build
BundledAssetGroupSchema infoSchema = group.GetSchema<BundledAssetGroupSchema>();
infoSchema.IncludeAddressInCatalog = true;
return group;
}
public static AddressableAssetEntry AddAsset(this AddressableAssetGroup group, UObject asset, params string[] labels)
{
Expand Down
16 changes: 10 additions & 6 deletions Modules/Resource/Runtime/ResourceCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
using Cysharp.Threading.Tasks;
namespace Kurisu.Framework.Resource
{
public class InvalidResourceRequestException : Exception
{
public string InvalidAddress { get; }
public InvalidResourceRequestException() : base() { }
public InvalidResourceRequestException(string address, string message) : base(message) { InvalidAddress = address; }
}
/// <summary>
/// Loading and cache specific asset as a group and release them by control version
/// </summary>
Expand All @@ -20,15 +14,25 @@ public class ResourceCache<TAsset> : IDisposable, IReadOnlyDictionary<string, TA
private readonly Dictionary<string, ResourceHandle<TAsset>> internalHandles = new();
private readonly Dictionary<string, TAsset> cacheMap = new();
private readonly Dictionary<string, int> versionMap = new();

/// <summary>
/// Validate asset location before loading, throw <see cref="InvalidResourceRequestException"/> if not exist
/// </summary>
/// <value></value>
public bool AddressSafeCheck { get; set; } = false;

/// <summary>
/// Current cache version
/// </summary>
/// <value></value>
public int Version { get; private set; } = 0;

public IEnumerable<string> Keys => cacheMap.Keys;

public IEnumerable<TAsset> Values => cacheMap.Values;

public int Count => cacheMap.Count;

public TAsset this[string key] => cacheMap[key];

/// <summary>
Expand Down
13 changes: 11 additions & 2 deletions Modules/Resource/Runtime/ResourceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
using Cysharp.Threading.Tasks;
namespace Kurisu.Framework.Resource
{
/// <summary>
/// Exception thrown when request resource address is invalid
/// </summary>
public class InvalidResourceRequestException : Exception
{
public string InvalidAddress { get; }
public InvalidResourceRequestException() : base() { }
public InvalidResourceRequestException(string address, string message) : base(message) { InvalidAddress = address; }
}
/// <summary>
/// Resource system that loads resource by address and label based on Addressables.
/// </summary>
Expand Down Expand Up @@ -99,7 +108,7 @@ public static async UniTask SafeCheckAsync<TAsset>(object key)
if (location.Status != AsyncOperationStatus.Succeeded || location.Result.Count == 0)
{
string stringValue;
if (key is IEnumerable<string> list) stringValue = $"[{string.Join(",", list)}]";
if (key is IEnumerable<string> list) stringValue = $"[{string.Join(',', list)}]";
else stringValue = key.ToString();
throw new InvalidResourceRequestException(stringValue, $"Address {stringValue} not valid for loading {typeof(TAsset)} asset");
}
Expand All @@ -118,7 +127,7 @@ public static async UniTask SafeCheckAsync<TAsset>(IEnumerable key, MergeMode me
if (location.Status != AsyncOperationStatus.Succeeded || location.Result.Count == 0)
{
string stringValue;
if (key is IEnumerable<string> list) stringValue = $"[{string.Join(",", list)}]";
if (key is IEnumerable<string> list) stringValue = $"[{string.Join(',', list)}]";
else stringValue = key.ToString();
throw new InvalidResourceRequestException(stringValue, $"Address {stringValue} not valid for loading {typeof(TAsset)} asset");
}
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ I supposed these code are useful but also not-must-be-included, which is my pers
[Pool](./Docs/Pool.md)
> Zero allocation GameObject/Component pooling.
[Scheduler](./Docs/Scheduler.md)
[Schedulers](./Docs/Schedulers.md)
> Zero allocation timer/frame counter.
[Serialization](./Docs/Serialization.md)
Expand All @@ -25,6 +25,9 @@ I supposed these code are useful but also not-must-be-included, which is my pers
[Data Driven](./Docs/DataDriven.md)
>Use Unreal-like DataTable workflow in Unity.
[Animations](./Docs/Animations.md)
>Create dynamic animation sequence and cutscene from script based on Playables.
## Modules

Modules are based on core features.
Expand Down
1 change: 0 additions & 1 deletion Runtime/Core/Events/EventSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using UnityEngine;
namespace Kurisu.Framework.Events
{
Expand Down
5 changes: 5 additions & 0 deletions Runtime/Core/Events/Interfaces/IEventCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ public interface IEventCoordinator
/// </summary>
/// <value></value>
CallbackEventHandler GetCallbackEventHandler();
/// <summary>
/// Get coordinator's dispatcher
/// </summary>
/// <value></value>
EventDispatcher EventDispatcher { get; }
}
}
5 changes: 5 additions & 0 deletions Runtime/Core/Events/Interfaces/IEventDispatchingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public interface IEventDispatchingStrategy
bool CanDispatchEvent(EventBase evt);
void DispatchEvent(EventBase evt, IEventCoordinator coordinator);
}
public interface IEventDispatchingListener
{
void OnPushDispatcherContext();
void OnPopDispatcherContext();
}
internal static class EventDispatchUtilities
{
public static void PropagateEvent(EventBase evt)
Expand Down
6 changes: 3 additions & 3 deletions Runtime/Core/Events/Models/EventBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ private set
}


internal bool StopDispatch
public bool StopDispatch
{
get { return (Status & LifeCycleStatus.StopDispatch) != LifeCycleStatus.None; }
set
Expand Down Expand Up @@ -447,7 +447,7 @@ protected bool Pooled
}
}

internal abstract void Acquire();
public abstract void Acquire();
/// <summary>
/// Implementation of <see cref="IDisposable"/>.
/// </summary>
Expand Down Expand Up @@ -554,7 +554,7 @@ static void ReleasePooled(T evt)
}
}

internal override void Acquire()
public override void Acquire()
{
m_RefCount++;
}
Expand Down
26 changes: 24 additions & 2 deletions Runtime/Core/Events/Models/EventDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ private struct DispatchContext
public uint m_GateCount;
public Queue<EventRecord> m_Queue;
}

private readonly List<IEventDispatchingListener> dispatchingListeners;
private readonly Stack<DispatchContext> m_DispatchContexts = new();
public EventDispatcher(IList<IEventDispatchingStrategy> strategies)
{
dispatchingListeners = new List<IEventDispatchingListener>();
m_DispatchingStrategies = new List<IEventDispatchingStrategy>();
#if UNITY_EDITOR
m_DebuggerEventDispatchingStrategy = new DebuggerEventDispatchingStrategy();
Expand Down Expand Up @@ -136,6 +137,11 @@ public void Dispatch(EventBase evt, IEventCoordinator coordinator, DispatchMode

public void PushDispatcherContext()
{
foreach (var listener in dispatchingListeners)
{
listener.OnPushDispatcherContext();
}

// Drain the event queue before pushing a new context.
ProcessEventQueue();

Expand All @@ -154,6 +160,11 @@ public void PopDispatcherContext()
m_GateCount = m_DispatchContexts.Peek().m_GateCount;
m_Queue = m_DispatchContexts.Peek().m_Queue;
m_DispatchContexts.Pop();

foreach (var listener in dispatchingListeners)
{
listener.OnPopDispatcherContext();
}
}

internal void CloseGate()
Expand Down Expand Up @@ -281,6 +292,17 @@ private void ApplyDispatchingStrategies(EventBase evt, IEventCoordinator coordin
}
}
}

public TListener GetEventDispatchingListener<TListener>() where TListener : IEventDispatchingListener
{
foreach (var listener in dispatchingListeners)
{
if (listener is TListener tlistener) return tlistener;
}
return default;
}
public void AddEventDispatchingListener(IEventDispatchingListener listener)
{
dispatchingListeners.Add(listener);
}
}
}
5 changes: 4 additions & 1 deletion Runtime/Core/Tasks/Models/TaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ protected virtual void Init()
{
mStatus = TaskStatus.Stopped;
}

public virtual void Stop()
{
mStatus = TaskStatus.Stopped;
Expand All @@ -97,6 +96,10 @@ public virtual void Pause()
public virtual void Tick()
{

}
protected void CompleteTask()
{
mStatus = TaskStatus.Completed;
}
protected virtual void Reset()
{
Expand Down
File renamed without changes.
Loading

0 comments on commit 8d46fc1

Please sign in to comment.