mirror of
https://github.com/BlackMATov/unity-flash-tools.git
synced 2026-01-08 17:35:46 +07:00
able move containers and assets
This commit is contained in:
@@ -8,27 +8,13 @@ using System.Collections.Generic;
|
||||
namespace FlashTools.Internal {
|
||||
[CustomEditor(typeof(SwfAsset)), CanEditMultipleObjects]
|
||||
public class SwfAssetEditor : Editor {
|
||||
List<SwfAsset> _assets = new List<SwfAsset>();
|
||||
bool _clipsFoldout = false;
|
||||
bool _settingsFoldout = false;
|
||||
List<SwfAsset> _assets = new List<SwfAsset>();
|
||||
bool _clipsFoldout = false;
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
static string GetAssetPath(SwfAsset asset) {
|
||||
return asset
|
||||
? AssetDatabase.GetAssetPath(asset)
|
||||
: string.Empty;
|
||||
}
|
||||
|
||||
static string GetSwfPath(SwfAsset asset) {
|
||||
var asset_path = GetAssetPath(asset);
|
||||
return string.IsNullOrEmpty(asset_path)
|
||||
? string.Empty
|
||||
: Path.ChangeExtension(asset_path, ".swf");
|
||||
}
|
||||
|
||||
static SwfSettings _defaultSettingsCache = null;
|
||||
static SwfSettings GetDefaultSettings() {
|
||||
if ( !_defaultSettingsCache ) {
|
||||
@@ -50,19 +36,14 @@ namespace FlashTools.Internal {
|
||||
}
|
||||
|
||||
static void ApplyOverriddenSettings(SwfAsset asset) {
|
||||
if ( File.Exists(GetSwfPath(asset)) ) {
|
||||
asset.Settings = asset.Overridden;
|
||||
ReconvertAsset(asset);
|
||||
} else {
|
||||
Debug.LogErrorFormat(
|
||||
"<b>[FlashTools]</b> Swf source for asset not found: '{0}'",
|
||||
GetSwfPath(asset));
|
||||
RevertOverriddenSettings(asset);
|
||||
}
|
||||
asset.Settings = asset.Overridden;
|
||||
ReconvertAsset(asset);
|
||||
}
|
||||
|
||||
static void ReconvertAsset(SwfAsset asset) {
|
||||
AssetDatabase.ImportAsset(GetSwfPath(asset));
|
||||
asset.Converting = new SwfAsset.ConvertingState();
|
||||
AssetDatabase.ImportAsset(
|
||||
AssetDatabase.GetAssetPath(asset));
|
||||
}
|
||||
|
||||
//
|
||||
@@ -113,14 +94,31 @@ namespace FlashTools.Internal {
|
||||
}
|
||||
}
|
||||
|
||||
void DrawGUIDefaultSettings() {
|
||||
SwfEditorUtils.DoWithEnabledGUI(false, () => {
|
||||
EditorGUILayout.ObjectField(
|
||||
"Default Settings",
|
||||
GetDefaultSettings(),
|
||||
typeof(SwfSettings),
|
||||
false);
|
||||
});
|
||||
void DrawGUISettingsControls() {
|
||||
var prop = SwfEditorUtils.GetPropertyByName(serializedObject, "Overridden");
|
||||
if ( prop.isExpanded ) {
|
||||
GUILayout.BeginHorizontal();
|
||||
GUILayout.FlexibleSpace();
|
||||
{
|
||||
var default_settings = GetDefaultSettings().Settings;
|
||||
SwfEditorUtils.DoWithEnabledGUI(
|
||||
_assets.Any(p => !p.Overridden.CheckEquals(default_settings)), () => {
|
||||
if ( GUILayout.Button("Default") ) {
|
||||
AllOverriddenSettingsToDefault();
|
||||
}
|
||||
});
|
||||
SwfEditorUtils.DoWithEnabledGUI(
|
||||
_assets.Any(p => !p.Overridden.CheckEquals(p.Settings)), () => {
|
||||
if ( GUILayout.Button("Revert") ) {
|
||||
RevertAllOverriddenSettings();
|
||||
}
|
||||
if ( GUILayout.Button("Apply") ) {
|
||||
ApplyAllOverriddenSettings();
|
||||
}
|
||||
});
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
}
|
||||
}
|
||||
|
||||
void DrawGUIClips() {
|
||||
@@ -155,41 +153,6 @@ namespace FlashTools.Internal {
|
||||
GUILayout.EndHorizontal();
|
||||
}
|
||||
|
||||
void DrawGUISettings() {
|
||||
_settingsFoldout = EditorGUILayout.Foldout(_settingsFoldout, "Settings");
|
||||
if ( _settingsFoldout ) {
|
||||
var it = SwfEditorUtils.GetPropertyByName(serializedObject, "Overridden");
|
||||
while ( it.Next(true) ) {
|
||||
EditorGUILayout.PropertyField(it, true);
|
||||
}
|
||||
DrawGUISettingsControls();
|
||||
}
|
||||
}
|
||||
|
||||
void DrawGUISettingsControls() {
|
||||
GUILayout.BeginHorizontal();
|
||||
GUILayout.FlexibleSpace();
|
||||
{
|
||||
var default_settings = GetDefaultSettings().Settings;
|
||||
SwfEditorUtils.DoWithEnabledGUI(
|
||||
_assets.Any(p => !p.Overridden.CheckEquals(default_settings)), () => {
|
||||
if ( GUILayout.Button("Default") ) {
|
||||
AllOverriddenSettingsToDefault();
|
||||
}
|
||||
});
|
||||
SwfEditorUtils.DoWithEnabledGUI(
|
||||
_assets.Any(p => !p.Overridden.CheckEquals(p.Settings)), () => {
|
||||
if ( GUILayout.Button("Revert") ) {
|
||||
RevertAllOverriddenSettings();
|
||||
}
|
||||
if ( GUILayout.Button("Apply") ) {
|
||||
ApplyAllOverriddenSettings();
|
||||
}
|
||||
});
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Messages
|
||||
@@ -197,9 +160,8 @@ namespace FlashTools.Internal {
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
void OnEnable() {
|
||||
_assets = targets.OfType<SwfAsset>().ToList();
|
||||
_clipsFoldout = false;
|
||||
_settingsFoldout = true;
|
||||
_assets = targets.OfType<SwfAsset>().ToList();
|
||||
_clipsFoldout = false;
|
||||
}
|
||||
|
||||
void OnDisable() {
|
||||
@@ -209,9 +171,8 @@ namespace FlashTools.Internal {
|
||||
public override void OnInspectorGUI() {
|
||||
serializedObject.Update();
|
||||
DrawDefaultInspector();
|
||||
DrawGUIDefaultSettings();
|
||||
DrawGUISettingsControls();
|
||||
DrawGUIClips();
|
||||
DrawGUISettings();
|
||||
if ( GUI.changed ) {
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
|
||||
@@ -26,26 +26,153 @@ namespace FlashTools.Internal {
|
||||
|
||||
static void SwfAssetProcess(SwfAsset asset) {
|
||||
try {
|
||||
var atlas_asset = LoadAtlasAsset(asset);
|
||||
if ( atlas_asset != asset.Atlas ) {
|
||||
asset.Atlas = atlas_asset;
|
||||
ConfigureAtlas(asset);
|
||||
ConfigureClips(asset);
|
||||
if ( asset.Converting.Stage == 0 ) {
|
||||
ConfigureBitmaps(asset);
|
||||
++asset.Converting.Stage;
|
||||
EditorUtility.SetDirty(asset);
|
||||
AssetDatabase.SaveAssets();
|
||||
AssetDatabase.ImportAsset(
|
||||
AssetDatabase.GetAssetPath(asset));
|
||||
} else if ( asset.Converting.Stage == 1 ) {
|
||||
asset.Atlas = LoadAssetAtlas(asset);
|
||||
if ( asset.Atlas ) {
|
||||
ConfigureAtlas(asset);
|
||||
ConfigureClips(asset);
|
||||
}
|
||||
++asset.Converting.Stage;
|
||||
EditorUtility.SetDirty(asset);
|
||||
AssetDatabase.ImportAsset(
|
||||
AssetDatabase.GetAssetPath(asset));
|
||||
}
|
||||
ConfigureAssetClips(asset);
|
||||
} catch ( Exception e ) {
|
||||
Debug.LogErrorFormat(
|
||||
"<b>[FlashTools]</b> Postprocess swf asset error: {0}",
|
||||
e.Message);
|
||||
SwfEditorUtils.DeleteAssetWithDepends(asset);
|
||||
AssetDatabase.DeleteAsset(
|
||||
AssetDatabase.GetAssetPath(asset));
|
||||
} finally {
|
||||
if ( asset ) {
|
||||
UpdateAssetClips(asset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Texture2D LoadAtlasAsset(SwfAsset asset) {
|
||||
static Texture2D LoadAssetAtlas(SwfAsset asset) {
|
||||
return AssetDatabase.LoadAssetAtPath<Texture2D>(
|
||||
SwfEditorUtils.GetAtlasPathFromAsset(asset));
|
||||
GetAtlasPath(asset));
|
||||
}
|
||||
|
||||
static string GetAtlasPath(SwfAsset asset) {
|
||||
if ( asset.Atlas ) {
|
||||
return AssetDatabase.GetAssetPath(asset.Atlas);
|
||||
} else {
|
||||
var asset_path = AssetDatabase.GetAssetPath(asset);
|
||||
return Path.ChangeExtension(asset_path, "._Atlas_.png");
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// ConfigureBitmaps
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
static void ConfigureBitmaps(SwfAsset asset) {
|
||||
var textures = asset.Data.Bitmaps
|
||||
.Where (p => p.Redirect == 0)
|
||||
.Select (p => new KeyValuePair<ushort, Texture2D>(
|
||||
p.Id,
|
||||
LoadTextureFromData(p)))
|
||||
.ToList();
|
||||
var rects = PackAndSaveBitmapsAtlas(
|
||||
GetAtlasPath(asset),
|
||||
textures.Select(p => p.Value).ToArray(),
|
||||
asset.Settings);
|
||||
for ( var i = 0; i < asset.Data.Bitmaps.Count; ++i ) {
|
||||
var bitmap = asset.Data.Bitmaps[i];
|
||||
var texture_key = bitmap.Redirect > 0 ? bitmap.Redirect : bitmap.Id;
|
||||
bitmap.SourceRect = rects[textures.FindIndex(p => p.Key == texture_key)];
|
||||
}
|
||||
}
|
||||
|
||||
static Texture2D LoadTextureFromData(SwfBitmapData bitmap) {
|
||||
var texture = new Texture2D(
|
||||
bitmap.RealWidth, bitmap.RealHeight,
|
||||
TextureFormat.ARGB32, false);
|
||||
texture.LoadRawTextureData(bitmap.ARGB32);
|
||||
RevertTexturePremultipliedAlpha(texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
static void RevertTexturePremultipliedAlpha(Texture2D texture) {
|
||||
for ( int y = 0; y < texture.height; ++y ) {
|
||||
for ( int x = 0; x < texture.width; ++x ) {
|
||||
var c = texture.GetPixel(x, y);
|
||||
if ( c.a > 0 ) {
|
||||
c.r /= c.a;
|
||||
c.g /= c.a;
|
||||
c.b /= c.a;
|
||||
}
|
||||
texture.SetPixel(x, y, c);
|
||||
}
|
||||
}
|
||||
texture.Apply();
|
||||
}
|
||||
|
||||
struct BitmapsAtlasInfo {
|
||||
public Texture2D Atlas;
|
||||
public Rect[] Rects;
|
||||
}
|
||||
|
||||
static Rect[] PackAndSaveBitmapsAtlas(
|
||||
string atlas_path, Texture2D[] textures, SwfSettingsData settings)
|
||||
{
|
||||
var atlas_info = PackBitmapsAtlas(textures, settings);
|
||||
File.WriteAllBytes(atlas_path, atlas_info.Atlas.EncodeToPNG());
|
||||
GameObject.DestroyImmediate(atlas_info.Atlas, true);
|
||||
AssetDatabase.ImportAsset(atlas_path);
|
||||
return atlas_info.Rects;
|
||||
}
|
||||
|
||||
static BitmapsAtlasInfo PackBitmapsAtlas(
|
||||
Texture2D[] textures, SwfSettingsData settings)
|
||||
{
|
||||
var atlas_padding = Mathf.Max(0, settings.AtlasPadding);
|
||||
var max_atlas_size = Mathf.Max(32, settings.AtlasPowerOfTwo
|
||||
? Mathf.ClosestPowerOfTwo(settings.MaxAtlasSize)
|
||||
: settings.MaxAtlasSize);
|
||||
var atlas = new Texture2D(0, 0);
|
||||
var rects = atlas.PackTextures(textures, atlas_padding, max_atlas_size);
|
||||
while ( rects == null ) {
|
||||
max_atlas_size = Mathf.NextPowerOfTwo(max_atlas_size + 1);
|
||||
rects = atlas.PackTextures(textures, atlas_padding, max_atlas_size);
|
||||
}
|
||||
return settings.AtlasForceSquare && atlas.width != atlas.height
|
||||
? BitmapsAtlasToSquare(atlas, rects)
|
||||
: new BitmapsAtlasInfo{Atlas = atlas, Rects = rects};
|
||||
}
|
||||
|
||||
static BitmapsAtlasInfo BitmapsAtlasToSquare(Texture2D atlas, Rect[] rects) {
|
||||
var atlas_size = Mathf.Max(atlas.width, atlas.height);
|
||||
var atlas_scale = new Vector2(atlas.width, atlas.height) / atlas_size;
|
||||
var new_atlas = new Texture2D(atlas_size, atlas_size, TextureFormat.ARGB32, false);
|
||||
for ( var i = 0; i < rects.Length; ++i ) {
|
||||
var new_position = rects[i].position;
|
||||
new_position.Scale(atlas_scale);
|
||||
var new_size = rects[i].size;
|
||||
new_size.Scale(atlas_scale);
|
||||
rects[i] = new Rect(new_position, new_size);
|
||||
}
|
||||
var fill_pixels = new Color32[atlas_size * atlas_size];
|
||||
for ( var i = 0; i < atlas_size * atlas_size; ++i ) {
|
||||
fill_pixels[i] = new Color(1,1,1,0);
|
||||
}
|
||||
new_atlas.SetPixels32(fill_pixels);
|
||||
new_atlas.SetPixels32(0, 0, atlas.width, atlas.height, atlas.GetPixels32());
|
||||
new_atlas.Apply();
|
||||
GameObject.DestroyImmediate(atlas, true);
|
||||
return new BitmapsAtlasInfo{
|
||||
Atlas = new_atlas,
|
||||
Rects = rects};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
@@ -55,28 +182,21 @@ namespace FlashTools.Internal {
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
static void ConfigureAtlas(SwfAsset asset) {
|
||||
var atlas_importer = GetBitmapsAtlasImporter(asset);
|
||||
var atlas_importer_size = GetSizeFromTextureImporter(atlas_importer);
|
||||
atlas_importer.spritesheet = asset.Data.Bitmaps
|
||||
.Select(bitmap => new SpriteMetaData{
|
||||
name = bitmap.Id.ToString(),
|
||||
rect = new Rect(
|
||||
bitmap.SourceRect.xMin * atlas_importer_size.x,
|
||||
bitmap.SourceRect.yMin * atlas_importer_size.y,
|
||||
bitmap.SourceRect.width * atlas_importer_size.x,
|
||||
bitmap.SourceRect.height * atlas_importer_size.y)})
|
||||
.ToArray();
|
||||
var atlas_path = AssetDatabase.GetAssetPath(asset.Atlas);
|
||||
var atlas_importer = GetBitmapsAtlasImporter(asset);
|
||||
atlas_importer.spritesheet = new SpriteMetaData[0];
|
||||
atlas_importer.textureType = TextureImporterType.Sprite;
|
||||
atlas_importer.spriteImportMode = SpriteImportMode.Multiple;
|
||||
atlas_importer.spritePixelsPerUnit = asset.Settings.PixelsPerUnit;
|
||||
atlas_importer.mipmapEnabled = asset.Settings.GenerateMipMaps;
|
||||
atlas_importer.filterMode = SwfAtlasFilterToImporterFilter(asset.Settings.AtlasTextureFilter);
|
||||
atlas_importer.textureFormat = SwfAtlasFormatToImporterFormat(asset.Settings.AtlasTextureFormat);
|
||||
AssetDatabase.ImportAsset(SwfEditorUtils.GetAtlasPathFromAsset(asset));
|
||||
AssetDatabase.WriteImportSettingsIfDirty(atlas_path);
|
||||
AssetDatabase.ImportAsset(atlas_path);
|
||||
}
|
||||
|
||||
static TextureImporter GetBitmapsAtlasImporter(SwfAsset asset) {
|
||||
var atlas_path = SwfEditorUtils.GetAtlasPathFromAsset(asset);
|
||||
var atlas_path = AssetDatabase.GetAssetPath(asset.Atlas);
|
||||
var atlas_importer = AssetImporter.GetAtPath(atlas_path) as TextureImporter;
|
||||
if ( !atlas_importer ) {
|
||||
throw new UnityException(string.Format(
|
||||
@@ -86,14 +206,6 @@ namespace FlashTools.Internal {
|
||||
return atlas_importer;
|
||||
}
|
||||
|
||||
static Vector2 GetSizeFromTextureImporter(TextureImporter importer) {
|
||||
var method_args = new object[2]{0,0};
|
||||
typeof(TextureImporter)
|
||||
.GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance)
|
||||
.Invoke(importer, method_args);
|
||||
return new Vector2((int)method_args[0], (int)method_args[1]);
|
||||
}
|
||||
|
||||
static FilterMode SwfAtlasFilterToImporterFilter(
|
||||
SwfSettingsData.AtlasFilter filter)
|
||||
{
|
||||
@@ -137,22 +249,34 @@ namespace FlashTools.Internal {
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
static void ConfigureClips(SwfAsset asset) {
|
||||
asset.Clips.Clear();
|
||||
asset.Clips = asset.Clips.Where(p => !!p).ToList();
|
||||
foreach ( var symbol in asset.Data.Symbols ) {
|
||||
ConfigureClip(asset, symbol);
|
||||
}
|
||||
}
|
||||
|
||||
static void ConfigureClip(SwfAsset asset, SwfSymbolData symbol) {
|
||||
var asset_path = AssetDatabase.GetAssetPath(asset);
|
||||
var clip_asset_path = Path.ChangeExtension(asset_path, symbol.Name + ".asset");
|
||||
var clip_asset = SwfEditorUtils.LoadOrCreateAsset<SwfClipAsset>(clip_asset_path);
|
||||
var clip_asset = asset.Clips.FirstOrDefault(p => p.Name == symbol.Name);
|
||||
if ( clip_asset ) {
|
||||
ConfigureClipAsset(clip_asset, asset, symbol);
|
||||
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(clip_asset));
|
||||
} else {
|
||||
var asset_path = AssetDatabase.GetAssetPath(asset);
|
||||
var clip_asset_path = Path.ChangeExtension(asset_path, symbol.Name + ".asset");
|
||||
SwfEditorUtils.LoadOrCreateAsset<SwfClipAsset>(clip_asset_path, new_clip_asset => {
|
||||
ConfigureClipAsset(new_clip_asset, asset, symbol);
|
||||
asset.Clips.Add(new_clip_asset);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static void ConfigureClipAsset(
|
||||
SwfClipAsset clip_asset, SwfAsset asset, SwfSymbolData symbol)
|
||||
{
|
||||
clip_asset.Name = symbol.Name;
|
||||
clip_asset.Atlas = asset.Atlas;
|
||||
clip_asset.Container = AssetDatabase.AssetPathToGUID(asset_path);
|
||||
clip_asset.FrameRate = asset.Data.FrameRate;
|
||||
clip_asset.Sequences = LoadClipSequences(asset, symbol);
|
||||
EditorUtility.SetDirty(clip_asset);
|
||||
asset.Clips.Add(clip_asset);
|
||||
}
|
||||
|
||||
static List<SwfClipAsset.Sequence> LoadClipSequences(
|
||||
@@ -206,8 +330,8 @@ namespace FlashTools.Internal {
|
||||
? FindBitmapFromAssetData(asset.Data, inst.Bitmap)
|
||||
: null;
|
||||
if ( bitmap != null && IsVisibleInstance(inst) ) {
|
||||
var width = bitmap.RealSize.x / 20.0f;
|
||||
var height = bitmap.RealSize.y / 20.0f;
|
||||
var width = bitmap.RealWidth / 20.0f;
|
||||
var height = bitmap.RealHeight / 20.0f;
|
||||
|
||||
var v0 = new Vector2( 0, 0);
|
||||
var v1 = new Vector2(width, 0);
|
||||
@@ -323,12 +447,12 @@ namespace FlashTools.Internal {
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// ConfigureAssetClips
|
||||
// UpdateAssetClips
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
static void ConfigureAssetClips(SwfAsset asset) {
|
||||
var clips = GameObject.FindObjectsOfType<SwfClip>();
|
||||
static void UpdateAssetClips(SwfAsset asset) {
|
||||
var clips = Resources.FindObjectsOfTypeAll<SwfClip>();
|
||||
foreach ( var clip in clips ) {
|
||||
if ( clip && clip.clip && asset.Clips.Contains(clip.clip) ) {
|
||||
clip.UpdateAllProperties();
|
||||
|
||||
@@ -26,44 +26,38 @@ namespace FlashTools.Internal {
|
||||
|
||||
static void SwfFileProcess(string swf_path) {
|
||||
var swf_asset_path = Path.ChangeExtension(swf_path, ".asset");
|
||||
var swf_asset = SwfEditorUtils.LoadOrCreateAsset<SwfAsset>(swf_asset_path);
|
||||
if ( LoadSwfAsset(swf_path, swf_asset) ) {
|
||||
EditorUtility.SetDirty(swf_asset);
|
||||
AssetDatabase.SaveAssets();
|
||||
} else {
|
||||
SwfEditorUtils.DeleteAssetWithDepends(swf_asset);
|
||||
}
|
||||
SwfEditorUtils.LoadOrCreateAsset<SwfAsset>(swf_asset_path, swf_asset => {
|
||||
SafeLoadSwfAsset(swf_path, swf_asset);
|
||||
});
|
||||
}
|
||||
|
||||
static bool LoadSwfAsset(string swf_path, SwfAsset swf_asset) {
|
||||
static void SafeLoadSwfAsset(string swf_path, SwfAsset swf_asset) {
|
||||
try {
|
||||
if ( swf_asset.Atlas ) {
|
||||
AssetDatabase.DeleteAsset(
|
||||
AssetDatabase.GetAssetPath(swf_asset.Atlas));
|
||||
swf_asset.Atlas = null;
|
||||
}
|
||||
swf_asset.Data = LoadSwfAssetData(
|
||||
swf_asset,
|
||||
new SwfDecoder(swf_path));
|
||||
return true;
|
||||
var new_data = LoadSwfAssetData(swf_path);
|
||||
swf_asset.Data = new_data;
|
||||
swf_asset.Converting = new SwfAsset.ConvertingState();
|
||||
} catch ( Exception e ) {
|
||||
Debug.LogErrorFormat(
|
||||
"<b>[FlashTools]</b> Parsing swf error: {0}",
|
||||
e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static SwfAssetData LoadSwfAssetData(
|
||||
SwfAsset swf_asset, SwfDecoder swf_decoder)
|
||||
{
|
||||
static SwfAssetData LoadSwfAssetData(string swf_path) {
|
||||
var library = new SwfLibrary();
|
||||
var decoder = new SwfDecoder(swf_path);
|
||||
return new SwfAssetData{
|
||||
FrameRate = swf_decoder.UncompressedHeader.FrameRate,
|
||||
Symbols = LoadSymbols(library, swf_decoder),
|
||||
Bitmaps = LoadBitmaps(library, swf_asset)};
|
||||
FrameRate = decoder.UncompressedHeader.FrameRate,
|
||||
Symbols = LoadSymbols(library, decoder),
|
||||
Bitmaps = LoadBitmaps(library)};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// LoadSymbols
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
static List<SwfSymbolData> LoadSymbols(
|
||||
SwfLibrary library, SwfDecoder decoder)
|
||||
{
|
||||
@@ -178,7 +172,7 @@ namespace FlashTools.Internal {
|
||||
break;
|
||||
default:
|
||||
throw new UnityException(string.Format(
|
||||
"Unsupported SwfDisplayInstanceType: {0}", inst.Type));
|
||||
"unsupported SwfDisplayInstanceType: {0}", inst.Type));
|
||||
}
|
||||
}
|
||||
CheckSelfMasks(self_masks, ushort.MaxValue, frame);
|
||||
@@ -201,105 +195,23 @@ namespace FlashTools.Internal {
|
||||
masks.RemoveAll(p => p.ClipDepth < depth);
|
||||
}
|
||||
|
||||
static List<SwfBitmapData> LoadBitmaps(
|
||||
SwfLibrary library, SwfAsset asset)
|
||||
{
|
||||
var bitmap_defines = library.Defines
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// LoadBitmaps
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
static List<SwfBitmapData> LoadBitmaps(SwfLibrary library) {
|
||||
return library.Defines
|
||||
.Where (p => p.Value.Type == SwfLibraryDefineType.Bitmap)
|
||||
.ToDictionary(p => p.Key, p => p.Value as SwfLibraryBitmapDefine);
|
||||
var textures = bitmap_defines
|
||||
.Where (p => p.Value.Redirect == 0)
|
||||
.Select (p => new KeyValuePair<int, Texture2D>(
|
||||
p.Key, LoadTextureFromBitmapDefine(p.Value)))
|
||||
.ToDictionary(p => p.Key, p => p.Value as SwfLibraryBitmapDefine)
|
||||
.Select (p => new SwfBitmapData{
|
||||
Id = p.Key,
|
||||
ARGB32 = p.Value.ARGB32,
|
||||
Redirect = p.Value.Redirect,
|
||||
RealWidth = p.Value.Width,
|
||||
RealHeight = p.Value.Height})
|
||||
.ToList();
|
||||
var rects = PackAndSaveBitmapsAtlas(asset, textures.Select(p => p.Value).ToArray());
|
||||
var bitmaps = new List<SwfBitmapData>(bitmap_defines.Count);
|
||||
foreach ( var bitmap_define in bitmap_defines ) {
|
||||
var texture_key = bitmap_define.Value.Redirect > 0
|
||||
? bitmap_define.Value.Redirect
|
||||
: bitmap_define.Key;
|
||||
var bitmap_data = new SwfBitmapData{
|
||||
Id = bitmap_define.Key,
|
||||
RealSize = new Vector2(bitmap_define.Value.Width, bitmap_define.Value.Height),
|
||||
SourceRect = rects[textures.FindIndex(p => p.Key == texture_key)]};
|
||||
bitmaps.Add(bitmap_data);
|
||||
}
|
||||
return bitmaps;
|
||||
}
|
||||
|
||||
static Texture2D LoadTextureFromBitmapDefine(SwfLibraryBitmapDefine bitmap) {
|
||||
var texture = new Texture2D(
|
||||
bitmap.Width, bitmap.Height,
|
||||
TextureFormat.ARGB32, false);
|
||||
texture.LoadRawTextureData(bitmap.ARGB32);
|
||||
RevertTexturePremultipliedAlpha(texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
static void RevertTexturePremultipliedAlpha(Texture2D texture) {
|
||||
for ( int y = 0; y < texture.height; ++y ) {
|
||||
for ( int x = 0; x < texture.width; ++x ) {
|
||||
var c = texture.GetPixel(x, y);
|
||||
if ( c.a > 0 ) {
|
||||
c.r /= c.a;
|
||||
c.g /= c.a;
|
||||
c.b /= c.a;
|
||||
}
|
||||
texture.SetPixel(x, y, c);
|
||||
}
|
||||
}
|
||||
texture.Apply();
|
||||
}
|
||||
|
||||
struct BitmapsAtlasInfo {
|
||||
public Texture2D Atlas;
|
||||
public Rect[] Rects;
|
||||
}
|
||||
|
||||
static Rect[] PackAndSaveBitmapsAtlas(SwfAsset asset, Texture2D[] textures) {
|
||||
var atlas_info = PackBitmapsAtlas(textures, asset.Settings);
|
||||
var atlas_path = SwfEditorUtils.GetAtlasPathFromAsset(asset);
|
||||
File.WriteAllBytes(atlas_path, atlas_info.Atlas.EncodeToPNG());
|
||||
GameObject.DestroyImmediate(atlas_info.Atlas, true);
|
||||
AssetDatabase.ImportAsset(atlas_path);
|
||||
return atlas_info.Rects;
|
||||
}
|
||||
|
||||
static BitmapsAtlasInfo PackBitmapsAtlas(Texture2D[] textures, SwfSettingsData settings) {
|
||||
var atlas_padding = Mathf.Max(0, settings.AtlasPadding);
|
||||
var max_atlas_size = Mathf.Max(32, settings.AtlasPowerOfTwo
|
||||
? Mathf.ClosestPowerOfTwo(settings.MaxAtlasSize)
|
||||
: settings.MaxAtlasSize);
|
||||
var atlas = new Texture2D(0, 0);
|
||||
var rects = atlas.PackTextures(textures, atlas_padding, max_atlas_size);
|
||||
if ( rects == null ) {
|
||||
throw new UnityException("Pack textures to atlas error");
|
||||
}
|
||||
return settings.AtlasForceSquare && atlas.width != atlas.height
|
||||
? BitmapsAtlasToSquare(atlas, rects)
|
||||
: new BitmapsAtlasInfo{Atlas = atlas, Rects = rects};
|
||||
}
|
||||
|
||||
static BitmapsAtlasInfo BitmapsAtlasToSquare(Texture2D atlas, Rect[] rects) {
|
||||
var atlas_size = Mathf.Max(atlas.width, atlas.height);
|
||||
var atlas_scale = new Vector2(atlas.width, atlas.height) / atlas_size;
|
||||
var new_atlas = new Texture2D(atlas_size, atlas_size, TextureFormat.ARGB32, false);
|
||||
for ( var i = 0; i < rects.Length; ++i ) {
|
||||
var new_position = rects[i].position;
|
||||
new_position.Scale(atlas_scale);
|
||||
var new_size = rects[i].size;
|
||||
new_size.Scale(atlas_scale);
|
||||
rects[i] = new Rect(new_position, new_size);
|
||||
}
|
||||
var fill_pixels = new Color32[atlas_size * atlas_size];
|
||||
for ( var i = 0; i < atlas_size * atlas_size; ++i ) {
|
||||
fill_pixels[i] = new Color(1,1,1,0);
|
||||
}
|
||||
new_atlas.SetPixels32(fill_pixels);
|
||||
new_atlas.SetPixels32(0, 0, atlas.width, atlas.height, atlas.GetPixels32());
|
||||
new_atlas.Apply();
|
||||
GameObject.DestroyImmediate(atlas, true);
|
||||
return new BitmapsAtlasInfo{Atlas = new_atlas, Rects = rects};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,17 +44,6 @@ namespace FlashTools.Internal {
|
||||
return prop;
|
||||
}
|
||||
|
||||
public static void DeleteAssetWithDepends(SwfAsset asset) {
|
||||
if ( asset ) {
|
||||
if ( asset.Atlas ) {
|
||||
AssetDatabase.DeleteAsset(
|
||||
AssetDatabase.GetAssetPath(asset.Atlas));
|
||||
}
|
||||
AssetDatabase.DeleteAsset(
|
||||
AssetDatabase.GetAssetPath(asset));
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveAllSubAssets(string asset_path) {
|
||||
var assets = AssetDatabase.LoadAllAssetsAtPath(asset_path);
|
||||
foreach ( var asset in assets ) {
|
||||
@@ -64,20 +53,19 @@ namespace FlashTools.Internal {
|
||||
}
|
||||
}
|
||||
|
||||
public static T LoadOrCreateAsset<T>(string asset_path) where T : ScriptableObject {
|
||||
public static T LoadOrCreateAsset<T>(string asset_path, System.Action<T> act) where T : ScriptableObject {
|
||||
var asset = AssetDatabase.LoadAssetAtPath<T>(asset_path);
|
||||
if ( !asset ) {
|
||||
if ( asset ) {
|
||||
act(asset);
|
||||
} else {
|
||||
asset = ScriptableObject.CreateInstance<T>();
|
||||
act(asset);
|
||||
AssetDatabase.CreateAsset(asset, asset_path);
|
||||
}
|
||||
AssetDatabase.ImportAsset(asset_path);
|
||||
return asset;
|
||||
}
|
||||
|
||||
public static string GetAtlasPathFromAsset(SwfAsset asset) {
|
||||
var asset_path = AssetDatabase.GetAssetPath(asset);
|
||||
return Path.ChangeExtension(asset_path, "._Atlas_.png");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Internal
|
||||
|
||||
@@ -271,4 +271,23 @@ namespace FlashTools.Internal {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// SwfDisplayNameDrawer
|
||||
//
|
||||
|
||||
[CustomPropertyDrawer(typeof(SwfDisplayNameAttribute))]
|
||||
public class SwfDisplayNameDrawer : PropertyDrawer {
|
||||
public override void OnGUI(
|
||||
Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
var new_label = new GUIContent(label);
|
||||
new_label.text = (attribute as SwfDisplayNameAttribute).DisplayName;
|
||||
if ( EditorGUI.PropertyField(position, property, new_label) ) {
|
||||
foreach ( SerializedProperty child in property ) {
|
||||
EditorGUILayout.PropertyField(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,4 +42,11 @@ namespace FlashTools.Internal {
|
||||
ReadOnly = read_only;
|
||||
}
|
||||
}
|
||||
|
||||
public class SwfDisplayNameAttribute : PropertyAttribute {
|
||||
public string DisplayName;
|
||||
public SwfDisplayNameAttribute(string display_name) {
|
||||
DisplayName = display_name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,8 +104,11 @@ namespace FlashTools {
|
||||
|
||||
[System.Serializable]
|
||||
public class SwfBitmapData {
|
||||
public int Id = 0;
|
||||
public Vector2 RealSize = Vector2.zero;
|
||||
public ushort Id = 0;
|
||||
public byte[] ARGB32 = new byte[0];
|
||||
public ushort Redirect = 0;
|
||||
public int RealWidth = 0;
|
||||
public int RealHeight = 0;
|
||||
public Rect SourceRect = new Rect();
|
||||
}
|
||||
|
||||
@@ -117,6 +120,10 @@ namespace FlashTools {
|
||||
}
|
||||
|
||||
public class SwfAsset : ScriptableObject {
|
||||
[System.Serializable]
|
||||
public struct ConvertingState {
|
||||
public int Stage;
|
||||
}
|
||||
[HideInInspector]
|
||||
public SwfAssetData Data;
|
||||
[SwfReadOnly]
|
||||
@@ -125,8 +132,10 @@ namespace FlashTools {
|
||||
public List<SwfClipAsset> Clips;
|
||||
[HideInInspector]
|
||||
public SwfSettingsData Settings;
|
||||
[HideInInspector]
|
||||
[SwfDisplayName("Settings")]
|
||||
public SwfSettingsData Overridden;
|
||||
[HideInInspector]
|
||||
public ConvertingState Converting;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void Reset() {
|
||||
@@ -135,6 +144,7 @@ namespace FlashTools {
|
||||
Clips = new List<SwfClipAsset>();
|
||||
Settings = SwfSettings.GetDefault();
|
||||
Overridden = SwfSettings.GetDefault();
|
||||
Converting = new ConvertingState();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -70,16 +70,17 @@ namespace FlashTools {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Sequence {
|
||||
public string Name = string.Empty;
|
||||
public List<Frame> Frames = new List<Frame>();
|
||||
}
|
||||
|
||||
[SwfReadOnly]
|
||||
public string Name;
|
||||
[SwfReadOnly]
|
||||
public Texture2D Atlas;
|
||||
[SwfAssetGUID(true)]
|
||||
public string Container;
|
||||
[SwfReadOnly]
|
||||
public float FrameRate;
|
||||
[HideInInspector]
|
||||
@@ -87,8 +88,8 @@ namespace FlashTools {
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void Reset() {
|
||||
Name = string.Empty;
|
||||
Atlas = null;
|
||||
Container = string.Empty;
|
||||
FrameRate = 1.0f;
|
||||
Sequences = new List<Sequence>();
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
<Properties StartupItem="Assembly-CSharp.csproj">
|
||||
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" PreferredExecutionTarget="Unity.Instance.Unity Editor" />
|
||||
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/FlashTools/Scripts/SwfAsset.cs">
|
||||
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfAssetPostprocessor.cs">
|
||||
<Files>
|
||||
<File FileName="Assets/FlashTools/Scripts/SwfAsset.cs" Line="20" Column="3" />
|
||||
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfAssetPostprocessor.cs" Line="160" Column="33" />
|
||||
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfAssetEditor.cs" Line="1" Column="1" />
|
||||
</Files>
|
||||
</MonoDevelop.Ide.Workbench>
|
||||
<MonoDevelop.Ide.DebuggingService.Breakpoints>
|
||||
|
||||
Reference in New Issue
Block a user