@@ -56,17 +56,10 @@ public static class UGUICodeGenerator
5656 [ MenuItem ( "GameObject/UI/Generate UGUI Code(生成UGUI代码)" , false , 1 ) ]
5757 static void Code ( )
5858 {
59- if ( Selection . activeObject )
59+ if ( Selection . activeObject is GameObject selectedObject )
6060 {
61- var selectedObject = ( GameObject ) Selection . activeObject ;
62- if ( selectedObject )
63- {
64- if ( PrefabUtility . GetPrefabAssetType ( selectedObject ) != PrefabAssetType . NotAPrefab )
65- {
66- Generate ( selectedObject ) ;
67- return ;
68- }
69- }
61+ Generate ( selectedObject ) ;
62+ return ;
7063 }
7164
7265 Debug . LogError ( "请选择一个有效的UGUI预制体进行操作" ) ;
@@ -83,11 +76,29 @@ static void Code()
8376 /// <param name="selectedObject">选中的游戏对象</param>
8477 public static void Generate ( GameObject selectedObject )
8578 {
86- if ( PrefabUtility . GetPrefabAssetType ( selectedObject ) != PrefabAssetType . NotAPrefab )
79+ if ( selectedObject == null )
80+ {
81+ Debug . LogError ( "请选择一个有效的UGUI预制体进行操作" ) ;
82+ return ;
83+ }
84+
85+ if ( PrefabUtility . GetPrefabAssetType ( selectedObject ) == PrefabAssetType . NotAPrefab )
8786 {
8887 Debug . LogError ( "请选择一个有效的UGUI预制体进行操作" ) ;
8988 return ;
9089 }
90+
91+ if ( selectedObject . GetComponent < RectTransform > ( ) == null )
92+ {
93+ Debug . LogError ( "请选择包含RectTransform组件的UI根节点预制体进行操作" ) ;
94+ return ;
95+ }
96+
97+ if ( selectedObject . transform . parent != null )
98+ {
99+ Debug . LogError ( "请选择UI对象根节点进行操作,不能选择子节点" ) ;
100+ return ;
101+ }
91102
92103 var assetPath = AssetDatabase . GetAssetPath ( selectedObject ) ;
93104
@@ -519,4 +530,4 @@ private static void CreateFoldersIfNotExist(string path)
519530 }
520531 }
521532 }
522- }
533+ }
0 commit comments