preview for assets and clips

This commit is contained in:
2016-09-10 01:49:19 +07:00
parent 060594dc29
commit 9609e07e5c
9 changed files with 220 additions and 17 deletions

View File

@@ -47,6 +47,7 @@
<ItemGroup>
<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\SwfClipAssetPreview.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" />

View File

@@ -1,7 +1,6 @@
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
@@ -61,7 +60,7 @@ namespace FlashTools.Internal {
//
//
void AllAssetsForeach(Action<SwfAsset> act) {
void AllAssetsForeach(System.Action<SwfAsset> act) {
foreach ( var asset in _assets ) {
act(asset);
}

View File

@@ -142,5 +142,9 @@ namespace FlashTools.Internal {
serializedObject.ApplyModifiedProperties();
}
}
public override bool RequiresConstantRepaint() {
return true;
}
}
}

View File

@@ -0,0 +1,164 @@
using UnityEngine;
using UnityEditor;
using System.Linq;
namespace FlashTools.Internal {
[CustomPreview(typeof(SwfClipAsset))]
public class SwfClipAssetPreview : ObjectPreview {
int _sequenceIndex = 0;
MaterialPropertyBlock _matPropBlock = null;
PreviewRenderUtility _previewUtility = null;
Texture2D targetAtlas {
get {
var clip = target as SwfClipAsset;
return clip.Atlas;
}
}
int targetSequenceCount {
get {
var clip = target as SwfClipAsset;
return clip && clip.Sequences != null
? clip.Sequences.Count
: 0;
}
}
SwfClipAsset.Frame targetFrame {
get {
var clip = target as SwfClipAsset;
return GetFrameForClip(clip, _sequenceIndex);
}
}
SwfClipAsset.Sequence targetSequence {
get {
var clip = target as SwfClipAsset;
return GetSequenceForClip(clip, _sequenceIndex);
}
}
static SwfClipAsset.Frame GetFrameForClip(SwfClipAsset clip, int sequence_index) {
var sequence = GetSequenceForClip(clip, sequence_index);
var frames = sequence != null && sequence.Frames != null && sequence.Frames.Count > 0
? sequence.Frames
: null;
var frame_time = (float)(EditorApplication.timeSinceStartup * clip.FrameRate);
return frames != null
? frames[Mathf.FloorToInt(frame_time) % frames.Count]
: null;
}
static SwfClipAsset.Sequence GetSequenceForClip(SwfClipAsset clip, int sequence_index) {
return clip && clip.Sequences != null && clip.Sequences.Count > 0
? clip.Sequences[Mathf.Abs(sequence_index) % clip.Sequences.Count]
: null;
}
static Bounds CalculateBoundsForSequence(SwfClipAsset.Sequence sequence) {
var bounds = sequence != null && sequence.Frames != null && sequence.Frames.Count > 0
? sequence.Frames
.Where (p => !!p.Mesh)
.Select(p => p.Mesh.bounds)
: new Bounds[0];
var result = bounds.Any() ? bounds.First() : new Bounds();
foreach ( var bound in bounds ) {
result.Encapsulate(bound);
}
return result;
}
static void ConfigureCameraForSequence(Camera camera, SwfClipAsset.Sequence sequence) {
var bounds = CalculateBoundsForSequence(sequence);
camera.orthographic = true;
camera.orthographicSize = Mathf.Max(
Mathf.Abs(bounds.extents.x),
Mathf.Abs(bounds.extents.y));
camera.transform.position = new Vector3(
bounds.center.x,
bounds.center.y,
-10.0f);
}
// ---------------------------------------------------------------------
//
// Functions
//
// ---------------------------------------------------------------------
public void SetCurrentSequence(string sequence_name) {
var clip = target as SwfClipAsset;
_sequenceIndex = clip && clip.Sequences != null
? Mathf.Max(0, clip.Sequences.FindIndex(p => p.Name == sequence_name))
: 0;
}
// ---------------------------------------------------------------------
//
// Messages
//
// ---------------------------------------------------------------------
public override void Initialize(Object[] targets) {
base.Initialize(targets);
_matPropBlock = new MaterialPropertyBlock();
_previewUtility = new PreviewRenderUtility();
}
public override bool HasPreviewGUI() {
return true;
}
public override void OnPreviewSettings() {
var any_multi_sequences = m_Targets
.OfType<SwfClipAsset>()
.Any(p => p.Sequences != null && p.Sequences.Count > 1);
if ( any_multi_sequences && GUILayout.Button("<", EditorStyles.miniButton) ) {
--_sequenceIndex;
}
var sequence_names = m_Targets
.OfType<SwfClipAsset>()
.Select (p => GetSequenceForClip(p, _sequenceIndex))
.Where (p => p != null && !string.IsNullOrEmpty(p.Name))
.Select (p => p.Name)
.ToArray();
var label_text = string.Empty;
for ( int i = 0, e = sequence_names.Length; i < e; ++i ) {
label_text += string.Format(
i < e - 1 ? "{0}, " : "{0}",
sequence_names[i]);
}
GUILayout.Label(label_text, EditorStyles.whiteLabel);
if ( any_multi_sequences && GUILayout.Button(">", EditorStyles.miniButton) ) {
++_sequenceIndex;
}
}
public override void OnPreviewGUI(Rect r, GUIStyle background) {
if ( Event.current.type == EventType.Repaint ) {
var atlas = targetAtlas;
var frame = targetFrame;
var sequence = targetSequence;
if ( atlas && frame != null && sequence != null ) {
_previewUtility.BeginPreview(r, background);
{
_matPropBlock.SetTexture("_MainTex", atlas);
ConfigureCameraForSequence(_previewUtility.m_Camera, sequence);
for ( var i = 0; i < frame.Materials.Length; ++i ) {
_previewUtility.DrawMesh(
frame.Mesh,
Matrix4x4.identity,
frame.Materials[i],
i,
_matPropBlock);
}
_previewUtility.m_Camera.Render();
}
_previewUtility.EndAndDrawPreview(r);
}
}
}
}
}

View File

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

View File

@@ -1,7 +1,6 @@
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;
using System.Collections.Generic;
@@ -10,7 +9,7 @@ namespace FlashTools.Internal {
public class SwfClipControllerEditor : Editor {
List<SwfClipController> _controllers = new List<SwfClipController>();
void AllControllersForeach(Action<SwfClipController> act) {
void AllControllersForeach(System.Action<SwfClipController> act) {
foreach ( var controller in _controllers ) {
act(controller);
}

View File

@@ -1,19 +1,17 @@
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;
using System.Collections.Generic;
namespace FlashTools.Internal {
[CustomEditor(typeof(SwfClip)), CanEditMultipleObjects]
public class SwfClipEditor : Editor {
List<SwfClip> _clips = new List<SwfClip>();
List<SwfClip> _clips = new List<SwfClip>();
Dictionary<SwfClip, SwfClipAssetPreview> _previews = new Dictionary<SwfClip, SwfClipAssetPreview>();
void AllClipsForeachWithUndo(Action<SwfClip> act) {
Undo.RecordObjects(
_clips.ToArray(),
"Inspector");
void AllClipsForeachWithUndo(System.Action<SwfClip> act) {
Undo.RecordObjects(_clips.ToArray(), "Inspector");
foreach ( var clip in _clips ) {
act(clip);
EditorUtility.SetDirty(clip);
@@ -141,9 +139,12 @@ namespace FlashTools.Internal {
// ---------------------------------------------------------------------
void OnEnable() {
_clips = targets
.OfType<SwfClip>()
.ToList();
_clips = targets.OfType<SwfClip>().ToList();
foreach ( var clip in _clips.Where(p => !!p.clip) ) {
var preview = new SwfClipAssetPreview();
preview.Initialize(new Object[]{clip.clip});
_previews.Add(clip, preview);
}
}
public override void OnInspectorGUI() {
@@ -155,5 +156,24 @@ namespace FlashTools.Internal {
serializedObject.ApplyModifiedProperties();
}
}
public override bool RequiresConstantRepaint() {
return _previews.Count > 0;
}
public override bool HasPreviewGUI() {
return _previews.Count > 0;
}
public override void OnPreviewGUI(Rect r, GUIStyle background) {
if ( Event.current.type == EventType.Repaint ) {
SwfClipAssetPreview preview;
var clip = target as SwfClip;
if ( _previews.TryGetValue(clip, out preview) ) {
preview.SetCurrentSequence(clip.sequence);
preview.DrawPreview(r);
}
}
}
}
}

View File

@@ -91,8 +91,7 @@ namespace FlashTools {
_controllers.Clear();
}
void UpdateControllers() {
var dt = Time.deltaTime;
void UpdateControllers(float dt) {
_controllers.AssignTo(_safeUpdates);
for ( int i = 0, e = _safeUpdates.Count; i < e; ++i ) {
var ctrl = _safeUpdates[i];
@@ -119,7 +118,8 @@ namespace FlashTools {
}
void Update() {
UpdateControllers();
var dt = Time.deltaTime;
UpdateControllers(dt);
}
}
}

View File

@@ -1,6 +1,10 @@
<Properties StartupItem="Assembly-CSharp.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" PreferredExecutionTarget="Unity.Instance.Unity Editor" />
<MonoDevelop.Ide.Workbench />
<MonoDevelop.Ide.Workbench>
<Files>
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipAssetPreview.cs" Line="1" Column="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>