mirror of
https://github.com/BlackMATov/unity-flash-tools.git
synced 2025-12-16 22:19:31 +07:00
default convert settings and little refactor
This commit is contained in:
@@ -145,7 +145,7 @@ Camera:
|
||||
far clip plane: 1000
|
||||
field of view: 60
|
||||
orthographic: 1
|
||||
orthographic size: 6
|
||||
orthographic size: 2.37
|
||||
m_Depth: 0
|
||||
m_CullingMask:
|
||||
serializedVersion: 2
|
||||
|
||||
21
Assets/FlashTools/Resources/SwfConverterSettings.asset
Normal file
21
Assets/FlashTools/Resources/SwfConverterSettings.asset
Normal file
@@ -0,0 +1,21 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!114 &11400000
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_PrefabParentObject: {fileID: 0}
|
||||
m_PrefabInternal: {fileID: 0}
|
||||
m_GameObject: {fileID: 0}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 6da591d87f967451298a275621e1fd5d, type: 3}
|
||||
m_Name: SwfConverterSettings
|
||||
m_EditorClassIdentifier:
|
||||
DefaultSettings:
|
||||
MaxAtlasSize: 1024
|
||||
AtlasPadding: 1
|
||||
PixelsPerUnit: 100
|
||||
AtlasPowerOfTwo: 0
|
||||
GenerateMipMaps: 1
|
||||
AtlasFilterMode: 1
|
||||
AtlasImporterFormat: -3
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cda4fb87dc8624946bdaed871b60f0b8
|
||||
timeCreated: 1471787769
|
||||
licenseType: Free
|
||||
NativeFormatImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -9,38 +9,109 @@ namespace FlashTools.Internal {
|
||||
public class SwfAnimationAssetEditor : Editor {
|
||||
SwfAnimationAsset _asset = null;
|
||||
|
||||
static void ApplySettings(SwfAnimationAsset asset) {
|
||||
if ( asset.Atlas ) {
|
||||
void Reconvert() {
|
||||
if ( _asset && _asset.Atlas ) {
|
||||
AssetDatabase.DeleteAsset(
|
||||
AssetDatabase.GetAssetPath(asset.Atlas));
|
||||
asset.Atlas = null;
|
||||
AssetDatabase.GetAssetPath(_asset.Atlas));
|
||||
_asset.Atlas = null;
|
||||
}
|
||||
if ( !_asset.OverrideSettings ) {
|
||||
_asset.OverriddenSettings =
|
||||
SwfConverterSettings.LoadOrCreate().DefaultSettings;
|
||||
}
|
||||
var swf_asset_path = Path.ChangeExtension(
|
||||
AssetDatabase.GetAssetPath(asset), "swf");
|
||||
AssetDatabase.ImportAsset(
|
||||
swf_asset_path,
|
||||
ImportAssetOptions.ForceUncompressedImport);
|
||||
GetSwfPath(),
|
||||
ImportAssetOptions.ForceUpdate);
|
||||
}
|
||||
|
||||
GameObject CreateAnimationGO() {
|
||||
if ( _asset ) {
|
||||
var anim_go = new GameObject(_asset.name);
|
||||
anim_go.AddComponent<MeshFilter>();
|
||||
anim_go.AddComponent<MeshRenderer>();
|
||||
anim_go.AddComponent<SwfAnimation>().Asset = _asset;
|
||||
return anim_go;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void CreateAnimationPrefab() {
|
||||
//TODO: IMPLME
|
||||
var anim_go = CreateAnimationGO();
|
||||
var prefab_path = GetPrefabPath();
|
||||
if ( anim_go && !string.IsNullOrEmpty(prefab_path) ) {
|
||||
var prefab = AssetDatabase.LoadMainAssetAtPath(prefab_path);
|
||||
if ( !prefab ) {
|
||||
prefab = PrefabUtility.CreateEmptyPrefab(prefab_path);
|
||||
}
|
||||
PrefabUtility.ReplacePrefab(
|
||||
anim_go,
|
||||
prefab,
|
||||
ReplacePrefabOptions.ConnectToPrefab);
|
||||
Undo.RegisterCreatedObjectUndo(anim_go, "Create SwfAnimation");
|
||||
}
|
||||
}
|
||||
|
||||
void CreateAnimationOnScene() {
|
||||
if ( _asset ) {
|
||||
var anim_go = new GameObject(_asset.name);
|
||||
var mesh_renderer = anim_go.AddComponent<MeshRenderer>();
|
||||
mesh_renderer.sharedMaterial = null;
|
||||
mesh_renderer.useLightProbes = false;
|
||||
mesh_renderer.receiveShadows = false;
|
||||
mesh_renderer.shadowCastingMode = ShadowCastingMode.Off;
|
||||
mesh_renderer.reflectionProbeUsage = ReflectionProbeUsage.Off;
|
||||
anim_go.AddComponent<MeshFilter>().sharedMesh = null;
|
||||
anim_go.AddComponent<SwfAnimation>().Asset = _asset;
|
||||
var anim_go = CreateAnimationGO();
|
||||
if ( anim_go ) {
|
||||
Undo.RegisterCreatedObjectUndo(anim_go, "Create SwfAnimation");
|
||||
}
|
||||
}
|
||||
|
||||
string GetAssetPath() {
|
||||
return _asset
|
||||
? AssetDatabase.GetAssetPath(_asset)
|
||||
: string.Empty;
|
||||
}
|
||||
|
||||
string GetSwfPath() {
|
||||
var asset_path = GetAssetPath();
|
||||
return string.IsNullOrEmpty(asset_path)
|
||||
? string.Empty
|
||||
: Path.ChangeExtension(asset_path, ".swf");
|
||||
}
|
||||
|
||||
string GetPrefabPath() {
|
||||
var asset_path = GetAssetPath();
|
||||
return string.IsNullOrEmpty(asset_path)
|
||||
? string.Empty
|
||||
: Path.ChangeExtension(asset_path, ".prefab");
|
||||
}
|
||||
|
||||
void DrawGUIControls() {
|
||||
if ( GUILayout.Button("Reconvert") ) {
|
||||
Reconvert();
|
||||
}
|
||||
GUILayout.BeginHorizontal();
|
||||
{
|
||||
if ( GUILayout.Button("Create animation prefab") ) {
|
||||
CreateAnimationPrefab();
|
||||
}
|
||||
if ( GUILayout.Button("Create animation on scene") ) {
|
||||
CreateAnimationOnScene();
|
||||
}
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
}
|
||||
|
||||
void DrawGUIProperties() {
|
||||
serializedObject.Update();
|
||||
EditorGUILayout.PropertyField(serializedObject.FindProperty("Atlas"));
|
||||
EditorGUILayout.PropertyField(serializedObject.FindProperty("OverrideSettings"));
|
||||
if ( _asset.OverrideSettings ) {
|
||||
EditorGUILayout.PropertyField(
|
||||
serializedObject.FindProperty("OverriddenSettings"), true);
|
||||
} else {
|
||||
GUI.enabled = false;
|
||||
var so = new SerializedObject(SwfConverterSettings.LoadOrCreate());
|
||||
EditorGUILayout.PropertyField(so.FindProperty("DefaultSettings"), true);
|
||||
GUI.enabled = true;
|
||||
}
|
||||
if ( GUI.changed ) {
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
//
|
||||
// Messages
|
||||
@@ -52,18 +123,8 @@ namespace FlashTools.Internal {
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI() {
|
||||
DrawDefaultInspector();
|
||||
if ( GUILayout.Button("Apply settings") ) {
|
||||
ApplySettings(_asset);
|
||||
}
|
||||
GUILayout.BeginHorizontal();
|
||||
if ( GUILayout.Button("Create animation prefab") ) {
|
||||
CreateAnimationPrefab();
|
||||
}
|
||||
if ( GUILayout.Button("Create animation on scene") ) {
|
||||
CreateAnimationOnScene();
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
DrawGUIProperties();
|
||||
DrawGUIControls();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace FlashTools.Internal {
|
||||
public class SwfAnimationAssetPostprocessor : AssetPostprocessor {
|
||||
@@ -39,32 +38,31 @@ namespace FlashTools.Internal {
|
||||
}
|
||||
|
||||
static void ConfigureAtlas(string asset_path, SwfAnimationAsset asset) {
|
||||
var meta_data = new List<SpriteMetaData>();
|
||||
var atlas_importer = GetBitmapsAtlasImporter(asset_path);
|
||||
var atlas_size = GetSizeFromTextureImporter(atlas_importer);
|
||||
var unique_bitmaps = asset.Data.Bitmaps;
|
||||
foreach ( var bitmap_data in unique_bitmaps ) {
|
||||
var meta_elem = new SpriteMetaData();
|
||||
meta_elem.name = bitmap_data.Id.ToString();
|
||||
meta_elem.rect = new Rect(
|
||||
bitmap_data.SourceRect.xMin * atlas_size.x,
|
||||
bitmap_data.SourceRect.yMin * atlas_size.y,
|
||||
bitmap_data.SourceRect.width * atlas_size.x,
|
||||
bitmap_data.SourceRect.height * atlas_size.y);
|
||||
meta_data.Add(meta_elem);
|
||||
atlas_importer.spritesheet = meta_data.ToArray();
|
||||
atlas_importer.textureType = TextureImporterType.Sprite;
|
||||
atlas_importer.spriteImportMode = SpriteImportMode.Multiple;
|
||||
atlas_importer.spritePixelsPerUnit = asset.PixelsPerUnit;
|
||||
AssetDatabase.ImportAsset(
|
||||
GetAtlasPath(asset_path),
|
||||
ImportAssetOptions.ForceUncompressedImport);
|
||||
}
|
||||
atlas_importer.spritesheet = asset.Data.Bitmaps
|
||||
.Select(bitmap => new SpriteMetaData{
|
||||
name = bitmap.Id.ToString(),
|
||||
rect = new Rect(
|
||||
bitmap.SourceRect.xMin * atlas_size.x,
|
||||
bitmap.SourceRect.yMin * atlas_size.y,
|
||||
bitmap.SourceRect.width * atlas_size.x,
|
||||
bitmap.SourceRect.height * atlas_size.y)})
|
||||
.ToArray();
|
||||
atlas_importer.textureType = TextureImporterType.Sprite;
|
||||
atlas_importer.spriteImportMode = SpriteImportMode.Multiple;
|
||||
atlas_importer.spritePixelsPerUnit = asset.OverriddenSettings.PixelsPerUnit;
|
||||
atlas_importer.mipmapEnabled = asset.OverriddenSettings.GenerateMipMaps;
|
||||
atlas_importer.filterMode = asset.OverriddenSettings.AtlasFilterMode;
|
||||
atlas_importer.textureFormat = asset.OverriddenSettings.AtlasImporterFormat;
|
||||
AssetDatabase.ImportAsset(
|
||||
GetAtlasPath(asset_path),
|
||||
ImportAssetOptions.ForceUpdate);
|
||||
}
|
||||
|
||||
static TextureImporter GetBitmapsAtlasImporter(string asset_path) {
|
||||
var atlas_path = GetAtlasPath(asset_path);
|
||||
var importer = AssetImporter.GetAtPath(atlas_path) as TextureImporter;
|
||||
var importer = AssetImporter.GetAtPath(atlas_path) as TextureImporter;
|
||||
if ( !importer ) {
|
||||
throw new UnityException(string.Format(
|
||||
"atlas texture importer not found ({0})",
|
||||
@@ -85,4 +83,4 @@ namespace FlashTools.Internal {
|
||||
return Path.ChangeExtension(asset_path, ".png");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -181,11 +181,14 @@ namespace FlashTools.Internal {
|
||||
.ToArray();
|
||||
|
||||
var textures = bitmap_defines
|
||||
.Select(p => LoadTextureFromBitmapDefine(p.Value));
|
||||
.Select (p => LoadTextureFromBitmapDefine(p.Value))
|
||||
.ToArray();
|
||||
|
||||
var atlas = new Texture2D(0, 0, TextureFormat.ARGB32, false);
|
||||
var atlas_rects = atlas.PackTextures(
|
||||
textures.ToArray(), asset.AtlasPadding, asset.MaxAtlasSize);
|
||||
textures,
|
||||
asset.OverriddenSettings.AtlasPadding,
|
||||
asset.OverriddenSettings.MaxAtlasSize);
|
||||
|
||||
File.WriteAllBytes(
|
||||
GetAtlasPath(swf_asset),
|
||||
@@ -193,7 +196,7 @@ namespace FlashTools.Internal {
|
||||
GameObject.DestroyImmediate(atlas, true);
|
||||
AssetDatabase.ImportAsset(
|
||||
GetAtlasPath(swf_asset),
|
||||
ImportAssetOptions.ForceUncompressedImport);
|
||||
ImportAssetOptions.ForceUpdate);
|
||||
|
||||
var bitmaps = new List<SwfAnimationBitmapData>();
|
||||
for ( var i = 0; i < bitmap_defines.Length; ++i ) {
|
||||
|
||||
@@ -25,4 +25,4 @@
|
||||
return tag;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,9 +110,9 @@ namespace FlashTools {
|
||||
|
||||
var matrix =
|
||||
Matrix4x4.Scale(new Vector3(
|
||||
1.0f / Asset.PixelsPerUnit,
|
||||
-1.0f / Asset.PixelsPerUnit,
|
||||
1.0f / Asset.PixelsPerUnit)) * inst.Matrix;
|
||||
1.0f / Asset.OverriddenSettings.PixelsPerUnit,
|
||||
-1.0f / Asset.OverriddenSettings.PixelsPerUnit,
|
||||
1.0f / Asset.OverriddenSettings.PixelsPerUnit)) * inst.Matrix;
|
||||
|
||||
_vertices.Add(matrix.MultiplyPoint3x4(v0));
|
||||
_vertices.Add(matrix.MultiplyPoint3x4(v1));
|
||||
|
||||
@@ -7,32 +7,28 @@ namespace FlashTools {
|
||||
public Vector4 Mul;
|
||||
public Vector4 Add;
|
||||
|
||||
public SwfAnimationColorTransform(Vector4 Mul, Vector4 Add) {
|
||||
this.Mul = Mul;
|
||||
this.Add = Add;
|
||||
}
|
||||
|
||||
public static SwfAnimationColorTransform identity {
|
||||
get {
|
||||
return new SwfAnimationColorTransform(
|
||||
new Vector4(1,1,1,1),
|
||||
new Vector4(0,0,0,0));
|
||||
return new SwfAnimationColorTransform{
|
||||
Mul = Vector4.one,
|
||||
Add = Vector4.zero};
|
||||
}
|
||||
}
|
||||
|
||||
public static SwfAnimationColorTransform operator*(
|
||||
SwfAnimationColorTransform a, SwfAnimationColorTransform b)
|
||||
{
|
||||
var res = new SwfAnimationColorTransform();
|
||||
res.Mul.x = b.Mul.x * a.Mul.x;
|
||||
res.Mul.y = b.Mul.y * a.Mul.y;
|
||||
res.Mul.z = b.Mul.z * a.Mul.z;
|
||||
res.Mul.w = b.Mul.w * a.Mul.w;
|
||||
res.Add.x = b.Add.x * a.Mul.x + a.Add.x;
|
||||
res.Add.y = b.Add.y * a.Mul.y + a.Add.y;
|
||||
res.Add.z = b.Add.z * a.Mul.z + a.Add.z;
|
||||
res.Add.w = b.Add.w * a.Mul.w + a.Add.w;
|
||||
return res;
|
||||
return new SwfAnimationColorTransform{
|
||||
Mul = new Vector4(
|
||||
b.Mul.x * a.Mul.x,
|
||||
b.Mul.y * a.Mul.y,
|
||||
b.Mul.z * a.Mul.z,
|
||||
b.Mul.w * a.Mul.w),
|
||||
Add = new Vector4(
|
||||
b.Add.x * a.Mul.x + a.Add.x,
|
||||
b.Add.y * a.Mul.y + a.Add.y,
|
||||
b.Add.z * a.Mul.z + a.Add.z,
|
||||
b.Add.w * a.Mul.w + a.Add.w)};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,39 +41,46 @@ namespace FlashTools {
|
||||
|
||||
[System.Serializable]
|
||||
public class SwfAnimationInstanceData {
|
||||
public SwfAnimationInstanceType Type = SwfAnimationInstanceType.Group;
|
||||
public ushort ClipDepth = 0;
|
||||
public ushort Bitmap = 0;
|
||||
public Matrix4x4 Matrix = Matrix4x4.identity;
|
||||
public SwfAnimationColorTransform ColorTransform = SwfAnimationColorTransform.identity;
|
||||
public SwfAnimationInstanceType Type = SwfAnimationInstanceType.Group;
|
||||
public ushort ClipDepth = 0;
|
||||
public ushort Bitmap = 0;
|
||||
public Matrix4x4 Matrix = Matrix4x4.identity;
|
||||
public SwfAnimationColorTransform ColorTransform = SwfAnimationColorTransform.identity;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SwfAnimationFrameData {
|
||||
public string Name = string.Empty;
|
||||
public List<SwfAnimationInstanceData> Instances = new List<SwfAnimationInstanceData>();
|
||||
public string Name = string.Empty;
|
||||
public List<SwfAnimationInstanceData> Instances = new List<SwfAnimationInstanceData>();
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SwfAnimationBitmapData {
|
||||
public int Id = 0;
|
||||
public Vector2 RealSize = Vector2.zero;
|
||||
public Rect SourceRect = new Rect();
|
||||
public int Id = 0;
|
||||
public Vector2 RealSize = Vector2.zero;
|
||||
public Rect SourceRect = new Rect();
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SwfAnimationData {
|
||||
public float FrameRate = 0.0f;
|
||||
public List<SwfAnimationFrameData> Frames = new List<SwfAnimationFrameData>();
|
||||
public List<SwfAnimationBitmapData> Bitmaps = new List<SwfAnimationBitmapData>();
|
||||
public float FrameRate = 0.0f;
|
||||
public List<SwfAnimationFrameData> Frames = new List<SwfAnimationFrameData>();
|
||||
public List<SwfAnimationBitmapData> Bitmaps = new List<SwfAnimationBitmapData>();
|
||||
}
|
||||
|
||||
public class SwfAnimationAsset : ScriptableObject {
|
||||
//[HideInInspector]
|
||||
public SwfAnimationData Data = new SwfAnimationData();
|
||||
public Texture2D Atlas = null;
|
||||
public int MaxAtlasSize = 1024;
|
||||
public int AtlasPadding = 1;
|
||||
public int PixelsPerUnit = 100;
|
||||
public SwfAnimationData Data = new SwfAnimationData();
|
||||
public Texture2D Atlas = null;
|
||||
public bool OverrideSettings = false;
|
||||
public SwfConverterSettings.Settings OverriddenSettings = new SwfConverterSettings.Settings();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void Reset() {
|
||||
Data = new SwfAnimationData();
|
||||
Atlas = null;
|
||||
OverrideSettings = false;
|
||||
OverriddenSettings = SwfConverterSettings.LoadOrCreate().DefaultSettings;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
64
Assets/FlashTools/Scripts/SwfConverterSettings.cs
Normal file
64
Assets/FlashTools/Scripts/SwfConverterSettings.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using UnityEngine;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace FlashTools {
|
||||
public class SwfConverterSettings : ScriptableObject {
|
||||
[System.Serializable]
|
||||
public struct Settings {
|
||||
public int MaxAtlasSize;
|
||||
public int AtlasPadding;
|
||||
public int PixelsPerUnit;
|
||||
public bool AtlasPowerOfTwo;
|
||||
public bool GenerateMipMaps;
|
||||
public FilterMode AtlasFilterMode;
|
||||
public TextureImporterFormat AtlasImporterFormat;
|
||||
|
||||
public void Reset() {
|
||||
MaxAtlasSize = 1024;
|
||||
AtlasPadding = 1;
|
||||
PixelsPerUnit = 100;
|
||||
AtlasPowerOfTwo = false;
|
||||
GenerateMipMaps = true;
|
||||
AtlasFilterMode = FilterMode.Bilinear;
|
||||
AtlasImporterFormat = TextureImporterFormat.AutomaticTruecolor;
|
||||
}
|
||||
}
|
||||
public Settings DefaultSettings;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void Reset() {
|
||||
DefaultSettings.Reset();
|
||||
}
|
||||
|
||||
public static SwfConverterSettings LoadOrCreate() {
|
||||
var settings_path = GetSettingsPath();
|
||||
var settings = AssetDatabase.LoadAssetAtPath<SwfConverterSettings>(settings_path);
|
||||
if ( !settings ) {
|
||||
settings = ScriptableObject.CreateInstance<SwfConverterSettings>();
|
||||
CreateAssetDatabaseFolders(Path.GetDirectoryName(settings_path));
|
||||
AssetDatabase.CreateAsset(settings, settings_path);
|
||||
AssetDatabase.SaveAssets();
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
|
||||
static void CreateAssetDatabaseFolders(string path) {
|
||||
if ( !AssetDatabase.IsValidFolder(path) ) {
|
||||
var parent = Path.GetDirectoryName(path);
|
||||
if ( !string.IsNullOrEmpty(parent) ) {
|
||||
CreateAssetDatabaseFolders(parent);
|
||||
}
|
||||
AssetDatabase.CreateFolder(parent, Path.GetFileName(path));
|
||||
}
|
||||
}
|
||||
|
||||
static string GetSettingsPath() {
|
||||
return "Assets/FlashTools/Resources/SwfConverterSettings.asset";
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
12
Assets/FlashTools/Scripts/SwfConverterSettings.cs.meta
Normal file
12
Assets/FlashTools/Scripts/SwfConverterSettings.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6da591d87f967451298a275621e1fd5d
|
||||
timeCreated: 1471761453
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user