static private void DrawButton(PSDUI.Layer layer, GameObject parent) { Button temp = Resources.Load(PSDImporterConst.PREFAB_PATH_BUTTON, typeof(Button)) as Button; Button button = GameObject.Instantiate(temp) as Button; button.transform.parent = parent.transform; if (layer.images != null) { for (int imageIndex = 0; imageIndex < layer.images.Length; imageIndex++) { PSDUI.Image image = layer.images [imageIndex]; if (image.name.Contains("normal")) { if (image.imageSource == PSDUI.ImageSource.Custom) { string assetPath = baseDirectory + image.name + PSDImporterConst.PNG_SUFFIX; Sprite sprite = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite)) as Sprite; button.image.sprite = sprite; RectTransform rectTransform = button.GetComponent <RectTransform> (); rectTransform.sizeDelta = new Vector2(image.size.width, image.size.height); rectTransform.anchoredPosition = new Vector2(image.position.x, image.position.y); } } } } }
//------------------------------------------------------------------ //when it's a common psd, then move the asset to special folder //------------------------------------------------------------------ static private void MoveAsset(PSDUI.Layer layer, string baseDirectory) { if (layer.images != null) { string newPath = baseDirectory; if (layer.name == PSDImporterConst.IMAGE) { newPath = baseDirectory + PSDImporterConst.IMAGE + "/"; System.IO.Directory.CreateDirectory(newPath); } else if (layer.name == PSDImporterConst.NINE_SLICE) { newPath = baseDirectory + PSDImporterConst.NINE_SLICE + "/"; System.IO.Directory.CreateDirectory(newPath); } Debug.Log("creating new folder : " + newPath); AssetDatabase.Refresh(); for (int imageIndex = 0; imageIndex < layer.images.Length; imageIndex++) { // we need to fixup all images that were exported from PS PSDUI.Image image = layer.images [imageIndex]; if (image.imageSource == PSDUI.ImageSource.Custom) { string texturePathName = baseDirectory + layer.images [imageIndex].name + PSDImporterConst.PNG_SUFFIX; string targetPathName = newPath + layer.images [imageIndex].name + PSDImporterConst.PNG_SUFFIX; Debug.Log(texturePathName); Debug.Log(targetPathName); AssetDatabase.MoveAsset(texturePathName, targetPathName); } } } if (layer.layers != null) { for (int layerIndex = 0; layerIndex < layer.layers.Length; layerIndex++) { MoveAsset(layer.layers [layerIndex], baseDirectory); } } }
//-------------------------------------------------------------------------- // private methods //------------------------------------------------------------------------- static private void ImportLayer(PSDUI.Layer layer, string baseDirectory) { if (layer.images != null) { for (int imageIndex = 0; imageIndex < layer.images.Length; imageIndex++) { // we need to fixup all images that were exported from PS PSDUI.Image image = layer.images [imageIndex]; if (image.imageSource == PSDUI.ImageSource.Custom) { string texturePathName = baseDirectory + layer.images [imageIndex].name + PSDImporterConst.PNG_SUFFIX; // modify the importer settings TextureImporter textureImporter = AssetImporter.GetAtPath(texturePathName) as TextureImporter; textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Single; textureImporter.spritePackingTag = baseFilename; textureImporter.maxTextureSize = 2048; if (baseFilename.Contains("Common") && layer.name == PSDImporterConst.NINE_SLICE) //If Psd's name contains "Common", then it's Common type; { textureImporter.spriteBorder = new Vector4(3, 3, 3, 3); // Set Default Slice type Image's border to Vector4 (3, 3, 3, 3) } AssetDatabase.WriteImportSettingsIfDirty(texturePathName); AssetDatabase.ImportAsset(texturePathName); } } } if (layer.layers != null) { for (int layerIndex = 0; layerIndex < layer.layers.Length; layerIndex++) { ImportLayer(layer.layers [layerIndex], baseDirectory); } } }
static private void DrawNormalLayer(PSDUI.Layer layer, GameObject parent) { GameObject obj = new GameObject(layer.name); obj.transform.parent = parent.transform; if (layer.images != null) { for (int imageIndex = 0; imageIndex < layer.images.Length; imageIndex++) { PSDUI.Image image = layer.images [imageIndex]; if (image.imageSource == PSDUI.ImageSource.Custom) { Image pic = Resources.Load(PSDImporterConst.PREFAB_PATH_IMAGE, typeof(Image)) as Image; string assetPath = baseDirectory + image.name + PSDImporterConst.PNG_SUFFIX; Sprite sprite = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite)) as Sprite; if (sprite == null) { Debug.Log("loading asset at path: " + baseDirectory + image.name); } pic.sprite = sprite; Image myImage = GameObject.Instantiate(pic) as Image; myImage.name = image.name; myImage.transform.parent = obj.transform; RectTransform rectTransform = myImage.GetComponent <RectTransform> (); rectTransform.sizeDelta = new Vector2(image.size.width, image.size.height); rectTransform.anchoredPosition = new Vector2(image.position.x, image.position.y); } else if (image.imageSource == PSDUI.ImageSource.Common) { if (image.imageType == PSDUI.ImageType.Label) { Text text = Resources.Load(PSDImporterConst.PREFAB_PATH_TEXT, typeof(Text)) as Text; Text myText = GameObject.Instantiate(text) as Text; Debug.Log("Label Color : " + image.arguments [0]); // myText.color = image.arguments[0]; // myText.font = image.arguments[1]; Debug.Log("fontSize : " + image.arguments [2]); myText.fontSize = System.Convert.ToInt32(image.arguments [2]); myText.text = image.arguments [3]; myText.transform.parent = obj.transform; RectTransform rectTransform = myText.GetComponent <RectTransform> (); rectTransform.sizeDelta = new Vector2(image.size.width, image.size.height); rectTransform.anchoredPosition = new Vector2(image.position.x, image.position.y); } else if (image.imageType == PSDUI.ImageType.Texture) { Image pic = Resources.Load(PSDImporterConst.PREFAB_PATH_IMAGE, typeof(Image)) as Image; pic.sprite = null; Image myImage = GameObject.Instantiate(pic) as Image; myImage.name = image.name; myImage.transform.parent = obj.transform; RectTransform rectTransform = myImage.GetComponent <RectTransform> (); rectTransform.sizeDelta = new Vector2(image.size.width, image.size.height); rectTransform.anchoredPosition = new Vector2(image.position.x, image.position.y); } else { Image pic = Resources.Load(PSDImporterConst.PREFAB_PATH_IMAGE, typeof(Image)) as Image; string commonImagePath = PSDImporterConst.COMMON_BASE_FOLDER + image.name.Replace(".", "/") + PSDImporterConst.PNG_SUFFIX; Debug.Log("== CommonImagePath ====" + commonImagePath); Sprite sprite = AssetDatabase.LoadAssetAtPath(commonImagePath, typeof(Sprite)) as Sprite; pic.sprite = sprite; Image myImage = GameObject.Instantiate(pic) as Image; myImage.name = image.name; myImage.transform.parent = obj.transform; if (image.imageType == PSDUI.ImageType.SliceImage) { myImage.type = Image.Type.Sliced; } RectTransform rectTransform = myImage.GetComponent <RectTransform> (); rectTransform.sizeDelta = new Vector2(image.size.width, image.size.height); rectTransform.anchoredPosition = new Vector2(image.position.x, image.position.y); } } } } if (layer.layers != null) { for (int layerIndex = 0; layerIndex < layer.layers.Length; layerIndex++) { DrawLayer(layer.layers [layerIndex], obj); } } }