MenuItemYIUICommon.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace YIUIFramework.Editor
  4. {
  5. public static class MenuItemYIUICommon
  6. {
  7. [MenuItem("Assets/YIUI/Create UICommon", false, 3)]
  8. static void CreateYIUICommonByFolder()
  9. {
  10. var activeObject = Selection.activeObject as DefaultAsset;
  11. if (activeObject == null)
  12. {
  13. UnityTipsHelper.ShowError($"请在路径 {YIUIConstHelper.Const.UIProjectResPath}/xxx/{YIUIConstHelper.Const.UIPrefabs} 下右键创建");
  14. return;
  15. }
  16. var path = AssetDatabase.GetAssetPath(Selection.activeObject);
  17. if (!path.Contains(YIUIConstHelper.Const.UIProjectResPath))
  18. {
  19. UnityTipsHelper.ShowError($"请在路径 {YIUIConstHelper.Const.UIProjectResPath}/xxx/{YIUIConstHelper.Const.UIPrefabs} 下右键创建");
  20. return;
  21. }
  22. var saveName = $"{YIUIConstHelper.Const.UIProjectName}{YIUIConstHelper.Const.UICommonName}";
  23. var savePath = $"{path}/{saveName}.prefab";
  24. if (AssetDatabase.LoadAssetAtPath(savePath, typeof(Object)) != null)
  25. {
  26. UnityTipsHelper.ShowError($"已存在 请先重命名 {saveName}");
  27. return;
  28. }
  29. var createCommon = CreateYIUICommon();
  30. PrefabUtility.SaveAsPrefabAsset(createCommon, savePath);
  31. Object.DestroyImmediate(createCommon);
  32. UIMenuItemHelper.SelectAssetAtPath(savePath);
  33. }
  34. [MenuItem("GameObject/YIUI/Create UICommon", false, 3)]
  35. static void CreateYIUICommonByGameObject()
  36. {
  37. var activeObject = Selection.activeObject as GameObject;
  38. if (activeObject == null)
  39. {
  40. UnityTipsHelper.ShowError($"请选择一个目标");
  41. return;
  42. }
  43. Selection.activeObject = CreateYIUICommon(activeObject);
  44. }
  45. public static GameObject CreateYIUICommon(GameObject activeObject = null)
  46. {
  47. var commonObject = new GameObject();
  48. commonObject.name = $"{YIUIConstHelper.Const.UIProjectName}{YIUIConstHelper.Const.UICommonName}";
  49. var viewRect = commonObject.GetOrAddComponent<RectTransform>();
  50. commonObject.GetOrAddComponent<CanvasRenderer>();
  51. var cdeTable = commonObject.GetOrAddComponent<UIBindCDETable>();
  52. cdeTable.UICodeType = EUICodeType.Common;
  53. if (activeObject != null)
  54. {
  55. viewRect.SetParent(activeObject.transform, false);
  56. EditorUtility.SetDirty(activeObject);
  57. }
  58. commonObject.SetLayerRecursively(LayerMask.NameToLayer("UI"));
  59. return commonObject;
  60. }
  61. }
  62. }