C#杂项

戈多CS源码

类声明

1
public class Node : GodotObject

枚举声明

1
2
3
4
5
6
7
8
public enum ProcessModeEnum : long
{
Inherit,
Pausable,
WhenPaused,
Always,
Disabled
}

FLAGS声明

  • 可以组合多种状态
  • 示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//声明
[Flags]
public fliagsenum MySlags{A=1,B=2,C=4};
//函数
public void func(fliags a)
{
if((fliags & MySlags.A)== MySlags.A)
{
//第一个方法
}
if((fliags & MySlags.A)== MySlags.A)
{
//第二个方法
}
}
//调用
int main()
{
func(MySlags.A|Slags.B) //此时可以调用两个方法
}
1
2
3
4
5
6
7
[Flags]
public enum ProcessThreadMessagesEnum : long
{
Messages = 1L,
MessagesPhysics = 2L,
MessagesAll = 3L
}

信号声明 委托

1
public delegate void ChildEnteredTreeEventHandler(Node node);

属性声明

1
2
3
4
public new class PropertyName : GodotObject.PropertyName
{
public static readonly StringName _ImportPath = "_import_path";
}

方法声明

1
2
3
4
5
6
7
public new class MethodName : GodotObject.MethodName
{
//
// 摘要:
// Cached name for the '_enter_tree' method.
public static readonly StringName _EnterTree = "_enter_tree";
}

信号声明

1
2
3
4
5
6
7
public new class SignalName : GodotObject.SignalName
{
//
// 摘要:
// Cached name for the 'ready' signal.
public static readonly StringName Ready = "ready";
}

通知

1
public const long NotificationEnterTree = 10L;

方法字段?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
     private static readonly Type CachedType = typeof(Node);

private static readonly StringName NativeName = "Node";



[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private unsafe static readonly delegate* unmanaged<IntPtr> NativeCtor = GodotObject.ClassDB_get_constructor(NativeName);

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static readonly IntPtr MethodBind0 = GodotObject.ClassDB_get_method_with_compatibility(NativeName, MethodName.PrintOrphanNodes, 3218959716uL);

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static readonly IntPtr MethodBind1 = GodotObject.ClassDB_get_method_with_compatibility(NativeName, MethodName.AddSibling, 2570952461uL);


[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static readonly StringName MethodProxyName__enter_tree = "_EnterTree";

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static readonly StringName MethodProxyName__exit_tree = "_ExitTree";

属性实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public StringName Name
{
get
{
return GetName();
}
set
{
SetName(value);
}
}

//
// 摘要:
// Sets this node's name as a unique name in its Godot.Node.Owner. This allows the
// node to be accessed as %Name instead of the full path, from any node within that
// scene.
//
// If another node with the same owner already had that name declared as unique,
// that other node's name will no longer be set as having a unique name.
public bool UniqueNameInOwner
{
get
{
return IsUniqueNameInOwner();
}
set
{
SetUniqueNameInOwner(value);
}
}

事件信号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public event Action Ready
{
add
{
Connect(SignalName.Ready, Callable.From(value));
}
remove
{
Disconnect(SignalName.Ready, Callable.From(value));
}
}

//
// 摘要:
// Emitted when the node is renamed.
public event Action Renamed
{
add
{
Connect(SignalName.Renamed, Callable.From(value));
}
remove
{
Disconnect(SignalName.Renamed, Callable.From(value));
}
}

信号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public unsafe event ChildEnteredTreeEventHandler ChildEnteredTree
{
add
{
Connect(SignalName.ChildEnteredTree, Callable.CreateWithUnsafeTrampoline((Delegate)value, (delegate*<object, NativeVariantPtrArgs, out godot_variant, void>)(&ChildEnteredTreeTrampoline)));
}
remove
{
Disconnect(SignalName.ChildEnteredTree, Callable.CreateWithUnsafeTrampoline((Delegate)value, (delegate*<object, NativeVariantPtrArgs, out godot_variant, void>)(&ChildEnteredTreeTrampoline)));
}
}

//
// 摘要:
// Emitted when a child node is about to exit the scene tree, either because it
// is being removed or freed directly, or because this node is exiting the tree.
//
//
// When this signal is received, the child node is still in the tree and valid.
// This signal is emitted after the child node's own Godot.Node.TreeExiting and
// Godot.Node.NotificationExitTree.
public unsafe event ChildExitingTreeEventHandler ChildExitingTree
{
add
{
Connect(SignalName.ChildExitingTree, Callable.CreateWithUnsafeTrampoline((Delegate)value, (delegate*<object, NativeVariantPtrArgs, out godot_variant, void>)(&ChildExitingTreeTrampoline)));
}
remove
{
Disconnect(SignalName.ChildExitingTree, Callable.CreateWithUnsafeTrampoline((Delegate)value, (delegate*<object, NativeVariantPtrArgs, out godot_variant, void>)(&ChildExitingTreeTrampoline)));
}
}

方法实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public T GetNode<T>(NodePath path) where T : class
{
return (T)(object)GetNode(path);
}

public T GetNodeOrNull<T>(NodePath path) where T : class
{
return GetNodeOrNull(path) as T;
}


public T GetChild<T>(int idx, bool includeInternal = false) where T : class
{
return (T)(object)GetChild(idx, includeInternal);
}
1
2
3
4
5
6
7
public virtual void _EnterTree()
{
}

public virtual void _ExitTree()
{
}

字典初始化

示例 1:使用构造函数初始化一个空的 Dictionary

1
Dictionary<string, int> dictionary = new Dictionary<string, int>();

示例 2:使用集合初始化器初始化并填充 Dictionary

1
2
3
4
5
6
Dictionary<string, int> dictionary = new Dictionary<string, int>
{
{ "apple", 1 },
{ "banana", 2 },
{ "cherry", 3 }
};

示例 3:使用 ToDictionary 扩展方法从集合初始化

假设你有一个包含键值对的列表或数组,你可以使用 ToDictionary 方法来初始化 Dictionary

1
2
3
4
5
6
7
8
List<Tuple<string, int>> list = new List<Tuple<string, int>>
{
new Tuple<string, int>("apple", 1),
new Tuple<string, int>("banana", 2),
new Tuple<string, int>("cherry", 3)
};

Dictionary<string, int> dictionary = list.ToDictionary(entry => entry.Item1, entry => entry.Item2);

或者,如果你有一个匿名类型或具体的类,并且该类具有两个属性可以用作键和值,你也可以这样做。

示例 4:使用LINQ查询初始化 Dictionary

你可以从其他集合(如列表、数组或数据库查询结果)使用LINQ查询来初始化 Dictionary

1
2
3
4
5
6
7
8
List<Fruit> fruits = new List<Fruit>
{
new Fruit { Name = "apple", Quantity = 1 },
new Fruit { Name = "banana", Quantity = 2 },
new Fruit { Name = "cherry", Quantity = 3 }
};

Dictionary<string, int> dictionary = fruits.ToDictionary(fruit => fruit.Name, fruit => fruit.Quantity);

在这个例子中,Fruit 是一个简单的类,它有两个属性:NameQuantity

示例 5:使用 DictionaryAdd 方法填充

如果你已经有一个空的 Dictionary,你可以使用 Add 方法来逐个添加键值对。

1
2
3
4
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("apple", 1);
dictionary.Add("banana", 2);
dictionary.Add("cherry", 3);

请注意,如果你尝试向 Dictionary 添加一个已经存在的键,它将抛出一个 ArgumentException。在添加之前,你可以使用 ContainsKey 方法来检查键是否已存在。

CS

接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public interface MoveAnd
{
//普通函数
void Moves(Vector2 dir) ;
//属性
string name { get; set; }
//索引器
int this[int index] { get; set; }
//事件
event Action<int> Move;
//带实现方法
void Bar() => GD.Print("打印");
//私有方法
private void AddToGroup(string name) { }
//受保护方法
protected void Update() { }
//静态方法
static void staticeme() { }
//静态抽象函数方法 C#11
//static abstract void Func();
//静态虚函数方法
//static virtual void func() { GD.Print("打印");}
//委托
delegate void Action(int index);
}

委托,函数指针

1
2
3
4
5
6
7
8
9
10
delegate void Action(int index);  //定义委托
void Func(int index) //函数,参数根委托一致
{
GD.print("执行委托");
}

int main()
{
Action act=new Action(Func); //调用委托
}

事件

1
2
3
4
5
6
7
8
9
10
11
12
13
delegate void Action(int index);
public class Game
{
public event Action 事件;
public void 发送事件()
{
事件.Invoke();
}
public void 链接事件()
{
事件+=On函数;
}
}