Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / activity / ActivityObject_EditorTool.cs
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using Godot;
  6. using UI.EditorTools;
  7.  
  8. public partial class ActivityObject
  9. {
  10. /// <summary>
  11. /// 该函数只会在编辑器中调用, 用于自定义处理被 [ExportFill] 标记后自动创建的节点
  12. /// </summary>
  13. /// <param name="propertyName">属性名称</param>
  14. /// <param name="node">节点实例</param>
  15. protected virtual void OnExportFillNode(string propertyName, Node node)
  16. {
  17. switch (propertyName)
  18. {
  19. case "ShadowSprite":
  20. {
  21. var sprite = (Sprite2D)node;
  22. sprite.ZIndex = -1;
  23. var material =
  24. ResourceManager.Load<ShaderMaterial>(ResourcePath.resource_material_Blend_tres, false);
  25. material.SetShaderParameter("blend", new Color(0, 0, 0, 0.47058824F));
  26. material.SetShaderParameter("schedule", 1);
  27. sprite.Material = material;
  28. }
  29. break;
  30. case "AnimatedSprite":
  31. {
  32. var animatedSprite = (AnimatedSprite2D)node;
  33. var material =
  34. ResourceManager.Load<ShaderMaterial>(ResourcePath.resource_material_Blend_tres, false);
  35. material.SetShaderParameter("blend", new Color(1, 1, 1, 1));
  36. material.SetShaderParameter("schedule", 0);
  37. animatedSprite.Material = material;
  38. }
  39. break;
  40. case "Collision":
  41. {
  42.  
  43. }
  44. break;
  45. }
  46. }
  47.  
  48. private void _InitNodeInEditor()
  49. {
  50. var parent = GetParent();
  51. if (parent != null)
  52. {
  53. //寻找 owner
  54. Node owner;
  55. if (parent.Owner != null)
  56. {
  57. owner = parent.Owner;
  58. }
  59. else if (Plugin.Plugin.Instance.GetEditorInterface().GetEditedSceneRoot() == this)
  60. {
  61. owner = this;
  62. }
  63. else
  64. {
  65. owner = parent;
  66. }
  67.  
  68. var type = GetType();
  69. var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  70. var propertyInfoList = new List<PropertyInfo>();
  71. foreach (var propertyInfo in propertyInfos)
  72. {
  73. if (propertyInfo.GetCustomAttributes(typeof(ExportFillNodeAttribute), false).Length > 0)
  74. {
  75. if (propertyInfo.GetCustomAttributes(typeof(ExportAttribute), false).Length == 0)
  76. {
  77. EditorToolsPanel.ShowConfirmInEditor("警告", $"'{type.FullName}'中字段'{propertyInfo.Name}'使用了[ExportAutoFill],\n但是并没有加上[Export], 请补上!");
  78. return;
  79. }
  80.  
  81. if (propertyInfo.PropertyType.IsAssignableTo(typeof(Node)))
  82. {
  83. if (propertyInfo.SetMethod == null)
  84. {
  85. EditorToolsPanel.ShowConfirmInEditor("警告", $"请为'{type.FullName}'中的'{propertyInfo.Name}'属性设置set访问器, 或者将set服务器设置成public!");
  86. return;
  87. }
  88. propertyInfoList.Add(propertyInfo);
  89. }
  90. }
  91. }
  92.  
  93. var tempList = new List<PropertyInfo>();
  94. Type tempType = null;
  95. var index = -1;
  96. for (int i = propertyInfoList.Count - 1; i >= 0; i--)
  97. {
  98. var item = propertyInfoList[i];
  99. if (tempType != item.DeclaringType || i == 0)
  100. {
  101. if (tempType == null)
  102. {
  103. index = i;
  104. }
  105. else
  106. {
  107. int j;
  108. if (i == 0)
  109. {
  110. j = i;
  111. }
  112. else
  113. {
  114. j = i + 1;
  115. }
  116. for (; j <= index; j++)
  117. {
  118. tempList.Add(propertyInfoList[j]);
  119. }
  120.  
  121. index = i;
  122. }
  123. tempType = item.DeclaringType;
  124. }
  125. }
  126. foreach (var propertyInfo in tempList)
  127. {
  128. var value = propertyInfo.GetValue(this);
  129. if (value == null || ((Node)value).GetParent() == null)
  130. {
  131. var node = GetNodeOrNull(propertyInfo.Name);
  132. if (node == null)
  133. {
  134. node = (Node)Activator.CreateInstance(propertyInfo.PropertyType);
  135. AddChild(node);
  136. node.Name = propertyInfo.Name;
  137. node.Owner = owner;
  138. //自定义处理导出的节点
  139. OnExportFillNode(propertyInfo.Name, node);
  140. }
  141. propertyInfo.SetValue(this, node);
  142. }
  143. }
  144. }
  145. }
  146. }