com.alicizax.unity.tuyoogam.../Editor/AssetBundleBuilder/BuildSystem/BuildContext.cs

66 lines
1.8 KiB
C#
Raw Normal View History

2025-01-09 11:31:04 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset.Editor
{
public class BuildContext
{
private readonly Dictionary<System.Type, IContextObject> _contextObjects = new Dictionary<System.Type, IContextObject>();
/// <summary>
2025-02-28 16:11:01 +08:00
/// 清空所有上下文对象
2025-01-09 11:31:04 +08:00
/// </summary>
public void ClearAllContext()
{
_contextObjects.Clear();
}
/// <summary>
2025-02-28 16:11:01 +08:00
/// 设置上下文对象
2025-01-09 11:31:04 +08:00
/// </summary>
public void SetContextObject(IContextObject contextObject)
{
if (contextObject == null)
throw new ArgumentNullException("contextObject");
var type = contextObject.GetType();
if (_contextObjects.ContainsKey(type))
throw new Exception($"Context object {type} is already existed.");
_contextObjects.Add(type, contextObject);
}
/// <summary>
2025-02-28 16:11:01 +08:00
/// 获取上下文对象
2025-01-09 11:31:04 +08:00
/// </summary>
public T GetContextObject<T>() where T : IContextObject
{
var type = typeof(T);
if (_contextObjects.TryGetValue(type, out IContextObject contextObject))
{
return (T)contextObject;
}
else
{
throw new Exception($"Not found context object : {type}");
}
}
2025-02-28 16:11:01 +08:00
/// <summary>
/// 获取上下文对象
/// </summary>
public T TryGetContextObject<T>() where T : IContextObject
{
var type = typeof(T);
if (_contextObjects.TryGetValue(type, out IContextObject contextObject))
{
return (T)contextObject;
}
else
{
return default;
}
}
2025-01-09 11:31:04 +08:00
}
}