big name refactor continue

This commit is contained in:
2016-09-02 15:34:50 +07:00
parent 0bdd3fe995
commit 5d9bc9ee40
22 changed files with 177 additions and 112 deletions

View File

@@ -45,14 +45,14 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfAssetEditor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfAssetPostprocessor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfClipAssetEditor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfClipControllerEditor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfClipEditor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\Editors\SwfAssetEditor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\Editors\SwfClipAssetEditor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\Editors\SwfClipControllerEditor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\Editors\SwfClipEditor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\Editors\SwfManagerEditor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\Postprocessors\SwfAssetPostprocessor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\Postprocessors\SwfPostprocessor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfEditorUtils.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfManagerEditor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfPostprocessor.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfPropertyDrawers.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfTools\SwfContext.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfTools\SwfContextExecuter.cs" />

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 518cad4150eaf4133881bf2a4bae81ed
folderAsset: yes
timeCreated: 1472790046
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -26,7 +26,7 @@ namespace FlashTools.Internal {
var asset_path = GetAssetPath(asset);
return string.IsNullOrEmpty(asset_path)
? string.Empty
: SwfEditorUtils.GetSwfPathFromSettingsPath(asset_path);
: Path.ChangeExtension(asset_path, ".swf");
}
//
@@ -93,7 +93,7 @@ namespace FlashTools.Internal {
var message = unapplied.Length == 1
? string.Format(
"Unapplied swf asset settings for '{0}'",
GetAssetPath(unapplied[0]))
AssetDatabase.GetAssetPath(unapplied[0]))
: string.Format(
"Unapplied multiple({0}) swf asset settings",
unapplied.Length);
@@ -107,12 +107,6 @@ namespace FlashTools.Internal {
void DrawGUISettings() {
SwfEditorUtils.DoWithEnabledGUI(false, () => {
var script_prop = SwfEditorUtils.GetPropertyByName(serializedObject, "m_Script");
EditorGUILayout.PropertyField(script_prop, true);
var atlas_prop = SwfEditorUtils.GetPropertyByName(serializedObject, "Atlas");
EditorGUILayout.PropertyField(atlas_prop, true);
var clips_prop = SwfEditorUtils.GetPropertyByName(serializedObject, "Clips");
if ( clips_prop.isArray ) {
SwfEditorUtils.DoWithMixedValue(
@@ -175,6 +169,7 @@ namespace FlashTools.Internal {
public override void OnInspectorGUI() {
serializedObject.Update();
DrawDefaultInspector();
DrawGUISettings();
if ( GUI.changed ) {
serializedObject.ApplyModifiedProperties();

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 6f6448c4e793c4081bce4ef3bea5dc30
folderAsset: yes
timeCreated: 1472790065
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -19,33 +19,33 @@ namespace FlashTools.Internal {
foreach ( var asset_path in asset_paths ) {
var asset = AssetDatabase.LoadAssetAtPath<SwfAsset>(asset_path);
if ( asset ) {
AssetProcess(asset_path, asset);
SwfAssetProcess(asset);
}
}
}
static void AssetProcess(string asset_path, SwfAsset asset) {
static void SwfAssetProcess(SwfAsset asset) {
try {
var atlas_asset = LoadAtlasAsset(asset_path);
var atlas_asset = LoadAtlasAsset(asset);
if ( atlas_asset != asset.Atlas ) {
asset.Atlas = atlas_asset;
ConfigureAtlas(asset_path, asset);
ConfigureClips(asset_path, asset);
ConfigureAtlas(asset);
ConfigureClips(asset);
EditorUtility.SetDirty(asset);
AssetDatabase.SaveAssets();
}
ConfigureAssetClips(asset);
} catch ( Exception e ) {
Debug.LogErrorFormat(
"Postprocess swf animation asset error: {0}",
"Postprocess swf asset error: {0}",
e.Message);
SwfEditorUtils.DeleteAssetWithDepends(asset);
}
}
static Texture2D LoadAtlasAsset(string asset_path) {
static Texture2D LoadAtlasAsset(SwfAsset asset) {
return AssetDatabase.LoadAssetAtPath<Texture2D>(
SwfEditorUtils.GetAtlasPathFromSettingsPath(asset_path));
SwfEditorUtils.GetAtlasPathFromAsset(asset));
}
// ---------------------------------------------------------------------
@@ -54,8 +54,8 @@ namespace FlashTools.Internal {
//
// ---------------------------------------------------------------------
static void ConfigureAtlas(string asset_path, SwfAsset asset) {
var atlas_importer = GetBitmapsAtlasImporter(asset_path);
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{
@@ -72,12 +72,11 @@ namespace FlashTools.Internal {
atlas_importer.mipmapEnabled = asset.Settings.GenerateMipMaps;
atlas_importer.filterMode = SwfAtlasFilterToImporterFilter(asset.Settings.AtlasTextureFilter);
atlas_importer.textureFormat = SwfAtlasFormatToImporterFormat(asset.Settings.AtlasTextureFormat);
AssetDatabase.ImportAsset(
SwfEditorUtils.GetAtlasPathFromSettingsPath(asset_path));
AssetDatabase.ImportAsset(SwfEditorUtils.GetAtlasPathFromAsset(asset));
}
static TextureImporter GetBitmapsAtlasImporter(string asset_path) {
var atlas_path = SwfEditorUtils.GetAtlasPathFromSettingsPath(asset_path);
static TextureImporter GetBitmapsAtlasImporter(SwfAsset asset) {
var atlas_path = SwfEditorUtils.GetAtlasPathFromAsset(asset);
var atlas_importer = AssetImporter.GetAtPath(atlas_path) as TextureImporter;
if ( !atlas_importer ) {
throw new UnityException(string.Format(
@@ -137,36 +136,32 @@ namespace FlashTools.Internal {
//
// ---------------------------------------------------------------------
static void ConfigureClips(
string asset_path,
SwfAsset asset)
{
SwfEditorUtils.RemoveAllSubAssets(asset_path);
static void ConfigureClips(SwfAsset asset) {
foreach ( var symbol in asset.Data.Symbols ) {
ConfigureClip(asset_path, asset, symbol);
ConfigureClip(asset, symbol);
}
}
static void ConfigureClip(
string asset_path,
SwfAsset asset,
SwfSymbolData symbol)
{
var clip_asset_path = SwfEditorUtils.GetClipPathFromSettingsPath(
asset_path, symbol.Name);
var clip_asset = AssetDatabase.LoadAssetAtPath<SwfClipAsset>(clip_asset_path);
if ( !clip_asset ) {
clip_asset = ScriptableObject.CreateInstance<SwfClipAsset>();
AssetDatabase.CreateAsset(clip_asset, clip_asset_path);
}
static void ConfigureClip(SwfAsset asset, SwfSymbolData symbol) {
var clip_asset_path = Path.ChangeExtension(
AssetDatabase.GetAssetPath(asset),
symbol.Name + ".asset");
var clip_asset = SwfEditorUtils
.LoadOrCreateAsset<SwfClipAsset>(clip_asset_path);
clip_asset.Atlas = asset.Atlas;
clip_asset.FrameRate = asset.Data.FrameRate;
clip_asset.Sequences = LoadClipSequences(asset, symbol);
ConfigureClipSubAssets(clip_asset);
asset.Clips.Add(clip_asset);
}
static void ConfigureClipSubAssets(SwfClipAsset clip_asset) {
SwfEditorUtils.RemoveAllSubAssets(
AssetDatabase.GetAssetPath(clip_asset));
foreach ( var sequence in clip_asset.Sequences ) {
for ( var i = 0; i < sequence.Frames.Count; ++i ) {
var mesh = sequence.Frames[i].Mesh;
mesh.name = sequence.Name + "_" + i.ToString();
var mesh = sequence.Frames[i].Mesh;
mesh.name = string.Format("{0}_{1}", sequence.Name, i);
AssetDatabase.AddObjectToAsset(mesh, clip_asset);
}
}
@@ -219,7 +214,7 @@ namespace FlashTools.Internal {
foreach ( var inst in frame.Instances ) {
var bitmap = inst != null
? FindBitmapFromAnimationData(asset.Data, inst.Bitmap)
? FindBitmapFromAssetData(asset.Data, inst.Bitmap)
: null;
if ( bitmap != null ) {
var width = bitmap.RealSize.x / 20.0f;
@@ -297,7 +292,7 @@ namespace FlashTools.Internal {
break;
default:
throw new UnityException(string.Format(
"SwfAnimationAssetPostprocessor. Incorrect instance type: {0}",
"SwfAssetPostprocessor. Incorrect instance type: {0}",
group.Type));
}
if ( group.Material ) {
@@ -305,7 +300,7 @@ namespace FlashTools.Internal {
baked_materials.Add(group.Material);
} else {
throw new UnityException(string.Format(
"SwfAnimationAssetPostprocessor. Material for baked group ({0}) not found",
"SwfAssetPostprocessor. Material for baked group ({0}) not found",
group.Type));
}
}
@@ -326,7 +321,7 @@ namespace FlashTools.Internal {
Materials = baked_materials.ToArray()};
}
static SwfBitmapData FindBitmapFromAnimationData(SwfAssetData data, int bitmap_id) {
static SwfBitmapData FindBitmapFromAssetData(SwfAssetData data, int bitmap_id) {
for ( var i = 0; i < data.Bitmaps.Count; ++i ) {
var bitmap = data.Bitmaps[i];
if ( bitmap.Id == bitmap_id ) {

View File

@@ -17,39 +17,38 @@ namespace FlashTools.Internal {
string[] moved_assets,
string[] moved_from_asset_paths)
{
var swf_asset_paths = imported_assets
var swf_paths = imported_assets
.Where(p => Path.GetExtension(p).ToLower().Equals(".swf"));
foreach ( var swf_asset_path in swf_asset_paths ) {
SwfAssetProcess(swf_asset_path);
foreach ( var swf_path in swf_paths ) {
SwfFileProcess(swf_path);
}
}
static void SwfAssetProcess(string swf_asset) {
var new_asset_path = SwfEditorUtils.GetSettingsPathFromSwfPath(swf_asset);
var new_asset = AssetDatabase.LoadAssetAtPath<SwfAsset>(new_asset_path);
if ( !new_asset ) {
new_asset = ScriptableObject.CreateInstance<SwfAsset>();
AssetDatabase.CreateAsset(new_asset, new_asset_path);
static void SwfFileProcess(string swf_path) {
var swf_asset_path = Path.ChangeExtension(swf_path, ".asset");
var swf_asset = AssetDatabase.LoadAssetAtPath<SwfAsset>(swf_asset_path);
if ( !swf_asset ) {
swf_asset = ScriptableObject.CreateInstance<SwfAsset>();
AssetDatabase.CreateAsset(swf_asset, swf_asset_path);
}
if ( LoadDataFromSwfFile(swf_asset, new_asset) ) {
EditorUtility.SetDirty(new_asset);
if ( LoadSwfAsset(swf_path, swf_asset) ) {
EditorUtility.SetDirty(swf_asset);
AssetDatabase.SaveAssets();
} else {
SwfEditorUtils.DeleteAssetWithDepends(new_asset);
SwfEditorUtils.DeleteAssetWithDepends(swf_asset);
}
}
static bool LoadDataFromSwfFile(string swf_asset, SwfAsset asset) {
static bool LoadSwfAsset(string swf_path, SwfAsset swf_asset) {
try {
if ( asset.Atlas ) {
if ( swf_asset.Atlas ) {
AssetDatabase.DeleteAsset(
AssetDatabase.GetAssetPath(asset.Atlas));
asset.Atlas = null;
AssetDatabase.GetAssetPath(swf_asset.Atlas));
swf_asset.Atlas = null;
}
asset.Data = LoadDataFromSwfDecoder(
swf_asset.Data = LoadSwfAssetData(
swf_asset,
asset,
new SwfDecoder(swf_asset));
new SwfDecoder(swf_path));
return true;
} catch ( Exception e ) {
Debug.LogErrorFormat("Parsing swf error: {0}", e.Message);
@@ -57,21 +56,21 @@ namespace FlashTools.Internal {
}
}
static SwfAssetData LoadDataFromSwfDecoder(
string swf_asset, SwfAsset asset, SwfDecoder decoder)
static SwfAssetData LoadSwfAssetData(
SwfAsset swf_asset, SwfDecoder swf_decoder)
{
var library = new SwfLibrary();
return new SwfAssetData{
FrameRate = decoder.UncompressedHeader.FrameRate,
Symbols = LoadSymbols(library, decoder),
Bitmaps = LoadBitmaps(swf_asset, asset.Settings, library)};
FrameRate = swf_decoder.UncompressedHeader.FrameRate,
Symbols = LoadSymbols(library, swf_decoder),
Bitmaps = LoadBitmaps(library, swf_asset)};
}
static List<SwfSymbolData> LoadSymbols(
SwfLibrary library, SwfDecoder decoder)
{
var symbols = new List<SwfSymbolData>();
symbols.Add(LoadSymbol("_Stage", library, decoder.Tags));
symbols.Add(LoadSymbol("_Stage_", library, decoder.Tags));
var sprite_defs = library.Defines.Values
.OfType<SwfLibrarySpriteDefine>()
.Where(p => !string.IsNullOrEmpty(p.ExportName));
@@ -90,14 +89,14 @@ namespace FlashTools.Internal {
var executer = new SwfContextExecuter(library, 0);
var symbol_frames = new List<SwfFrameData>();
while ( executer.NextFrame(tags, disp_lst) ) {
symbol_frames.Add(LoadSymbolFrame(library, disp_lst));
symbol_frames.Add(LoadSymbolFrameData(library, disp_lst));
}
return new SwfSymbolData{
Name = symbol_name,
Frames = symbol_frames};
}
static SwfFrameData LoadSymbolFrame(
static SwfFrameData LoadSymbolFrameData(
SwfLibrary library, SwfDisplayList display_list)
{
var frame = new SwfFrameData();
@@ -205,7 +204,7 @@ namespace FlashTools.Internal {
}
static List<SwfBitmapData> LoadBitmaps(
string swf_asset, SwfSettingsData settings, SwfLibrary library)
SwfLibrary library, SwfAsset asset)
{
var bitmap_defines = library.Defines
.Where (p => p.Value.Type == SwfLibraryDefineType.Bitmap)
@@ -215,8 +214,7 @@ namespace FlashTools.Internal {
var textures = bitmap_defines
.Select (p => LoadTextureFromBitmapDefine(p.Value))
.ToArray();
var rects = PackAndSaveBitmapsAtlas(
swf_asset, textures, settings);
var rects = PackAndSaveBitmapsAtlas(asset, textures);
var bitmaps = new List<SwfBitmapData>(bitmap_defines.Length);
for ( var i = 0; i < bitmap_defines.Length; ++i ) {
var bitmap_define = bitmap_defines[i];
@@ -258,11 +256,9 @@ namespace FlashTools.Internal {
public Rect[] Rects;
}
static Rect[] PackAndSaveBitmapsAtlas(
string swf_asset, Texture2D[] textures, SwfSettingsData settings)
{
var atlas_info = PackBitmapsAtlas(textures, settings);
var atlas_path = SwfEditorUtils.GetAtlasPathFromSwfPath(swf_asset);
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);
@@ -276,6 +272,9 @@ namespace FlashTools.Internal {
: 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};

View File

@@ -3,6 +3,7 @@ using UnityEditor;
using System;
using System.IO;
using System.Collections.Generic;
namespace FlashTools.Internal {
public static class SwfEditorUtils {
@@ -50,24 +51,18 @@ namespace FlashTools.Internal {
}
}
public static string GetSwfPathFromSettingsPath(string settings_path) {
return Path.ChangeExtension(settings_path, ".swf");
public static T LoadOrCreateAsset<T>(string asset_path) where T : ScriptableObject {
var asset = AssetDatabase.LoadAssetAtPath<T>(asset_path);
if ( !asset ) {
asset = ScriptableObject.CreateInstance<T>();
AssetDatabase.CreateAsset(asset, asset_path);
}
return asset;
}
public static string GetAtlasPathFromSwfPath(string swf_path) {
return Path.ChangeExtension(swf_path, "._Atlas.png");
}
public static string GetAtlasPathFromSettingsPath(string settings_path) {
return Path.ChangeExtension(settings_path, "._Atlas.png");
}
public static string GetSettingsPathFromSwfPath(string swf_path) {
return Path.ChangeExtension(swf_path, ".asset");
}
public static string GetClipPathFromSettingsPath(string settings_path, string clip_name) {
return Path.ChangeExtension(settings_path, clip_name + ".asset");
public static string GetAtlasPathFromAsset(SwfAsset asset) {
var asset_path = AssetDatabase.GetAssetPath(asset);
return Path.ChangeExtension(asset_path, "._Atlas_.png");
}
}
}

View File

@@ -132,8 +132,7 @@ namespace FlashTools.Internal.SwfEditorTools {
}
property.stringValue = new_value;
property.serializedObject.ApplyModifiedProperties();
}
});
}});
} else {
EditorGUI.LabelField(position, label.text, "Use SwfSortingLayer with string attribute.");
}
@@ -204,11 +203,60 @@ namespace FlashTools.Internal.SwfEditorTools {
EditorGUI.IntPopup(position, property, vnames, values, label);
} else {
EditorGUI.PropertyField(position, property, label, true);
}
});
}});
} else {
EditorGUI.LabelField(position, label.text, "Use SwfPowerOfTwoIf with integer attribute.");
}
}
}
//
// SwfReadOnlyDrawer
//
[CustomPropertyDrawer(typeof(SwfReadOnlyAttribute))]
public class SwfReadOnlyDrawer : PropertyDrawer {
public override void OnGUI(
Rect position, SerializedProperty property, GUIContent label)
{
SwfEditorUtils.DoWithEnabledGUI(false, () => {
EditorGUI.PropertyField(position, property, label, true);
});
}
}
//
// SwfAssetGUIDDrawer
//
[CustomPropertyDrawer(typeof(SwfAssetGUIDAttribute))]
public class SwfAssetGUIDDrawer : PropertyDrawer {
public override void OnGUI(
Rect position, SerializedProperty property, GUIContent label)
{
if ( property.propertyType == SerializedPropertyType.String ) {
var attr = attribute as SwfAssetGUIDAttribute;
SwfEditorUtils.DoWithEnabledGUI(!attr.ReadOnly, () => {
SwfEditorUtils.DoWithMixedValue(
property.hasMultipleDifferentValues, () => {
EditorGUI.BeginChangeCheck();
var asset_path = AssetDatabase.GUIDToAssetPath(property.stringValue);
var asset = AssetDatabase.LoadMainAssetAtPath(asset_path);
var new_asset = EditorGUI.ObjectField(
position, property.displayName, asset, typeof(UnityEngine.Object), false);
if ( EditorGUI.EndChangeCheck() ) {
if ( property.hasMultipleDifferentValues ) {
property.stringValue = "--";
}
var new_asset_path = AssetDatabase.GetAssetPath(new_asset);
property.stringValue = AssetDatabase.AssetPathToGUID(new_asset_path);
property.serializedObject.ApplyModifiedProperties();
}
});
});
} else {
EditorGUI.LabelField(position, label.text, "Use SwfAssetGUID with string attribute.");
}
}
}
}

View File

@@ -32,4 +32,14 @@ namespace FlashTools.Internal {
BoolProp = bool_prop;
}
}
public class SwfReadOnlyAttribute : PropertyAttribute {
}
public class SwfAssetGUIDAttribute : PropertyAttribute {
public bool ReadOnly;
public SwfAssetGUIDAttribute(bool read_only) {
ReadOnly = read_only;
}
}
}

View File

@@ -180,11 +180,11 @@ namespace FlashTools.Internal {
}
public static SwfSettings GetHolder() {
var settings_holder = LoadFirstAssetByFilter<SwfSettings>("t:SwfSettings");
if ( !settings_holder ) {
throw new UnityException("SwfSettings asset not found");
var holder = LoadFirstAssetByFilter<SwfSettings>("t:SwfSettings");
if ( !holder ) {
throw new UnityException("SwfSettings. SwfSettings asset not found");
}
return settings_holder;
return holder;
}
public static SwfSettingsData GetDefault() {

View File

@@ -75,10 +75,15 @@ namespace FlashTools {
}
public class SwfAsset : ScriptableObject {
[HideInInspector]
public SwfAssetData Data;
[SwfReadOnly]
public Texture2D Atlas;
[HideInInspector]
public List<SwfClipAsset> Clips;
[HideInInspector]
public SwfSettingsData Settings;
[HideInInspector]
public SwfSettingsData Overridden;
#if UNITY_EDITOR