Пример #1
0
    // 初始化
    public static GameObject InitRectComponent(Children child, GameObject parentObj)
    {
        // 获取属性
        var propStr = child.options.property;
        var props   = propStr.Split('\r');
        var prop    = new Property();

        prop.DeserializeProperty(props);

        // 构建控件
        string     path        = PSDConst.GetPrefabPathByName("GameObject");
        GameObject temp        = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
        GameObject childObject = GameObject.Instantiate(temp) as GameObject;

        childObject.name = child.name;
        childObject.transform.SetParent(parentObj.transform);

        RectTransform rect = childObject.GetComponent <RectTransform>();


        RectTransform parentRect = parentObj.GetComponent <RectTransform>();

        // 设置锚点
        // Vector2 anchorP = new Vector2 (prop.anchors[0], prop.anchors[1]);
        Vector2 anchorP = prop.anchors;

        rect.anchorMin = PSDConst.PIVOTPOINT1;
        rect.anchorMax = PSDConst.PIVOTPOINT1;
        rect.pivot     = anchorP;

        // RectTransform 根据锚点计算 x,y位置
        if (anchorP == PSDConst.PIVOTPOINT0)
        {         // 中间
            float x = child.options.x + (child.options.width / 2);
            float y = child.options.y + (child.options.height / 2);
            rect.localPosition = new Vector3(x, -y);

            rect.sizeDelta = new Vector2(child.options.width, child.options.height);
        }
        else      //默认左上
        // else if (anchorP == PSDConst.PIVOTPOINT1)
        {         // 左上
            float x = child.options.x;
            float y = child.options.y;
            rect.localPosition = new Vector3(x, -y);

            rect.sizeDelta = new Vector2(child.options.width, child.options.height);
        }

        return(childObject);
    }
Пример #2
0
    public static void Json2Prefab(string outPath, string dirName)
    {
        string[] jsonPaths = Directory.GetFiles(outPath, "*.json", SearchOption.AllDirectories);


        List <Model> modelList = new List <Model>();

        foreach (string jsonPath in jsonPaths)
        {
            // if(!PSD2UI.isMD5Change(jsonPath))
            // {
            //  continue;
            // }
            StreamReader jsonReader = File.OpenText(jsonPath);
            if (jsonReader != null)
            {
                string jsonStr = jsonReader.ReadToEnd();
                Model  model   = JsonUtility.FromJson <Model> (jsonStr);
                modelList.Add(model);
            }
        }

        if (modelList.Count <= 0)
        {
            return;
        }

        PSD2UI.curBaseAssetsDir = outPath;

        for (var i = 0; i < modelList.Count; i++)
        {
            var model = modelList[i];

            for (var j = model.children.Length - 1; j >= 0; j--)
            {
                oneCopyImgToTmpPath(model.children[j], dirName);
            }
        }
        copyImgToTmpPathFromPNGExport(dirName);


        //打包临时文件的图集
        Debug.Log("######开始打图集######");
        string srcPath = PSD2UI.texturePackerTempPath + dirName;
        string tarPath = PSDConst.GUI_PATH + dirName + "/" + dirName + ".png";

        AtlasManager.InitAtlasForTextureP(srcPath, tarPath);

        Debug.Log("######图集打完,开始加载UI######");
        string path1 = PSDConst.GetPrefabPathByName("Canvas");
        Canvas temp1 = AssetDatabase.LoadAssetAtPath(path1, typeof(Canvas)) as Canvas;
        // 实例化显示prefab(要另外用个对象保存避免被释放)
        Canvas canvas = GameObject.Instantiate(temp1) as Canvas;


        for (var i = 0; i < modelList.Count; i++)
        {
            var        model   = modelList[i];
            string     path3   = PSDConst.GetPrefabPathByName("GameObject");
            GameObject temp    = AssetDatabase.LoadAssetAtPath(path3, typeof(GameObject)) as GameObject;
            GameObject gameobj = GameObject.Instantiate(temp) as GameObject;
            gameobj.name = model.name + "Prefab";
            gameobj.transform.SetParent(canvas.gameObject.transform, false);
            RectTransform rect = gameobj.GetComponent <RectTransform>();
            // 总预设锚点再左上角(父锚点,锚点)
            Vector2 anchorP = PSDConst.PIVOTPOINT1;
            rect.anchorMin = anchorP;
            rect.anchorMax = anchorP;
            rect.pivot     = anchorP;
            rect.offsetMin = new Vector2(0.0f, 0.0f);
            rect.offsetMax = new Vector2(0.0f, 0.0f);
            rect.sizeDelta = new Vector2(model.options.width, model.options.height);
            for (int j = model.children.Length - 1; j >= 0; j--)
            {
                initComponent(model.children[j], gameobj, dirName);
            }

            string prefabPath = PSDConst.GUI_PATH + dirName + "/" + model.name + "Prefab.prefab";
            Debug.Log("######创建预设:" + prefabPath + "######");
            PrefabUtility.CreatePrefab(prefabPath, gameobj, ReplacePrefabOptions.ReplaceNameBased);
        }


        SetAssetBundleName();
        BuildAB.BuildAllAssetBundles();

        AssetDatabase.Refresh();
    }
Пример #3
0
    // 初始化
    public static GameObject InitImageComponent(Children child, GameObject parentObj, string fileName)
    {
        // 获取属性
        var propertys = child.options.property.Split('\r');
        var prop      = new Property();

        prop.DeserializeProperty(propertys);



        bool   isCommon    = false;    // 是否使用公共图集
        string file_result = "";


        RectTransform parentRect = parentObj.GetComponent <RectTransform> ();

        /**
         *      外部图片:outpic
         *      公共图片:common
         *      面板独有:剩下的
         *
         * 1.先判断是否为外部图片,预留空go,单独图集
         * 2.每次查询图集先从公共图集里查询
         * 3.剩下的加入面板独有图集
         */

        // 构建图片控件
        string path = PSDConst.GetPrefabPathByName("Image");

        GameObject temp       = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
        GameObject gameObject = GameObject.Instantiate(temp) as GameObject;

        gameObject.name = child.name;
        gameObject.transform.SetParent(parentObj.transform, false);

        Image imgComponent = gameObject.GetComponent <Image>();

        // 设置图片控件位移及锚点信息
        RectTransform rectTransform = gameObject.GetComponent <RectTransform>();

        // 设置锚点
        // Vector2 anchorP = new Vector2 (prop.anchors[0], prop.anchors[1]);
        // 默认中心的点
        Vector2 anchorP = PSDConst.PIVOTPOINT0;

        rectTransform.anchorMin = PSDConst.PIVOTPOINT1;
        rectTransform.anchorMax = PSDConst.PIVOTPOINT1;
        rectTransform.pivot     = anchorP;

        // RectTransform 根据锚点计算 x,y位置
        if (anchorP == PSDConst.PIVOTPOINT0)
        {         // 中间
            float x = child.options.x + (child.options.width / 2);
            float y = child.options.y + (child.options.height / 2);
            rectTransform.localPosition = new Vector3(x, -y);

            rectTransform.sizeDelta = new Vector2(child.options.width, child.options.height);
        }
        else      //默认左上
        // else if (anchorP == PSDConst.PIVOTPOINT1)
        {         // 左上
            float x = child.options.x;
            float y = child.options.y;
            rectTransform.localPosition = new Vector3(x, -y);

            rectTransform.sizeDelta = new Vector2(child.options.width, child.options.height);
        }

        Sprite   sprite   = null;
        Material material = null;

        string[] link_split = child.options.link.Split('#');
        string   rootPath   = (link_split.Length > 0) ? (link_split[0]) : "";
        string   imgPath    = (link_split.Length > 0) ? link_split[link_split.Length - 1] : "";
        string   prefix     = (imgPath.Split('/').Length > 1) ? imgPath.Split('/')[0] : "";
        string   imgName    = (imgPath.Split('/').Length > 0) ? imgPath.Split('/')[imgPath.Split('/').Length - 1] : "";

        imgName = Path.GetFileNameWithoutExtension(imgName);
        string atlasName = (prefix != "") ? (prefix + "-" + imgName) : (imgName);

        // 获取图集名称,先从公共图集中查询
        if (File.Exists(PSDConst.ATLAS_PATH_COMMON))
        {
            sprite = AtlasManager.getSpriteForTextureP(PSDConst.ATLAS_PATH_COMMON, atlasName);
            // 加载材质
            material = AtlasManager.getMaterForAtlasPath(PSDConst.ATLAS_PATH_COMMON);
            if (sprite != null)
            {
                isCommon = true;
            }
        }
        Debug.Log("$$$imgPath: " + imgPath);


        //Debug.Log ("$$$src:"+baseAssetsDir + rootPath + '/');
        //Debug.Log ("$$$tar:"+PSDConst.GUI_PATH + rootPath + '/' + rootPath + PSDConst.PNG_SUFFIX);
        if (sprite == null)
        {
            isCommon = false;
            string tarPath = PSDConst.GUI_PATH + fileName + '/' + fileName + ".png";

            file_result = fileName;
            if (fileName.Contains("Item"))
            {
                string[] file_split = fileName.Split('_');
                for (int i = 0; i < file_split.Length; i++)
                {
                    if (file_split[i].Contains("Item"))
                    {
                        file_result = fileName.Replace(("_" + file_split[i]), (""));
                        tarPath     = PSDConst.GUI_PATH + file_result + '-' + file_result + ".png";
                    }
                }
            }
            if (File.Exists(tarPath))
            {
                // Debug.Log ("!!!!:"+imgPath.Split('/').Length);
                Debug.Log("###tarPath:" + tarPath + ",atlasName=" + atlasName);
                sprite = AtlasManager.getSpriteForTextureP(tarPath, atlasName);
            }
            else
            {
                Debug.Log("File is not Exists:" + tarPath);
            }
            // 加载材质
            material = AtlasManager.getMaterForAtlasPath(tarPath);
        }

        // 设置控件背景颜色(包含透明度)
        var colorR = 1f;
        var colorG = 1f;
        var colorB = 1f;
        var alpha  = child.options.opacity / 255f;

        if (child.options.property != "")
        {
            colorR = prop.colorR / 255f;
            colorG = prop.colorG / 255f;
            colorB = prop.colorB / 255f;
            alpha  = prop.opacity / 255f;
        }
        if (prop.isOutpic > 0)
        {
            material = null;
            alpha    = 0;
        }
        imgComponent.color    = new Color(colorR, colorG, colorB, alpha);
        imgComponent.sprite   = sprite;
        imgComponent.material = material;

        if (prop.isOutpic > 0)   // 外部图片
        {
            if (prop.isOutpic == 2)
            {
                return(imgComponent.gameObject);
            }
            // 外部图片统一放到"out/*/*.png"  child_tvaabb  child_tvaabbBoy
            string outSrc     = PSD2UI.curBaseAssetsDir + rootPath + "/" + imgPath;
            string outImg     = PSDConst.OUT_PATH + "/" + rootPath + "-" + imgPath;
            string saveDir    = Directory.GetParent(outImg).FullName;
            string outMetaSrc = outSrc + ".meta";
            string outMetaImg = outImg + ".meta";
            // prefab路径只能使用相对路径!!!
            if (!Directory.Exists(saveDir))
            {
                Directory.CreateDirectory(saveDir);
            }
            if (!File.Exists(outImg))
            {
                // 对原图先转换成精灵图片后拷贝
                //Sprchildanager.conversion(outSrc);
                File.Copy(outSrc, outImg);
                File.Copy(outMetaSrc, outMetaImg);
            }
            else
            {
                File.Delete(outImg);
                string metaTxt = outImg + ".meta";
                if (File.Exists(metaTxt))
                {
                    File.Delete(metaTxt);
                }
                File.Copy(outSrc, outImg);
                File.Copy(outMetaSrc, outMetaImg);
            }
            return(imgComponent.gameObject);
        }
        // 是否设置九宫格
        if (child.options.isScaleImage == true)
        {
            // Texture图集九宫格切图
            TextureImporter importer = null;
            if (isCommon)
            {
                importer            = AssetImporter.GetAtPath(PSDConst.UI_COMMON_PATH) as TextureImporter;
                importer.isReadable = true;
            }
            else
            {
                importer            = AssetImporter.GetAtPath(PSDConst.GUI_PATH + file_result + '/' + file_result + ".png") as TextureImporter;
                importer.isReadable = true;
            }

            SpriteMetaData[] datas = importer.spritesheet;
            for (int i = 0; i < datas.Length; i++)
            {
                //Debug.Log (datas[i].name + "<<<>>>" + atlasName);
                if (datas[i].name == atlasName)
                {
                    //Debug.Log ("9sclice");
                    datas [i].border = new Vector4(child.options.left, child.options.bottom, child.options.right, child.options.top);
                }
            }
            importer.spritesheet      = datas;
            imgComponent.type         = UnityEngine.UI.Image.Type.Sliced;
            importer.textureType      = TextureImporterType.Sprite;
            importer.spriteImportMode = SpriteImportMode.Multiple;
            importer.mipmapEnabled    = false;
            importer.SaveAndReimport();
        }

        return(imgComponent.gameObject);
    }
Пример #4
0
    public static GameObject InitTextFieldComponent(Children child, GameObject parentObj)
    {
        // 获取属性
        var propStr = child.options.property;
        var props   = propStr.Split('\r');
        var prop    = new Property();

        prop.DeserializeProperty(props);

        string     path       = PSDConst.GetPrefabPathByName("InputField");
        GameObject temp       = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
        GameObject gameObject = GameObject.Instantiate(temp) as GameObject;

        gameObject.name = child.name;
        InputField fieldComponent = gameObject.GetComponent <InputField>();

        Text textComponent = fieldComponent.textComponent;

        RectTransform textRectTransform = gameObject.GetComponent <RectTransform>();

        RectTransform rectTransform = fieldComponent.GetComponent <RectTransform>();
        RectTransform parentRect    = parentObj.GetComponent <RectTransform>();

        // 如果有额外属性,先根据额外属性设置控件
        if ((prop.scaleX > 0f) || (prop.scaleY > 0f))
        {
            rectTransform.localScale     = new Vector3(prop.scaleX, prop.scaleY, 1f);
            textRectTransform.localScale = new Vector3(prop.scaleX, prop.scaleY, 1f);
        }
        if ((prop.colorR < 255) || (prop.colorG < 255) || (prop.colorB < 255))
        {
            textComponent.color = new Color(prop.colorR, prop.colorG, prop.colorB);
        }

        // 构建控件
        if (child.options.Leading == 0)
        {
            //修正 ps默认行间距是 字号的1.75倍
            child.options.Leading = 1f * child.options.fontSize;
        }
        // Debug.Log("input文本内容 = "+child.options.text);
        textComponent.lineSpacing = child.options.Leading / child.options.fontSize;
        string a = child.options.text.Replace("\r", "\n");

        // Debug.Log("input修正后内容 = "+a);
        fieldComponent.text    = a;
        textComponent.fontSize = child.options.fontSize;
        // textComponent.verticalOverflow = UnityEngine.VerticalWrapMode.Truncate;
        //多行
        // if (!child.options.singleLine)
        // {
        // fieldComponent.lineType = UnityEngine.UI.InputField.LineType.MultiLineNewline;
        // textComponent.horizontalOverflow = UnityEngine.HorizontalWrapMode.Wrap;
        // }

        // 居中方式
        switch (child.options.Justification)
        {
        case 0:         // 左
            textComponent.alignment = TextAnchor.UpperLeft;
            break;

        case 1:         // 右
            textComponent.alignment = TextAnchor.UpperRight;
            break;

        case 2:         // 中
            textComponent.alignment = TextAnchor.UpperCenter;
            break;
        }


        // 默认勾上,按几何对齐,否则距离上边缘会有2个像素偏差
        //勾上 输入英文和_时,会出问题
        // textComponent.alignByGeometry = true;

        var colorR = child.options.colorR / 255f;
        var colorG = child.options.colorG / 255f;
        var colorB = child.options.colorB / 255f;
        var alpha  = child.options.opacity / 255f;

        textComponent.color = new Color(colorR, colorG, colorB, alpha);

        var font = AssetDatabase.LoadAssetAtPath(PSDConst.FONT_PATH, typeof(Font)) as Font;

        textComponent.font = font;

        //设置锚点(父锚点默认左上跟psd一致,锚点读psd设置)
        //文本中心点pivot只能左上角,因为文字是从中心点开始往右输出的
        Vector2 anchorP = PSDConst.PIVOTPOINT1;

        rectTransform.anchorMin = PSDConst.PIVOTPOINT1;
        rectTransform.anchorMax = PSDConst.PIVOTPOINT1;
        rectTransform.pivot     = anchorP;

        textRectTransform.anchorMin = PSDConst.PIVOTPOINT1;
        textRectTransform.anchorMax = PSDConst.PIVOTPOINT1;
        textRectTransform.pivot     = PSDConst.PIVOTPOINT1;


        // RectTransform 根据锚点计算 x,y位置
        if (anchorP == PSDConst.PIVOTPOINT0)
        {         //中间
            float x = child.options.x + (child.options.width / 2);
            float y = child.options.y + (child.options.height / 4);
            rectTransform.localPosition = new Vector3(x, -y);

            float width  = child.options.width;
            float height = (child.options.height < (float)(child.options.fontSize)) ? (float)(child.options.fontSize) : child.options.height;
            rectTransform.sizeDelta     = new Vector2(width, height);
            textRectTransform.sizeDelta = new Vector2(width, height);
        }
        else      //默认左上
        // else if (anchorP == PSDConst.PIVOTPOINT1)
        {         // 左上
            float x = child.options.x;
            float y = child.options.y - PSDConst.TEXT_FIX_Y;
            rectTransform.localPosition = new Vector3(x, -y);

            float width  = child.options.width;
            float height = child.options.height;
            height = (height < (float)(child.options.fontSize)) ? (float)(child.options.fontSize) : height;
            rectTransform.sizeDelta     = new Vector2(width, height + PSDConst.TEXTFIELD_FIX_H);
            textRectTransform.sizeDelta = new Vector2(width, height + PSDConst.TEXTFIELD_FIX_H);
        }

        return(fieldComponent.gameObject);
    }
Пример #5
0
    public static GameObject InitComponent(Children child, GameObject parentObj)
    {
        // 获取属性
        var propStr = child.options.property;
        var props   = propStr.Split('\r');
        var prop    = new Property();

        prop.DeserializeProperty(props);

        /** ScrollView的组成
         *
         * ScrollRect 滚动范围
         *      --Viewport 可视范围
         *      --Scrollbar 垂直水平滚动条(可有可无)
         */
        // 控件相关获取
        string     path        = PSDConst.GetPrefabPathByName("ScrollView");
        GameObject temp        = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
        GameObject childObject = GameObject.Instantiate(temp) as GameObject;

        childObject.name = child.name;
        childObject.transform.SetParent(parentObj.transform, false);
        RectTransform rectTransform = childObject.GetComponent <RectTransform>();

        RectTransform parentRect = parentObj.GetComponent <RectTransform>();

//		 如果有额外属性,先根据额外属性设置控件
        if ((prop.scaleX > 0f) || (prop.scaleY > 0f))
        {
            rectTransform.localScale = new Vector3(prop.scaleX, prop.scaleY);
        }

        // 设置锚点
        // Vector2 anchorP = new Vector2 (prop.anchors[0], prop.anchors[1]);
        Vector2 anchorP = prop.anchors;

        rectTransform.anchorMin = PSDConst.PIVOTPOINT1;
        rectTransform.anchorMax = PSDConst.PIVOTPOINT1;
        rectTransform.pivot     = anchorP;

        // RectTransform 根据锚点计算 x,y位置
        if (anchorP == PSDConst.PIVOTPOINT0)
        {         // 中间
            float x = child.options.x + (child.options.width / 2);
            float y = child.options.y + (child.options.height / 2);
            rectTransform.localPosition = new Vector3(x, -y);

            rectTransform.sizeDelta = new Vector2(child.options.width, child.options.height);
            RectTransform maskTrans = rectTransform.Find("Viewport") as RectTransform;
            maskTrans.sizeDelta = new Vector2(child.options.width, child.options.height);
        }
        else      //默认左上
        // else if (anchorP == PSDConst.PIVOTPOINT1)
        {         // 左上
            float x = child.options.x;
            float y = child.options.y;
            rectTransform.localPosition = new Vector3(x, -y);

            rectTransform.sizeDelta = new Vector2(child.options.width, child.options.height);
            RectTransform maskTrans = rectTransform.Find("Viewport") as RectTransform;
            maskTrans.sizeDelta = new Vector2(child.options.width, child.options.height);
        }


        return(childObject);
    }