2024-05-07 09:38:50 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Godot;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.utils;
|
|
|
|
|
|
|
|
|
|
public class NodeUtils
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Delete all child nodes</para>
|
|
|
|
|
/// <para>删除所有子节点</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="parent"></param>
|
2024-05-07 09:38:50 +00:00
|
|
|
|
public static int DeleteAllChild(Node parent)
|
2024-04-28 13:55:19 +00:00
|
|
|
|
{
|
2024-05-07 09:38:50 +00:00
|
|
|
|
var deleteNumber = 0;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
var count = parent.GetChildCount();
|
2024-05-07 09:38:50 +00:00
|
|
|
|
if (count <= 0) return deleteNumber;
|
|
|
|
|
for (var i = 0; i < count; i++)
|
2024-04-28 13:55:19 +00:00
|
|
|
|
{
|
2024-05-07 09:38:50 +00:00
|
|
|
|
var node = parent.GetChild(0);
|
|
|
|
|
parent.RemoveChild(node);
|
|
|
|
|
node.QueueFree();
|
|
|
|
|
deleteNumber++;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
}
|
2024-05-07 09:38:50 +00:00
|
|
|
|
|
|
|
|
|
return deleteNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>All child nodes are removed asynchronously</para>
|
|
|
|
|
/// <para>异步删除所有子节点</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="parent"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<int> DeleteAllChildAsync(Node parent)
|
|
|
|
|
{
|
|
|
|
|
return await Task.Run<int>(() => DeleteAllChild(parent));
|
2024-04-28 13:55:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|