first version postprocessor completed

This commit is contained in:
2016-02-26 22:55:24 +06:00
parent 60a10c9006
commit 684e392801
10 changed files with 282 additions and 116 deletions

View File

@@ -46,8 +46,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\FlashAnimAssetEditor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\FlashAnimAssetPostprocessor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\FlashAnimEditor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\FlashAnimPostprocessor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\FlashAnimFtaPostprocessor.cs" />
<Reference Include="UnityEditor.Advertisements">
<HintPath>/Applications/Unity/Unity.app/Contents/UnityExtensions/Unity/Advertisements/Editor/UnityEditor.Advertisements.dll</HintPath>
</Reference>

View File

@@ -13,6 +13,11 @@ namespace FlashTools {
List<int> _triangles = new List<int>();
List<Vector2> _uvs = new List<Vector2>();
Mesh _mesh = null;
Vector3[] _vertices_arr = new Vector3[0];
int[] _triangles_arr = new int[0];
Vector2[] _uvs_arr = new Vector2[0];
public void Play() {
}
@@ -29,7 +34,9 @@ namespace FlashTools {
get {
int frames = 0;
if ( Asset ) {
foreach ( var layer in GetCurrentSymbol().Layers ) {
var layers = GetCurrentSymbol().Layers;
for ( var i = 0; i < layers.Count; ++i ) {
var layer = layers[i];
frames = Mathf.Max(frames, layer.Frames.Count);
}
}
@@ -55,7 +62,8 @@ namespace FlashTools {
}
FlashAnimSymbolData FindSymbol(FlashAnimLibraryData library, string symbol_id) {
foreach ( var symbol in library.Symbols ) {
for ( var i = 0; i < library.Symbols.Count; ++i ) {
var symbol = library.Symbols[i];
if ( symbol.Id == symbol_id ) {
return symbol;
}
@@ -64,7 +72,8 @@ namespace FlashTools {
}
FlashAnimBitmapData FindBitmap(FlashAnimLibraryData library, string bitmap_id) {
foreach ( var bitmap in library.Bitmaps ) {
for ( var i = 0; i < library.Bitmaps.Count; ++i ) {
var bitmap = library.Bitmaps[i];
if ( bitmap.Id == bitmap_id ) {
return bitmap;
}
@@ -111,11 +120,13 @@ namespace FlashTools {
}
void RenderSymbol(FlashAnimSymbolData symbol, int frame_num, Matrix4x4 matix) {
foreach ( var layer in symbol.Layers ) {
for ( var i = 0; i < symbol.Layers.Count; ++i ) {
var layer = symbol.Layers[i];
if ( layer.LayerType != FlashAnimLayerType.Mask ) {
var frame = GetFrameByNum(layer, frame_num);
if ( frame != null ) {
foreach ( var elem in frame.Elems ) {
for ( var j = 0; j < frame.Elems.Count; ++j ) {
var elem = frame.Elems[j];
if ( elem.Instance != null ) {
RenderInstance(
elem.Instance, frame_num, matix * elem.Matrix);
@@ -148,12 +159,42 @@ namespace FlashTools {
_current_frame,
Matrix4x4.Scale(new Vector3(1,-1,1)));
/*
if ( _vertices_arr.Length < _vertices.Count ) {
_vertices_arr = _vertices.ToArray();
} else {
_vertices.CopyTo(_vertices_arr);
}
if ( _triangles_arr.Length < _triangles.Count ) {
_triangles_arr = _triangles.ToArray();
} else {
_triangles.CopyTo(_triangles_arr);
}
if ( _uvs_arr.Length < _uvs.Count ) {
_uvs_arr = _uvs.ToArray();
} else {
_uvs.CopyTo(_uvs_arr);
}
var mesh = new Mesh();
mesh.vertices = _vertices.ToArray();
mesh.triangles = _triangles.ToArray();
mesh.uv = _uvs.ToArray();
mesh.vertices = _vertices_arr;
mesh.triangles = _triangles_arr;
mesh.uv = _uvs_arr;
mesh.RecalculateNormals();
GetComponent<MeshFilter>().mesh = mesh;
GetComponent<MeshFilter>().mesh = mesh;*/
if ( !_mesh ) {
_mesh = new Mesh();
}
if ( _mesh ) {
_mesh.Clear();
_mesh.SetVertices(_vertices);
_mesh.SetTriangles(_triangles, 0);
_mesh.SetUVs(0, _uvs);
_mesh.RecalculateNormals();
GetComponent<MeshFilter>().mesh = _mesh;
}
}
}
}

View File

@@ -50,6 +50,11 @@ namespace FlashTools {
public Vector2 RealSize = Vector2.zero;
public Rect SourceRect = new Rect();
public string ImageSource = string.Empty;
public void CopyDataFrom(FlashAnimBitmapData other) {
RealSize = other.RealSize;
SourceRect = other.SourceRect;
ImageSource = other.ImageSource;
}
}
[System.Serializable]
@@ -105,6 +110,8 @@ namespace FlashTools {
}
public class FlashAnimAsset : ScriptableObject {
public FlashAnimData Data = new FlashAnimData();
public FlashAnimData Data = new FlashAnimData();
public int MaxAtlasSize = 1024;
public int AtlasPadding = 1;
}
}

View File

@@ -8,6 +8,12 @@ namespace FlashTools.Internal {
public class FlashAnimAssetEditor : Editor {
FlashAnimAsset _asset = null;
void ApplySettings() {
AssetDatabase.ImportAsset(
AssetDatabase.GetAssetPath(_asset),
ImportAssetOptions.ForceUpdate);
}
void CreateFlashAnimOnScene() {
var anim_go = new GameObject("FlashAnim");
try {
@@ -49,9 +55,12 @@ namespace FlashTools.Internal {
public override void OnInspectorGUI() {
DrawDefaultInspector();
if ( GUILayout.Button("Apply settings") ) {
ApplySettings();
}
if ( GUILayout.Button("Create animation on scene") ) {
CreateFlashAnimOnScene();
}
}
}
}
}

View File

@@ -0,0 +1,180 @@
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Linq;
namespace FlashTools.Internal {
public class FlashAnimAssetPostprocessor : AssetPostprocessor {
static void OnPostprocessAllAssets(
string[] imported_assets, string[] deleted_assets,
string[] moved_assets, string[] moved_from_asset_paths)
{
var asset_paths = imported_assets
.Where(p => Path.GetExtension(p).ToLower().Equals(".asset"));
foreach ( var asset_path in asset_paths ) {
var asset = AssetDatabase.LoadAssetAtPath<FlashAnimAsset>(asset_path);
if ( asset ) {
FaAssetProcess(asset_path, asset);
}
}
}
static void FaAssetProcess(string fa_asset_path, FlashAnimAsset fa_asset) {
try {
if ( !MarkAllBitmapsReadable(fa_asset_path, fa_asset) ) {
AssetDatabase.ImportAsset(fa_asset_path, ImportAssetOptions.ForceUpdate);
return;
}
RemoveDuplicatedBitmaps(fa_asset_path, fa_asset);
if ( !PackBitmapsAtlas(fa_asset_path, fa_asset) ) {
AssetDatabase.ImportAsset(fa_asset_path, ImportAssetOptions.ForceUpdate);
return;
}
} catch ( Exception e ) {
Debug.LogErrorFormat("Postprocess flash anim asset error: {0}", e.Message);
}
}
static bool MarkAllBitmapsReadable(string fa_asset_path, FlashAnimAsset fa_asset) {
return FoldBitmapImporters(fa_asset_path, fa_asset.Data, true, (acc, importer) => {
var readable = importer.isReadable;
if ( !readable ) {
importer.isReadable = true;
AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
}
return readable && acc;
});
}
static void RemoveDuplicatedBitmaps(string fa_asset_path, FlashAnimAsset fa_asset) {
var bitmaps = fa_asset.Data.Library.Bitmaps;
for ( var i = 0; i < bitmaps.Count; ++i ) {
for ( var j = i + 1; j < bitmaps.Count; ++j ) {
var fst_bitmap = bitmaps[i];
var snd_bitmap = bitmaps[j];
if ( IsBitmapsExistsAndDifferentButEqual(fa_asset_path, fst_bitmap, snd_bitmap) ) {
var bitmap_importer = GetBitmapImporter(fa_asset_path, snd_bitmap);
if ( AssetDatabase.DeleteAsset(bitmap_importer.assetPath) ) {
snd_bitmap.CopyDataFrom(fst_bitmap);
EditorUtility.SetDirty(fa_asset);
}
}
}
}
}
static bool PackBitmapsAtlas(string fa_asset_path, FlashAnimAsset fa_asset) {
var textures = fa_asset.Data.Library.Bitmaps
.Select(bitmap_data => GetBitmapTexture(fa_asset_path, bitmap_data))
.ToList();
var atlas = new Texture2D(0, 0);
var atlas_path = GetBitmapsAtlasPath(fa_asset_path);
var atlas_rects = atlas.PackTextures(
textures.ToArray(), fa_asset.AtlasPadding, fa_asset.MaxAtlasSize);
File.WriteAllBytes(atlas_path, atlas.EncodeToPNG());
GameObject.DestroyImmediate(atlas);
AssetDatabase.ImportAsset(atlas_path, ImportAssetOptions.ForceUpdate);
for ( var i = 0; i < textures.Count; ++i ) {
var bitmap_data = fa_asset.Data.Library.Bitmaps[i];
bitmap_data.RealSize = new Vector2(textures[i].width, textures[i].height);
bitmap_data.SourceRect = atlas_rects[i];
}
fa_asset.Data.Atlas = AssetDatabase.LoadAssetAtPath<Texture2D>(atlas_path);
EditorUtility.SetDirty(fa_asset);
return fa_asset.Data.Atlas != null;
}
// -----------------------------
// Common
// -----------------------------
static T FoldBitmapAssetPaths<T>(
string fa_asset_path, FlashAnimData data,
T init, Func<T, string, FlashAnimBitmapData, T> act)
{
return data.Library.Bitmaps.Aggregate(init, (acc, bitmap_data) => {
var asset_path = GetBitmapAssetPath(fa_asset_path, bitmap_data);
return act(acc, asset_path, bitmap_data);
});
}
static T FoldBitmapImporters<T>(
string fa_asset_path, FlashAnimData data,
T init, Func<T, TextureImporter, T> act)
{
return FoldBitmapAssetPaths(fa_asset_path, data, init, (acc, path, bitmap_data) => {
var importer = GetBitmapImporter(fa_asset_path, bitmap_data);
return act(acc, importer);
});
}
static string GetBitmapAssetPath(string fa_asset_path, FlashAnimBitmapData bitmap_data) {
return Path.Combine(
Path.GetDirectoryName(fa_asset_path),
bitmap_data.ImageSource);
}
static Texture2D GetBitmapTexture(string fa_asset_path, FlashAnimBitmapData bitmap_data) {
var asset_path = GetBitmapAssetPath(fa_asset_path, bitmap_data);
var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(asset_path);
if ( !texture ) {
throw new UnityException(string.Format(
"bitmap ({0}) texture not found ({1})",
bitmap_data.Id, asset_path));
}
return texture;
}
static Texture2D FindBitmapTexture(string fa_asset_path, FlashAnimBitmapData bitmap_data) {
try {
return GetBitmapTexture(fa_asset_path, bitmap_data);
} catch ( Exception ) {
return null;
}
}
static TextureImporter GetBitmapImporter(string fa_asset_path, FlashAnimBitmapData bitmap_data) {
var asset_path = GetBitmapAssetPath(fa_asset_path, bitmap_data);
var importer = AssetImporter.GetAtPath(asset_path) as TextureImporter;
if ( !importer ) {
throw new UnityException(string.Format(
"bitmap ({0}) texture importer not found ({1})",
bitmap_data.Id, asset_path));
}
return importer;
}
static TextureImporter FindBitmapImporter(string fa_asset_path, FlashAnimBitmapData bitmap_data) {
try {
return GetBitmapImporter(fa_asset_path, bitmap_data);
} catch ( Exception ) {
return null;
}
}
static bool IsBitmapsExistsAndDifferentButEqual(
string fa_asset_path,
FlashAnimBitmapData bitmap_data_a, FlashAnimBitmapData bitmap_data_b)
{
if ( bitmap_data_a.ImageSource == bitmap_data_b.ImageSource ) {
return false;
}
var texture_a = FindBitmapTexture(fa_asset_path, bitmap_data_a);
var texture_b = FindBitmapTexture(fa_asset_path, bitmap_data_b);
if ( !texture_a || !texture_b ) {
return false;
}
if ( texture_a.width != texture_b.width || texture_a.height != texture_b.height ) {
return false;
}
var tex_data_a = texture_a.GetPixels32();
var tex_data_b = texture_a.GetPixels32();
return tex_data_a.SequenceEqual(tex_data_b);
}
static string GetBitmapsAtlasPath(string fa_asset_path) {
return Path.ChangeExtension(fa_asset_path, "atlas.png");
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: ca3ab7e53ac8f4d94988820890b14b55
timeCreated: 1456252226
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -4,20 +4,18 @@ using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Reflection;
using System.Globalization;
using System.Collections.Generic;
namespace FlashTools.Internal {
public class FlashAnimPostprocessor : AssetPostprocessor {
public class FlashAnimFtaPostprocessor : AssetPostprocessor {
static void OnPostprocessAllAssets(
string[] imported_assets, string[] deleted_assets,
string[] moved_assets, string[] moved_from_asset_paths)
{
var fta_assets = imported_assets
var fta_asset_paths = imported_assets
.Where(p => Path.GetExtension(p).ToLower().Equals(".fta"));
foreach ( var fta_asset in fta_assets ) {
FtaAssetProcess(fta_asset);
foreach ( var fta_asset_path in fta_asset_paths ) {
FtaAssetProcess(fta_asset_path);
}
}
@@ -33,6 +31,7 @@ namespace FlashTools.Internal {
new_asset.Data = flash_anim_data;
EditorUtility.SetDirty(new_asset);
AssetDatabase.SaveAssets();
AssetDatabase.DeleteAsset(fta_asset);
}
}
@@ -43,11 +42,9 @@ namespace FlashTools.Internal {
LoadFlashAnimStageFromFtaRootElem (fta_root_elem, flash_anim_data);
LoadFlashAnimLibraryFromFtaRootElem(fta_root_elem, flash_anim_data);
LoadFlashAnimStringsFromFtaRootElem(fta_root_elem, flash_anim_data);
PrepareBitmapTextures(fta_path, flash_anim_data);
PackBitmapTextures(fta_path, flash_anim_data);
return flash_anim_data;
} catch ( Exception e ) {
Debug.LogErrorFormat("Parsing FTA file error: {0}", e.Message);
Debug.LogErrorFormat("Parsing flash anim .fta file error: {0}", e.Message);
return null;
}
}
@@ -73,8 +70,8 @@ namespace FlashTools.Internal {
static void LoadFlashAnimBitmapsFromFtaLibraryElem(XElement library_elem, FlashAnimData data) {
foreach ( var bitmap_elem in library_elem.Elements("bitmap") ) {
var bitmap = new FlashAnimBitmapData();
bitmap.Id = SafeLoadStrFromElemAttr(bitmap_elem, "id", bitmap.Id);
var bitmap = new FlashAnimBitmapData();
bitmap.Id = SafeLoadStrFromElemAttr(bitmap_elem, "id", bitmap.Id);
bitmap.ImageSource = bitmap.Id + ".png";
data.Library.Bitmaps.Add(bitmap);
}
@@ -128,8 +125,8 @@ namespace FlashTools.Internal {
instance.Visible = SafeLoadBoolFromElemAttr(inst_elem, "visible" , instance.Visible);
var looping_elem = inst_elem.Element("looping");
if ( looping_elem != null ) {
instance.LoopingType = SafeLoadEnumFromElemAttr(looping_elem, "type" , instance.LoopingType);
instance.LoopingFirstFrame = SafeLoadIntFromElemAttr (looping_elem, "first_frame" , instance.LoopingFirstFrame);
instance.LoopingType = SafeLoadEnumFromElemAttr(looping_elem, "type" , instance.LoopingType);
instance.LoopingFirstFrame = SafeLoadIntFromElemAttr (looping_elem, "first_frame", instance.LoopingFirstFrame);
}
data.Instance = instance;
}
@@ -150,91 +147,6 @@ namespace FlashTools.Internal {
}
}
// -----------------------------
// Textures
// -----------------------------
static void PrepareBitmapTextures(string fta_path, FlashAnimData data) {
}
static void PackBitmapTextures(string fta_path, FlashAnimData data) {
var base_path = Path.GetDirectoryName(fta_path);
var textures = new List<Texture2D>();
var texturen = new List<string>();
foreach ( var bitmap in data.Library.Bitmaps ) {
var texture_path = Path.Combine(base_path, bitmap.ImageSource);
var importer = AssetImporter.GetAtPath(texture_path) as TextureImporter;
if ( !importer ) {
throw new UnityException(string.Format(
"bitmap ({0}) texture importer not found ({1})",
bitmap.Id, texture_path));
}
if ( !importer.isReadable ) {
importer.isReadable = true;
AssetDatabase.ImportAsset(texture_path, ImportAssetOptions.ForceUpdate);
AssetDatabase.ImportAsset(fta_path, ImportAssetOptions.ForceUpdate);
return;
}
var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(texture_path);
if ( !texture ) {
throw new UnityException(string.Format(
"bitmap ({0}) texture not found ({1})",
bitmap.Id, texture_path));
}
textures.Add(texture);
texturen.Add(bitmap.ImageSource);
}
var atlas = new Texture2D(0, 0);
var atlas_rects = atlas.PackTextures(textures.ToArray(), 1, 1024);
var atlas_asset_path = Path.Combine(
Path.GetDirectoryName(Application.dataPath),
Path.Combine(base_path, "atlas.png"));
File.WriteAllBytes(atlas_asset_path, atlas.EncodeToPNG());
GameObject.DestroyImmediate(atlas);
AssetDatabase.Refresh();
var atlas_path = Path.Combine(base_path, "atlas.png");
data.Atlas = AssetDatabase.LoadAssetAtPath<Texture2D>(atlas_path);
if ( !data.Atlas ) {
AssetDatabase.ImportAsset(fta_path, ImportAssetOptions.ForceUpdate);
return;
}
var atlas_importer = AssetImporter.GetAtPath(atlas_path) as TextureImporter;
if ( !atlas_importer ) {
throw new UnityException(string.Format(
"atlas importer not found ({0})",
atlas_path));
}
var method_args = new object[2]{0,0};
typeof(TextureImporter)
.GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(atlas_importer, method_args);
var atlas_width = (int)method_args[0];
var atlas_height = (int)method_args[1];
var meta_data = new List<SpriteMetaData>();
for ( var i = 0; i < atlas_rects.Length; ++i ) {
var meta_elem = new SpriteMetaData();
meta_elem.name = texturen[i];
data.Library.Bitmaps[i].RealSize = new Vector2(textures[i].width, textures[i].height);
data.Library.Bitmaps[i].SourceRect = atlas_rects[i];
meta_elem.rect = new Rect(
atlas_rects[i].xMin * atlas_width,
atlas_rects[i].yMin * atlas_height,
atlas_rects[i].width * atlas_width,
atlas_rects[i].height * atlas_height);
meta_data.Add(meta_elem);
}
atlas_importer.spritesheet = meta_data.ToArray();
atlas_importer.textureType = TextureImporterType.Sprite;
atlas_importer.spriteImportMode = SpriteImportMode.Multiple;
atlas_importer.textureFormat = TextureImporterFormat.AutomaticTruecolor;
AssetDatabase.ImportAsset(atlas_path, ImportAssetOptions.ForceUpdate);
}
// -----------------------------
// Common
// -----------------------------

View File

@@ -922,11 +922,15 @@ if (typeof Object.create != 'function') {
indent = indent || "";
ft.type_assert(indent, 'string');
ft.trace_fmt("{0}-= Exporter =-", indent);
ft.trace_fmt("{0}-Document : {1}", indent, this.document.name);
ft.trace_fmt("{0}-Document name : {1}", indent, this.get_document_name());
ft.trace_fmt("{0}-Document path : {1}", indent, this.get_document_path());
ft.trace_fmt("{0}-Export folter : {1}", indent, this.get_export_folder());
ft.trace_fmt("{0}-Export path : {1}", indent, this.get_export_path());
};
Exporter.prototype.get_document_name = function () {
return this.document.name;
};
Exporter.prototype.get_document_path = function () {
return this.documentPath;
@@ -941,7 +945,7 @@ if (typeof Object.create != 'function') {
Exporter.prototype.get_export_path = function () {
return ft.combine_path(
this.get_export_folder(),
"asset.fta");
this.get_document_name() + ".fta");
};
Exporter.prototype.export = function () {

View File

@@ -1,10 +1,10 @@
<Properties StartupItem="Assembly-CSharp.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" PreferredExecutionTarget="Unity.Instance.Unity Editor" />
<MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetPostprocessor.cs">
<Files>
<File FileName="Assets/FlashTools/Scripts/FlashAnim.cs" Line="1" Column="1" />
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/FlashAnimPostprocessor.cs" Line="1" Column="1" />
<File FileName="Assets/FlashTools/Scripts/FlashAnimAsset.cs" Line="1" Column="1" NotebookId="1" />
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetPostprocessor.cs" Line="148" Column="44" />
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/FlashAnimFtaPostprocessor.cs" Line="25" Column="5" />
<File FileName="Assets/FlashTools/Scripts/FlashAnimAsset.cs" Line="118" Column="1" NotebookId="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>