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