为了简化Godot 的编写,我会将我的扩展方法写在这里面。
public static class GD_Extensions
{
///
/// 假数据生成,详情请看Bogus官方文档
///
public static Faker Faker = new Faker();
///
/// 获取子节点,需要保证子节点命名完全一致
///
///
///
/// node跟节点
/// 子节点属性,需要保证和场景命名完全一致
/// 获取子节点命名字符串
///
public static void GetChildNode<T1, T2>(this T1 root, ref T2 childNode,
[CallerArgumentExpression(nameof(childNode))] string nameExpression = null)
where T1 : Node where T2 : Node
{
childNode = root.GetNode<T2>(nameExpression);
if (childNode == null)
{
var str = $"{nameExpression} node is null!";
GD.Print(str);
throw new Exception(str);
}
}
///
/// Godot 序列号输出
///
///
///
public static void GD_Print(object obj, Formatting formatting = Formatting.None)
{
GD.Print(JsonConvert.SerializeObject(obj, formatting));
}
///
/// 获取输入转为2D移动
///
///
public static Vector2 GetMoveInput()
{
var velocity = Vector2.Zero;
if (Input.IsActionPressed("ui_right"))
{
velocity.X += 1;
}
if (Input.IsActionPressed("ui_left"))
{
velocity.X -= 1;
}
if (Input.IsActionPressed("ui_up"))
{
velocity.Y -= 1;
}
if (Input.IsActionPressed("ui_down"))
{
velocity.Y += 1;
}
return velocity;
}
}