Skip to content

Commit 608cd8a

Browse files
committed
[增加]1. 增加Actor组件获取组件函数的是否创建函数的参数
1 parent f9c5d15 commit 608cd8a

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

GameFrameX.Core/Actors/Actor.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,52 @@ public void SetAutoRecycle(bool autoRecycle)
8585
/// 根据组件类型获取对应的IComponentAgent
8686
/// </summary>
8787
/// <typeparam name="T">组件类型</typeparam>
88+
/// <param name="isNew">是否当获取为空的时候默认创建,默认值为true</param>
8889
/// <returns></returns>
89-
public async Task<T> GetComponentAgent<T>() where T : IComponentAgent
90+
public async Task<T> GetComponentAgent<T>(bool isNew = true) where T : IComponentAgent
9091
{
91-
return (T)await GetComponentAgent(typeof(T));
92+
return (T)await GetComponentAgent(typeof(T), isNew);
9293
}
9394

9495
/// <summary>
9596
/// 根据组件类型获取对应的IComponentAgent
9697
/// </summary>
9798
/// <param name="agentType">代理类型</param>
99+
/// <param name="isNew">是否当获取为空的时候默认创建,默认值为true</param>
98100
/// <returns></returns>
99-
public async Task<IComponentAgent> GetComponentAgent(Type agentType)
101+
public async Task<IComponentAgent> GetComponentAgent(Type agentType, bool isNew = true)
100102
{
101103
var compType = agentType.BaseType.GetGenericArguments()[0];
102-
var comp = _componentsMap.GetOrAdd(compType, GetOrAddFactory);
103-
var agent = comp.GetAgent(agentType);
104-
if (!comp.IsActive)
104+
IComponentAgent agent;
105+
if (isNew)
106+
{
107+
var comp = _componentsMap.GetOrAdd(compType, GetOrAddFactory);
108+
agent = comp.GetAgent(agentType);
109+
if (!comp.IsActive)
110+
{
111+
async Task Worker()
112+
{
113+
await comp.Active();
114+
agent.Active();
115+
}
116+
117+
await SendAsyncWithoutCheck(Worker);
118+
}
119+
120+
return agent;
121+
}
122+
123+
if (!_componentsMap.TryGetValue(compType, out var component))
124+
{
125+
return default;
126+
}
127+
128+
agent = component.GetAgent(agentType);
129+
if (!component.IsActive)
105130
{
106131
async Task Worker()
107132
{
108-
await comp.Active();
133+
await component.Active();
109134
agent.Active();
110135
}
111136

0 commit comments

Comments
 (0)