HelperInfo.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using System.Collections.Generic;
  9. using System.Text.RegularExpressions;
  10. using UnityEditor;
  11. using UnityEngine;
  12. namespace UnityGameFramework.Editor
  13. {
  14. internal sealed class HelperInfo<T> where T : MonoBehaviour
  15. {
  16. private const string CustomOptionName = "<Custom>";
  17. private readonly string m_Name;
  18. private SerializedProperty m_HelperTypeName;
  19. private SerializedProperty m_CustomHelper;
  20. private string[] m_HelperTypeNames;
  21. private int m_HelperTypeNameIndex;
  22. public HelperInfo(string name)
  23. {
  24. m_Name = name;
  25. m_HelperTypeName = null;
  26. m_CustomHelper = null;
  27. m_HelperTypeNames = null;
  28. m_HelperTypeNameIndex = 0;
  29. }
  30. public void Init(SerializedObject serializedObject)
  31. {
  32. m_HelperTypeName = serializedObject.FindProperty(Utility.Text.Format("m_{0}HelperTypeName", m_Name));
  33. m_CustomHelper = serializedObject.FindProperty(Utility.Text.Format("m_Custom{0}Helper", m_Name));
  34. }
  35. public void Draw()
  36. {
  37. string displayName = FieldNameForDisplay(m_Name);
  38. int selectedIndex = EditorGUILayout.Popup(Utility.Text.Format("{0} Helper", displayName), m_HelperTypeNameIndex, m_HelperTypeNames);
  39. if (selectedIndex != m_HelperTypeNameIndex)
  40. {
  41. m_HelperTypeNameIndex = selectedIndex;
  42. m_HelperTypeName.stringValue = selectedIndex <= 0 ? null : m_HelperTypeNames[selectedIndex];
  43. }
  44. if (m_HelperTypeNameIndex <= 0)
  45. {
  46. EditorGUILayout.PropertyField(m_CustomHelper);
  47. if (m_CustomHelper.objectReferenceValue == null)
  48. {
  49. EditorGUILayout.HelpBox(Utility.Text.Format("You must set Custom {0} Helper.", displayName), MessageType.Error);
  50. }
  51. }
  52. }
  53. public void Refresh()
  54. {
  55. List<string> helperTypeNameList = new List<string>
  56. {
  57. CustomOptionName
  58. };
  59. helperTypeNameList.AddRange(Type.GetRuntimeTypeNames(typeof(T)));
  60. m_HelperTypeNames = helperTypeNameList.ToArray();
  61. m_HelperTypeNameIndex = 0;
  62. if (!string.IsNullOrEmpty(m_HelperTypeName.stringValue))
  63. {
  64. m_HelperTypeNameIndex = helperTypeNameList.IndexOf(m_HelperTypeName.stringValue);
  65. if (m_HelperTypeNameIndex <= 0)
  66. {
  67. m_HelperTypeNameIndex = 0;
  68. m_HelperTypeName.stringValue = null;
  69. }
  70. }
  71. }
  72. private string FieldNameForDisplay(string fieldName)
  73. {
  74. if (string.IsNullOrEmpty(fieldName))
  75. {
  76. return string.Empty;
  77. }
  78. string str = Regex.Replace(fieldName, @"^m_", string.Empty);
  79. str = Regex.Replace(str, @"((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))", @" $1").TrimStart();
  80. return str;
  81. }
  82. }
  83. }