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