SurfaceManagementWindow.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace HQFPSWeapons
  4. {
  5. public class SurfaceManagementWindow : EditorWindow
  6. {
  7. private SurfaceInfo[] m_Surfaces = null;
  8. private string[] m_SurfaceNames = null;
  9. private int m_SelectedSurface = 0;
  10. private Vector2 m_ScrollPos = Vector2.zero;
  11. private UnityEditor.Editor m_CachedSurfaceInspector;
  12. [MenuItem("Tools/HQ FPS Weapons Pack/Surfaces...", false, 7)]
  13. public static void Init()
  14. {
  15. GetWindow<SurfaceManagementWindow>(true, "Surfaces");
  16. }
  17. private void OnEnable()
  18. {
  19. LoadSurfaceAssets();
  20. if(m_Surfaces != null)
  21. CreateInspectorForSelectedSurface();
  22. }
  23. private void OnProjectChange()
  24. {
  25. LoadSurfaceAssets();
  26. }
  27. private void OnGUI()
  28. {
  29. if(m_Surfaces != null)
  30. {
  31. int previousSelectedSurf = m_SelectedSurface;
  32. m_SelectedSurface = GUILayout.Toolbar(m_SelectedSurface, m_SurfaceNames);
  33. if(m_SelectedSurface != previousSelectedSurf)
  34. CreateInspectorForSelectedSurface();
  35. // GUI
  36. EditorGUILayout.Space();
  37. m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos);
  38. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  39. EditorGUILayout.LabelField(m_SurfaceNames[m_SelectedSurface], EditorGUICustom.TitleLabel);
  40. m_CachedSurfaceInspector.DrawDefaultInspector();
  41. EditorGUILayout.EndVertical();
  42. EditorGUILayout.EndScrollView();
  43. }
  44. }
  45. private void LoadSurfaceAssets()
  46. {
  47. m_Surfaces = null;
  48. m_SurfaceNames = null;
  49. string[] surfGUIDs = AssetDatabase.FindAssets("t:SurfaceInfo");
  50. if(surfGUIDs.Length > 0)
  51. {
  52. m_Surfaces = new SurfaceInfo[surfGUIDs.Length];
  53. m_SurfaceNames = new string[surfGUIDs.Length];
  54. for(int i = 0;i < surfGUIDs.Length;i++)
  55. {
  56. m_Surfaces[i] = AssetDatabase.LoadAssetAtPath<SurfaceInfo>(AssetDatabase.GUIDToAssetPath(surfGUIDs[i]));
  57. m_SurfaceNames[i] = m_Surfaces[i].name.DoUnityLikeNameFormat();
  58. }
  59. }
  60. }
  61. private void CreateInspectorForSelectedSurface()
  62. {
  63. UnityEditor.Editor.CreateCachedEditor(m_Surfaces[m_SelectedSurface], null, ref m_CachedSurfaceInspector);
  64. }
  65. }
  66. }