Another Workflow from Phtoshop to Unity

最近我们需要把一些Photoshop做的2D素材导入Unity。如果用传统方式一张一张的导,然后在Unity里再拼起来的话,会非常费时费力。好在这个过程是可以自动化的。

一种方法是在Unity端全部解决。SwingSwingSubmarine 的 Ben 写的 From Photoshop to Unity 描述了这种方法,使用了一个第三方的可以处理Photoshop文件的Library:PsdPlugin. 这种方法的问题是项目文件夹下需要保存psd文件,然后psd文件可能比较大,版本控制会有点麻烦。

Unity官方博客曾经发过一套方法: A good workflow to smoothly import 2D content into Unity, Part I 和 Part II,作者是 Brett Bibby。 这个方法中,输出图片素材由Photoshop完成,导入和重建场景由Unity完成,这不但是一个更自然的方法,也是一个更模块化的方法,谁也不用知道对方领域里的东西。但是原版方法要求事先合成一个图层才能输出。

所以这篇博客描述一种尽量符合美术工作习惯,能够从原版工作文档直接输出的方法。

Recently we need to transfer a large amount of 2D assets from Photoshop to Unity, then reproduce the positions etc. which is already in Photoshop. It would cost a lot of of time if we do this manually, fortunately we don’t have to, because this process can be automated.

A recent approach is From Photoshop to Unity written by Ben of SwingSwingSubmarine. It is a pure Unity-side solution where examine Photoshop file (psd) is also completed inside Unity via a third-party library PsdPlugin. Thus the project have to keep large psd files within Unity’s folder and caused some inconvenience for version control.

Another approach is: A good workflow to smoothly import 2D content into Unity, Part I and Part II by Brett Bibby. The images are exported inside Photoshop via Adobe Extend scripts, their positions are recorded in a xml file, then use a customized Unity Editor script to import these assets. This seems to be the most flexible and robust process. However this script is layer-based which still requires the artist to combine layers before export.

The process should take place on a ‘working psd’ which doesn’t require the artist to manually change anything before exporting. So here a improved version is described in this blog post.


流程 The Process

Photoshop文件
The Photoshop document to export

2D素材导入Unity
2D Assets imported into Unity, hierarchy is reproduced.

导入生成的xml
Create a new object in the Hierarchy by importing the xml file exported from Photoshop.

导入完成
Imported Object in Unity Scene


Notes

Performance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Do not duplicate the whole doc
// Create an empty doc and copy export content over is much faster
var doc = app.documents.add(
_dupDoc.width,
_dupDoc.height,
_dupDoc.resolution,
artLayer.name,
NewDocumentMode.RGB,
DocumentFill.TRANSPARENT,
_dupDoc.pixelAspectRatio,
_dupDoc.bitsPerChannel,
//_dupDoc.colorProfileName
);

app.activeDocument = _dupDoc;
exportLayer.duplicate (doc);

Adobe Photoshop layer bounds:

1
2
3
4
5
6
7
8
9
// CHART - Adobe Photoshop layer bounds:
//
// (bounds[0],bounds[1])
// *--------
// | |
// | |
// --------*
// (bounds[2],bounds[3])
//

Adobe Extend Script UI:

1
2
// decode the file path to make sure it (Chinese) displays correctly
pathPanel.pathText.text = decodeURI(_options.expFolderPath);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// buttons become useless while the script is running
/*
var cancelButton = _proPlet.add("button", undefined, "Cancel");

cancelButton.onClick = function () {
stopExcution();
_proPlet.close();
return 'cancel';
}
*/

// use the 'x' instead
_proPlet.onClose = function () {
stopExcution();
}

// Extend Script UI is not reliable in Photoshop, call WaitForRedraw() once after palette is shown.
WaitForRedraw ();

XML Encoding:

1
2
<!-- Use "UNICODE-1-1-UTF-8" for Chinese -->
<?xml version="1.0" encoding="UNICODE-1-1-UTF-8"?>

Unity Deserialize XML:

1
2
3
4
5
6
7
LayersetsObject lo;

using (TextReader reader = File.OpenText(filePath))
{
XmlSerializer serializer = new XmlSerializer( typeof(LayersetsObject) );
lo = serializer.Deserialize(reader) as LayersetsObject;
}


Resources

[1]. A good workflow to smoothly import 2D content into Unity, Part I and Part II by Brett Bibby
[2]. From Photoshop to Unity written by Ben of SwingSwingSubmarine
[3]. ExtendScript Toolkit & Adobe Scripting resources on Adobe Scripting Center
[4]. Independent Photoshop Scripting Community
[5]. Paul Riggott

分享