Skip to content

Commit fd7ae5d

Browse files
committed
add Flow_GameObjectAddComponent
add Flow_GameObjectGetOrAddComponent
1 parent 38d3e87 commit fd7ae5d

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Runtime/Flow/Models/Libraries/UnityExecutableLibrary.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
using System;
12
using System.Diagnostics.CodeAnalysis;
23
using Ceres.Annotations;
34
using Ceres.Graph.Flow.Annotations;
45
using Chris.Serialization;
56
using UnityEngine;
7+
using Random = UnityEngine.Random;
68
using UObject = UnityEngine.Object;
79

810
namespace Ceres.Graph.Flow.Utilities
@@ -100,6 +102,12 @@ public static GameObject Flow_FindGameObjectWithTag(string tag)
100102
return GameObject.FindWithTag(tag);
101103
}
102104

105+
/// <summary>
106+
/// Retrieves a reference to a component of specified type, by providing the component type as a method parameter.
107+
/// </summary>
108+
/// <param name="gameObject"></param>
109+
/// <param name="type"></param>
110+
/// <returns></returns>
103111
[ExecutableFunction(IsScriptMethod = true, IsSelfTarget = true), CeresLabel("GetComponent")]
104112
public static Component Flow_GameObjectGetComponent(GameObject gameObject,
105113
[ResolveReturn] SerializedType<Component> type)
@@ -113,6 +121,31 @@ public static Component Flow_GameObjectGetComponentInChildren(GameObject gameObj
113121
{
114122
return gameObject.GetComponentInChildren(type);
115123
}
124+
125+
/// <summary>
126+
/// Adds a component of the specified type to the GameObject.
127+
/// </summary>
128+
/// <param name="gameObject"></param>
129+
/// <param name="type"></param>
130+
/// <returns></returns>
131+
[ExecutableFunction(IsScriptMethod = true, IsSelfTarget = true), CeresLabel("AddComponent")]
132+
public static Component Flow_GameObjectAddComponent(GameObject gameObject,
133+
[ResolveReturn] SerializedType<Component> type)
134+
{
135+
return gameObject.AddComponent(type);
136+
}
137+
138+
[ExecutableFunction(IsScriptMethod = true, IsSelfTarget = true), CeresLabel("GetOrAddComponent")]
139+
public static Component Flow_GameObjectGetOrAddComponent(GameObject gameObject,
140+
[ResolveReturn] SerializedType<Component> type)
141+
{
142+
Type t = type;
143+
if (gameObject.TryGetComponent(t, out Component component))
144+
{
145+
return component;
146+
}
147+
return gameObject.AddComponent(type);
148+
}
116149

117150
#endregion GameObject
118151

0 commit comments

Comments
 (0)