Esempio n. 1
0
    /// <summary>
    /// 显示控件
    /// </summary>
    private void OnGUI()
    {
        EditorGUILayout.LabelField("说明: ", "在Hierarchy创建Canvas,并设置默认参数");

        EditorGUILayout.BeginHorizontal();
        {
            theWindowName = EditorGUILayout.TextField("View-Window名字: ", theWindowName);
        }
        EditorGUILayout.EndHorizontal();

        if (GUILayout.Button("创  建"))
        {
            KGameSetting kGameSetting = KGameSetting.LoadKGameSetting();
            if (kGameSetting == null)
            {
                EditorUtility.DisplayDialog("错误", "游戏开发设定未创建,请点击菜单 \"KCore/游戏开发设定\" ", "确定");
                return;
            }

            string finalWindowName = theWindowName.Trim();

            if (string.IsNullOrEmpty(finalWindowName))
            {
                EditorUtility.DisplayDialog("错误", "请输入一个合法的Window名字", "确定");
                return;
            }

            //判断首字母大写
            char firstCH = finalWindowName[0];
            if (firstCH < 'A' || firstCH > 'Z')
            {
                EditorUtility.DisplayDialog("错误", "Window名字需要首字母大写", "确定");
                return;
            }

            if (EditorApplication.ExecuteMenuItem("GameObject/UI/Canvas"))
            {
                GameObject insertControl = Selection.activeGameObject;
                insertControl.name = finalWindowName;

                CanvasScaler cs = insertControl.GetComponent <CanvasScaler>();
                cs.uiScaleMode         = CanvasScaler.ScaleMode.ScaleWithScreenSize;
                cs.referenceResolution = new Vector2(kGameSetting.resolutionWidth, kGameSetting.resolutionHeight);
                cs.screenMatchMode     = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
                cs.matchWidthOrHeight  = 0;

                // 添加一个默认的空节点,用来生成代码
                GameObject codeGenDummy = new GameObject("CodeGenDummy");
                codeGenDummy.transform.SetParent(insertControl.transform);
                codeGenDummy.transform.localPosition = Vector3.zero;

                codeGenDummy.AddComponent <KUIPropertyDefine>();
            }
        }
    }
    private static void CreateKGameSettingAsset()
    {
        KGameSetting kGameSetting = KGameSetting.LoadKGameSetting();

        if (kGameSetting == null)
        {
            // 没有则先创建
            kGameSetting = ScriptableObject.CreateInstance <KGameSetting>();
            AssetDatabase.CreateAsset(kGameSetting, "Assets/Resources/KGameSetting.asset");
            AssetDatabase.Refresh();

            // 需要选中的是Resources下的.asset文件
            kGameSetting = KGameSetting.LoadKGameSetting();
        }

        Selection.activeObject = kGameSetting;
    }