settings refactor

This commit is contained in:
2016-08-22 18:22:54 +07:00
parent 64a08b57da
commit c643e36692
9 changed files with 155 additions and 87 deletions

View File

@@ -145,7 +145,7 @@ Camera:
far clip plane: 1000
field of view: 60
orthographic: 1
orthographic size: 2.37
orthographic size: 3
m_Depth: 0
m_CullingMask:
serializedVersion: 2

View File

@@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: cda4fb87dc8624946bdaed871b60f0b8
timeCreated: 1471787769
timeCreated: 1471847086
licenseType: Free
NativeFormatImporter:
userData:

View File

@@ -1,29 +1,56 @@
using UnityEngine;
using UnityEngine.Rendering;
using UnityEditor;
using System;
using System.IO;
namespace FlashTools.Internal {
[CustomEditor(typeof(SwfAnimationAsset))]
public class SwfAnimationAssetEditor : Editor {
SwfAnimationAsset _asset = null;
SwfAnimationAsset _asset = null;
bool _settingsFoldout = false;
void Reconvert() {
void OverriddenSettingsToDefault() {
if ( _asset ) {
_asset.Overridden = SwfConverterSettings.GetDefaultSettings();
}
}
void RevertOverriddenSettings() {
if ( _asset ) {
_asset.Overridden = _asset.Settings;
}
}
void ApplyOverriddenSettings() {
if ( _asset ) {
_asset.Settings = _asset.Overridden;
}
ReconvertAnimation();
}
void ReconvertAnimation() {
if ( _asset && _asset.Atlas ) {
AssetDatabase.DeleteAsset(
AssetDatabase.GetAssetPath(_asset.Atlas));
_asset.Atlas = null;
}
if ( !_asset.OverrideSettings ) {
_asset.OverriddenSettings =
SwfConverterSettings.LoadOrCreate().DefaultSettings;
}
AssetDatabase.ImportAsset(
GetSwfPath(),
ImportAssetOptions.ForceUpdate);
}
void ShowUnappliedDialog() {
var title =
"Unapplied swf animation settings";
var message = string.Format(
"Unapplied swf animation settings for '{0}'",
GetAssetPath());
if ( EditorUtility.DisplayDialog(title, message, "Apply", "Revert") ) {
ApplyOverriddenSettings();
} else {
RevertOverriddenSettings();
}
}
GameObject CreateAnimationGO() {
if ( _asset ) {
var anim_go = new GameObject(_asset.name);
@@ -37,17 +64,19 @@ namespace FlashTools.Internal {
void CreateAnimationPrefab() {
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);
if ( anim_go ) {
var prefab_path = GetPrefabPath();
if ( !string.IsNullOrEmpty(prefab_path) ) {
var prefab = AssetDatabase.LoadMainAssetAtPath(prefab_path);
if ( !prefab ) {
prefab = PrefabUtility.CreateEmptyPrefab(prefab_path);
}
PrefabUtility.ReplacePrefab(
anim_go,
prefab,
ReplacePrefabOptions.ConnectToPrefab);
}
PrefabUtility.ReplacePrefab(
anim_go,
prefab,
ReplacePrefabOptions.ConnectToPrefab);
Undo.RegisterCreatedObjectUndo(anim_go, "Create SwfAnimation");
DestroyImmediate(anim_go);
}
}
@@ -78,10 +107,41 @@ namespace FlashTools.Internal {
: Path.ChangeExtension(asset_path, ".prefab");
}
void DrawGUIControls() {
if ( GUILayout.Button("Reconvert") ) {
Reconvert();
void DrawGUISettings() {
GUI.enabled = false;
EditorGUILayout.PropertyField(serializedObject.FindProperty("Atlas"));
GUI.enabled = true;
_settingsFoldout = EditorGUILayout.Foldout(_settingsFoldout, "Settings");
if ( _settingsFoldout ) {
var it = serializedObject.FindProperty("Overridden");
while ( it.NextVisible(true) ) {
EditorGUILayout.PropertyField(it);
}
DrawGUISettingsControls();
}
}
void DrawGUISettingsControls() {
GUILayout.BeginHorizontal();
{
GUI.enabled = !_asset.Overridden.Equal(SwfConverterSettings.GetDefaultSettings());
if ( GUILayout.Button("Default") ) {
OverriddenSettingsToDefault();
}
GUI.enabled = !_asset.Overridden.Equal(_asset.Settings);
if ( GUILayout.Button("Revert") ) {
RevertOverriddenSettings();
}
GUI.enabled = !_asset.Overridden.Equal(_asset.Settings);
if ( GUILayout.Button("Apply") ) {
ApplyOverriddenSettings();
}
GUI.enabled = true;
}
GUILayout.EndHorizontal();
}
void DrawGUIAnimation() {
GUILayout.BeginHorizontal();
{
if ( GUILayout.Button("Create animation prefab") ) {
@@ -94,24 +154,6 @@ namespace FlashTools.Internal {
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
@@ -119,12 +161,23 @@ namespace FlashTools.Internal {
// ------------------------------------------------------------------------
void OnEnable() {
_asset = target as SwfAnimationAsset;
_asset = target as SwfAnimationAsset;
_settingsFoldout = _asset && !_asset.Settings.Equal(SwfConverterSettings.GetDefaultSettings());
}
void OnDisable() {
if ( _asset && !_asset.Settings.Equal(_asset.Overridden) ) {
ShowUnappliedDialog();
}
}
public override void OnInspectorGUI() {
DrawGUIProperties();
DrawGUIControls();
serializedObject.Update();
DrawGUISettings();
DrawGUIAnimation();
if ( GUI.changed ) {
serializedObject.ApplyModifiedProperties();
}
}
}
}

View File

@@ -51,10 +51,10 @@ namespace FlashTools.Internal {
.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;
atlas_importer.spritePixelsPerUnit = asset.Settings.PixelsPerUnit;
atlas_importer.mipmapEnabled = asset.Settings.GenerateMipMaps;
atlas_importer.filterMode = asset.Settings.AtlasFilterMode;
atlas_importer.textureFormat = asset.Settings.AtlasImporterFormat;
AssetDatabase.ImportAsset(
GetAtlasPath(asset_path),
ImportAssetOptions.ForceUpdate);

View File

@@ -187,8 +187,8 @@ namespace FlashTools.Internal {
var atlas = new Texture2D(0, 0, TextureFormat.ARGB32, false);
var atlas_rects = atlas.PackTextures(
textures,
asset.OverriddenSettings.AtlasPadding,
asset.OverriddenSettings.MaxAtlasSize);
asset.Settings.AtlasPadding,
asset.Settings.MaxAtlasSize);
File.WriteAllBytes(
GetAtlasPath(swf_asset),

View File

@@ -110,9 +110,9 @@ namespace FlashTools {
var matrix =
Matrix4x4.Scale(new Vector3(
1.0f / Asset.OverriddenSettings.PixelsPerUnit,
-1.0f / Asset.OverriddenSettings.PixelsPerUnit,
1.0f / Asset.OverriddenSettings.PixelsPerUnit)) * inst.Matrix;
1.0f / Asset.Settings.PixelsPerUnit,
-1.0f / Asset.Settings.PixelsPerUnit,
1.0f / Asset.Settings.PixelsPerUnit)) * inst.Matrix;
_vertices.Add(matrix.MultiplyPoint3x4(v0));
_vertices.Add(matrix.MultiplyPoint3x4(v1));

View File

@@ -41,45 +41,45 @@ 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 {
public SwfAnimationData Data = new SwfAnimationData();
public Texture2D Atlas = null;
public bool OverrideSettings = false;
public SwfConverterSettings.Settings OverriddenSettings = new SwfConverterSettings.Settings();
public SwfAnimationData Data;
public Texture2D Atlas;
public SwfConverterSettings.Settings Settings;
public SwfConverterSettings.Settings Overridden;
#if UNITY_EDITOR
void Reset() {
Data = new SwfAnimationData();
Atlas = null;
OverrideSettings = false;
OverriddenSettings = SwfConverterSettings.LoadOrCreate().DefaultSettings;
Data = new SwfAnimationData();
Atlas = null;
Settings = SwfConverterSettings.GetDefaultSettings();
Overridden = SwfConverterSettings.GetDefaultSettings();
}
#endif
}

View File

@@ -17,24 +17,38 @@ namespace FlashTools {
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 bool Equal(Settings other) {
return
MaxAtlasSize == other.MaxAtlasSize &&
AtlasPadding == other.AtlasPadding &&
PixelsPerUnit == other.PixelsPerUnit &&
AtlasPowerOfTwo == other.AtlasPowerOfTwo &&
GenerateMipMaps == other.GenerateMipMaps &&
AtlasFilterMode == other.AtlasFilterMode &&
AtlasImporterFormat == other.AtlasImporterFormat;
}
public static Settings identity {
get {
return new Settings{
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();
DefaultSettings = Settings.identity;
}
public static SwfConverterSettings LoadOrCreate() {
public static SwfConverterSettings.Settings GetDefaultSettings() {
var settings_path = GetSettingsPath();
var settings = AssetDatabase.LoadAssetAtPath<SwfConverterSettings>(settings_path);
if ( !settings ) {
@@ -43,7 +57,7 @@ namespace FlashTools {
AssetDatabase.CreateAsset(settings, settings_path);
AssetDatabase.SaveAssets();
}
return settings;
return settings.DefaultSettings;
}
static void CreateAssetDatabaseFolders(string path) {

View File

@@ -1,8 +1,9 @@
<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/SwfAnimationAssetEditor.cs">
<Files>
<File FileName="Assets/FlashTools/Scripts/SwfAnimationAsset.cs" Line="1" Column="1" />
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs" Line="128" Column="18" />
<File FileName="Assets/FlashTools/Scripts/SwfAnimationAsset.cs" Line="86" Column="2" NotebookId="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>