The Swartz-Manning’s first exhibit will provide a detailed history of Aaron Swartz Day. https://www.aaronswartzday.org/vr
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomAction.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * This script demonstrates how to create a new action that can be accessed from the
  3. * ProBuilder toolbar.
  4. *
  5. * A new menu item is registered under "Geometry" actions called "Make Double-Sided".
  6. *
  7. * To enable, remove the #if PROBUILDER_API_EXAMPLE and #endif directives.
  8. */
  9. // Uncomment this line to enable the menu action.
  10. // #define PROBUILDER_API_EXAMPLE
  11. #if PROBUILDER_API_EXAMPLE
  12. using UnityEngine;
  13. using UnityEditor;
  14. using ProBuilder2.Common;
  15. using ProBuilder2.EditorCommon;
  16. using ProBuilder2.MeshOperations;
  17. using ProBuilder2.Interface;
  18. using System.Linq;
  19. using System.Collections.Generic;
  20. namespace MySpecialNamespace.Actions
  21. {
  22. /**
  23. * This class is responsible for loading the pb_MenuAction into the toolbar and menu.
  24. */
  25. [InitializeOnLoad]
  26. static class RegisterCustomAction
  27. {
  28. /**
  29. * Static initializer is called when Unity loads the assembly.
  30. */
  31. static RegisterCustomAction()
  32. {
  33. // This registers a new MakeFacesDoubleSided menu action with the toolbar.
  34. pb_EditorToolbarLoader.RegisterMenuItem(InitCustomAction);
  35. }
  36. /**
  37. * Helper function to load a new menu action object.
  38. */
  39. static pb_MenuAction InitCustomAction()
  40. {
  41. return new MakeFacesDoubleSided();
  42. }
  43. /**
  44. * Usually you'll want to add a menu item entry for your action.
  45. * https://docs.unity3d.com/ScriptReference/MenuItem.html
  46. */
  47. [MenuItem("Tools/ProBuilder/Geometry/Make Faces Double-Sided", true)]
  48. static bool MenuVerifyDoSomethingWithPbObject()
  49. {
  50. // Using pb_EditorToolbarLoader.GetInstance keeps MakeFacesDoubleSided as a singleton.
  51. MakeFacesDoubleSided instance = pb_EditorToolbarLoader.GetInstance<MakeFacesDoubleSided>();
  52. return instance != null && instance.IsEnabled();
  53. }
  54. [MenuItem("Tools/ProBuilder/Geometry/Make Faces Double-Sided", false, pb_Constant.MENU_GEOMETRY + 3)]
  55. static void MenuDoDoSomethingWithPbObject()
  56. {
  57. MakeFacesDoubleSided instance = pb_EditorToolbarLoader.GetInstance<MakeFacesDoubleSided>();
  58. if(instance != null)
  59. pb_EditorUtility.ShowNotification(instance.DoAction().notification);
  60. }
  61. }
  62. /**
  63. * This is the actual action that will be executed.
  64. */
  65. public class MakeFacesDoubleSided : pb_MenuAction
  66. {
  67. public override pb_ToolbarGroup group { get { return pb_ToolbarGroup.Geometry; } }
  68. public override Texture2D icon { get { return null; } }
  69. public override pb_TooltipContent tooltip { get { return _tooltip; } }
  70. /**
  71. * What to show in the hover tooltip window. pb_TooltipContent is similar to GUIContent, with the exception that it also
  72. * includes an optional params[] char list in the constructor to define shortcut keys (ex, CMD_CONTROL, K).
  73. */
  74. static readonly pb_TooltipContent _tooltip = new pb_TooltipContent
  75. (
  76. "Set Double-Sided",
  77. "Adds another face to the back of the selected faces."
  78. );
  79. /**
  80. * Determines if the action should be enabled or grayed out.
  81. */
  82. public override bool IsEnabled()
  83. {
  84. // `selection` is a helper property on pb_MenuAction that returns a pb_Object[] array from the current selection.
  85. return pb_Editor.instance != null &&
  86. selection != null &&
  87. selection.Length > 0 &&
  88. selection.Any(x => x.SelectedFaceCount > 0);
  89. }
  90. /**
  91. * Determines if the action should be loaded in the menu (ex, face actions shouldn't be shown when in vertex editing mode).
  92. */
  93. public override bool IsHidden()
  94. {
  95. return pb_Editor.instance == null ||
  96. pb_Editor.instance.editLevel != EditLevel.Geometry ||
  97. pb_Editor.instance.selectionMode != SelectMode.Face;
  98. }
  99. /**
  100. * Do the thing. Return a pb_ActionResult indicating the success/failure of action.
  101. */
  102. public override pb_ActionResult DoAction()
  103. {
  104. pbUndo.RecordObjects(selection, "Make Double-Sided Faces");
  105. foreach(pb_Object pb in selection)
  106. {
  107. pb_AppendDelete.DuplicateAndFlip(pb, pb.SelectedFaces);
  108. pb.ToMesh();
  109. pb.Refresh();
  110. pb.Optimize();
  111. }
  112. // This is necessary! Otherwise the pb_Editor will be working with caches from
  113. // outdated meshes and throw errors.
  114. pb_Editor.Refresh();
  115. return new pb_ActionResult(Status.Success, "Make Faces Double-Sided");
  116. }
  117. }
  118. }
  119. #endif