コード例 #1
0
ファイル: LayoutNodeManager.cs プロジェクト: damon0609/PGUI
        /// <summary>
        /// 创建节点区域
        /// </summary>
        void CreateLayoutNode()
        {
            GUILayout.Space(5);
            EditorGUILayout.LabelField("Create LayoutNode", EditorStyles.boldLabel);
            EditorGUILayout.BeginVertical("box");

            layoutName = EditorGUILayout.TextField("LayoutName", layoutName);
            isShow     = EditorGUILayout.Toggle("isShow", !isShow);
            fillType   = (LayoutNode.FillType)EditorGUILayout.EnumPopup("FillType", fillType);

            if (fillType == LayoutNode.FillType.FillParent)
            {
                GUI.enabled = false;
            }

            EditorGUILayout.BeginHorizontal();
            pos      = EditorGUILayout.Vector2Field("Pos", pos);
            size     = EditorGUILayout.Vector2Field("Size", size);
            rotation = EditorGUILayout.Vector3Field("Rotation", rotation);
            EditorGUILayout.EndHorizontal();
            GUI.enabled = true;
            GUILayout.Space(2);
            if (GUILayout.Button("Create LayoutNode"))
            {
                //EditorApplication.ExecuteMenuItem("PUI/Layout/LayoutNode");
                LayoutNode layoutNode = PUITool.CreateGameObject <LayoutNode>(layoutName);
                layoutNode.rectTransform = layoutNode.gameObject.GetComponent <RectTransform>();
                layoutNode.isShow        = isShow;
                layoutNode.fillType      = fillType;
                layoutNode.OnSetRectTransform(pos, size, rotation, fillType);
                layoutName = "LayoutNode";
            }
            EditorGUILayout.EndVertical();
        }
コード例 #2
0
        /// <summary>
        /// 在Canvas下创建一个带有指定组件的对象
        /// </summary>
        /// <typeparam name="T">添加指定组件</typeparam>
        /// <param name="name">游戏对象名称</param>
        /// <returns></returns>
        public static T CreateGameObject <T>(string name) where T : UnityEngine.Component
        {
            T          t        = default(T);
            GameObject activeGo = Selection.activeGameObject;
            Transform  parent   = null;

            if (activeGo != null)
            {
                Transform temp = activeGo.transform;
                while (temp != null)
                {
                    if (temp.GetComponent <Canvas>() != null)
                    {
                        parent = activeGo.transform;
                        break;
                    }
                    temp = temp.transform.parent;
                }
                if (parent == null)
                {
                    Debug.LogError("the selected obj did not hava the canvas component");
                    return(t);
                }
            }
            else
            {
                Canvas canvas = PUITool.FindCanvas();
                parent = canvas.transform;
            }

            GameObject go = new GameObject(name);

            t = go.AddComponent <T>();
            go.transform.parent        = parent;
            go.transform.localPosition = Vector3.zero;
            go.transform.rotation      = Quaternion.identity;
            go.transform.localScale    = Vector3.one;

            Undo.RegisterCreatedObjectUndo(go, "Create Label");
            Selection.activeGameObject = go;

            return(t);
        }