Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / activity / ActivityObject_EditorTool.cs
@小李xl 小李xl on 18 Jun 2023 6 KB 解决升级Godot版本产生的bug
  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] 标记属性节点, 当在编辑器中切换页签或者创建 ActivityObject 时会调用该函数
  12. /// </summary>
  13. /// <param name="propertyName">属性名称</param>
  14. /// <param name="node">节点实例</param>
  15. /// <param name="isJustCreated">是否刚刚创建</param>
  16. protected virtual void OnExamineExportFillNode(string propertyName, Node node, bool isJustCreated)
  17. {
  18. switch (propertyName)
  19. {
  20. case "ShadowSprite":
  21. {
  22. var sprite = (Sprite2D)node;
  23. if (isJustCreated)
  24. {
  25. sprite.ZIndex = -1;
  26. }
  27.  
  28. if (sprite.Material == null)
  29. {
  30. var material =
  31. ResourceManager.Load<ShaderMaterial>(ResourcePath.resource_material_Blend_tres, false);
  32. material.SetShaderParameter("blend", new Color(0, 0, 0, 0.47058824F));
  33. material.SetShaderParameter("schedule", 1);
  34. sprite.Material = material;
  35. }
  36. }
  37. break;
  38. case "AnimatedSprite":
  39. {
  40. var animatedSprite = (AnimatedSprite2D)node;
  41. if (animatedSprite.Material == null)
  42. {
  43. var material =
  44. ResourceManager.Load<ShaderMaterial>(ResourcePath.resource_material_Blend_tres, false);
  45. material.SetShaderParameter("blend", new Color(1, 1, 1, 1));
  46. material.SetShaderParameter("schedule", 0);
  47. animatedSprite.Material = material;
  48. }
  49. }
  50. break;
  51. case "Collision":
  52. {
  53.  
  54. }
  55. break;
  56. }
  57. }
  58.  
  59. private void _InitNodeInEditor()
  60. {
  61. var parent = GetParent();
  62. if (parent != null)
  63. {
  64. //寻找 owner
  65. Node owner;
  66. if (parent.Owner != null)
  67. {
  68. owner = parent.Owner;
  69. }
  70. else if (Plugin.Plugin.Instance.GetEditorInterface().GetEditedSceneRoot() == this)
  71. {
  72. owner = this;
  73. }
  74. else
  75. {
  76. owner = parent;
  77. }
  78.  
  79. var type = GetType();
  80. var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  81. var propertyInfoList = new List<PropertyInfo>();
  82. foreach (var propertyInfo in propertyInfos)
  83. {
  84. if (propertyInfo.GetCustomAttributes(typeof(ExportFillNodeAttribute), false).Length > 0)
  85. {
  86. if (propertyInfo.GetCustomAttributes(typeof(ExportAttribute), false).Length == 0)
  87. {
  88. EditorToolsPanel.ShowConfirmInEditor("警告", $"'{type.FullName}'中字段'{propertyInfo.Name}'使用了[ExportAutoFill],\n但是并没有加上[Export], 请补上!");
  89. return;
  90. }
  91.  
  92. if (propertyInfo.PropertyType.IsAssignableTo(typeof(Node)))
  93. {
  94. if (propertyInfo.SetMethod == null)
  95. {
  96. EditorToolsPanel.ShowConfirmInEditor("警告", $"请为'{type.FullName}'中的'{propertyInfo.Name}'属性设置set访问器, 或者将set服务器设置成public!");
  97. return;
  98. }
  99. propertyInfoList.Add(propertyInfo);
  100. }
  101. }
  102. }
  103.  
  104. var tempList = new List<PropertyInfo>();
  105. Type tempType = null;
  106. var index = -1;
  107. for (int i = propertyInfoList.Count - 1; i >= 0; i--)
  108. {
  109. var item = propertyInfoList[i];
  110. if (tempType != item.DeclaringType || i == 0)
  111. {
  112. if (tempType == null)
  113. {
  114. index = i;
  115. }
  116. else
  117. {
  118. int j;
  119. if (i == 0)
  120. {
  121. j = i;
  122. }
  123. else
  124. {
  125. j = i + 1;
  126. }
  127. for (; j <= index; j++)
  128. {
  129. tempList.Add(propertyInfoList[j]);
  130. }
  131.  
  132. index = i;
  133. }
  134. tempType = item.DeclaringType;
  135. }
  136. }
  137. foreach (var propertyInfo in tempList)
  138. {
  139. var value = propertyInfo.GetValue(this);
  140. if (value == null || ((Node)value).GetParent() == null)
  141. {
  142. var node = _FindNodeInChild(this, propertyInfo.Name, propertyInfo.PropertyType);
  143. if (node == null)
  144. {
  145. node = (Node)Activator.CreateInstance(propertyInfo.PropertyType);
  146. AddChild(node);
  147. node.Name = propertyInfo.Name;
  148. node.Owner = owner;
  149. OnExamineExportFillNode(propertyInfo.Name, node, true);
  150. }
  151. else
  152. {
  153. OnExamineExportFillNode(propertyInfo.Name, node, false);
  154. }
  155. propertyInfo.SetValue(this, node);
  156. }
  157. else
  158. {
  159. OnExamineExportFillNode(propertyInfo.Name, (Node)value, false);
  160. }
  161. }
  162. }
  163. }
  164.  
  165. private Node _FindNodeInChild(Node node, string name, Type type)
  166. {
  167. var childCount = node.GetChildCount();
  168. for (int i = 0; i < childCount; i++)
  169. {
  170. var child = node.GetChild(i);
  171. if (child.Name == name && child.GetType().IsAssignableTo(type))
  172. {
  173. return child;
  174. }
  175. else
  176. {
  177. var result = _FindNodeInChild(child, name, type);
  178. if (result != null)
  179. {
  180. return result;
  181. }
  182. }
  183. }
  184.  
  185. return null;
  186. }
  187. }