Skip to content

Commit

Permalink
Merge pull request #32 from loodakrawa/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
loodakrawa committed Dec 26, 2015
2 parents c9450f4 + a9ecb1c commit 24a0edf
Show file tree
Hide file tree
Showing 34 changed files with 650 additions and 140 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
##### 1.2.0
* Add support for animation data providers
* Fix bug that caused sprite color to reset on every frame
* Use sortOrder instead of z coordinate for z-index
* Add ability to choose sorting layer

##### 1.1.2
* Reduce garbage generation to 0

##### 1.1.1
* Unity importer retains components during prefab replace
* Add sprite swapping capabilities

##### 1.1.0
* Add flags to control Metadata calculations
* Unity importer retains transform values during prefab replace
* Fix bug when Animation has no Timelines
* Fix bug when Entity has no ObjectInfos

##### 1.0.4
* Fix SpriterImporter to pass prefab instead of destroyed tmp object

Expand Down
Binary file not shown.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class ChildData
[ExecuteInEditMode]
public class SpriterDotNetBehaviour : MonoBehaviour
{
[HideInInspector]
public string SortingLayer;

[HideInInspector]
public ChildData ChildData;

Expand Down Expand Up @@ -58,6 +61,8 @@ public void Update()
#endif

if (Animator == null) return;

Animator.SortingLayer = SortingLayer;
Animator.Step(Time.deltaTime * 1000.0f);

if (UseNativeTags)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2015 The original author or authors
//
// This software may be modified and distributed under the terms
// of the zlib license. See the LICENSE file for details.

#if UNITY_EDITOR

using System;
using System.Reflection;
using UnityEditor;
using UnityEditorInternal;

namespace SpriterDotNetUnity
{
[CustomEditor(typeof(SpriterDotNetBehaviour))]
public class SpriterDotNetBehaviourEditor : Editor
{
string[] GetSortingLayerNames()
{
Type internalEditorUtilityType = typeof(InternalEditorUtility);
PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
return (string[])sortingLayersProperty.GetValue(null, new object[0]);
}

public override void OnInspectorGUI()
{
DrawDefaultInspector();

SpriterDotNetBehaviour sdnb = target as SpriterDotNetBehaviour;

string[] layers = GetSortingLayerNames();
int currentIndex = Array.IndexOf(layers, sdnb.SortingLayer);
if (currentIndex < 0) currentIndex = 0;
int choiceIndex = EditorGUILayout.Popup("Sorting Layer", currentIndex, layers);
sdnb.SortingLayer = layers[choiceIndex];
EditorUtility.SetDirty(target);
}
}
}

#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 37 additions & 6 deletions SpriterDotNet.Unity/Assets/SpriterDotNet/SpriterImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public class SpriterImporter : AssetPostprocessor
{
private static readonly string AutosaveExtension = ".autosave.scml";
private static readonly string[] ScmlExtensions = new string[] { ".scml" };
private static readonly string ObjectNameSprites = "Sprites";
private static readonly string ObjectNameMetadata = "Metadata";

public static float DeltaZ = -0.001f;
public static bool UseNativeTags = true;

public static event Action<SpriterEntity, GameObject> EntityImported = (e, p) => { };
Expand Down Expand Up @@ -51,8 +52,8 @@ private static void CreateSpriter(string path)
foreach (SpriterEntity entity in spriter.Entities)
{
GameObject go = new GameObject(entity.Name);
GameObject sprites = new GameObject("Sprites");
GameObject metadata = new GameObject("Metadata");
GameObject sprites = new GameObject(ObjectNameSprites);
GameObject metadata = new GameObject(ObjectNameMetadata);

SpriterDotNetBehaviour behaviour = go.AddComponent<SpriterDotNetBehaviour>();
behaviour.UseNativeTags = UseNativeTags;
Expand Down Expand Up @@ -94,17 +95,47 @@ private static SpriterData CreateSpriterData(Spriter spriter, string rootFolder,
private static GameObject CreatePrefab(GameObject go, string folder)
{
string prefabPath = folder + "/" + go.name + ".prefab";
GameObject existing = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
GameObject existingPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);

GameObject prefab;
if (existing != null) prefab = PrefabUtility.ReplacePrefab(go, existing, ReplacePrefabOptions.Default);
if (existingPrefab != null) prefab = ReplacePrefab(go, existingPrefab, prefabPath);
else prefab = PrefabUtility.CreatePrefab(prefabPath, go, ReplacePrefabOptions.Default);

GameObject.DestroyImmediate(go);

return prefab;
}

private static GameObject ReplacePrefab(GameObject go, GameObject prefab, string path)
{
GameObject existing = GameObject.Instantiate(prefab);
MoveChild(go, existing, ObjectNameSprites);
MoveChild(go, existing, ObjectNameMetadata);

SpriterDotNetBehaviour sdnbNew = go.GetComponent<SpriterDotNetBehaviour>();
SpriterDotNetBehaviour sdnbExisting = existing.GetComponent<SpriterDotNetBehaviour>();
sdnbExisting.ChildData = sdnbNew.ChildData;
sdnbExisting.EntityIndex = sdnbNew.EntityIndex;
sdnbExisting.SpriterData = sdnbNew.SpriterData;
sdnbExisting.UseNativeTags = sdnbNew.UseNativeTags;

GameObject createdPrefab = PrefabUtility.ReplacePrefab(existing, prefab, ReplacePrefabOptions.Default);
GameObject.DestroyImmediate(existing);
return createdPrefab;
}

private static void MoveChild(GameObject from, GameObject to, string name)
{
Transform toChild = to.transform.Find(name);
GameObject.DestroyImmediate(toChild.gameObject);

Transform fromChild = from.transform.Find(name);
fromChild.SetParent(to.transform);
fromChild.localPosition = Vector3.zero;
fromChild.localRotation = Quaternion.identity;
fromChild.localScale = Vector3.one;
}

private static void CreateSprites(SpriterEntity entity, ChildData cd, Spriter spriter, GameObject parent)
{
int maxObjects = GetDrawablesCount(entity);
Expand All @@ -123,7 +154,7 @@ private static void CreateSprites(SpriterEntity entity, ChildData cd, Spriter sp
cd.SpritePivots[i] = pivot;
cd.Sprites[i] = child;

child.transform.localPosition = new Vector3(0, 0, DeltaZ * i);
child.transform.localPosition = Vector3.zero;

child.AddComponent<SpriteRenderer>();
}
Expand Down
17 changes: 16 additions & 1 deletion SpriterDotNet.Unity/Assets/SpriterDotNet/UnitySpriterAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
// of the zlib license. See the LICENSE file for details.

using SpriterDotNet;
using System.Collections.Generic;
using UnityEngine;

namespace SpriterDotNetUnity
{
public class UnitySpriterAnimator : SpriterAnimator<Sprite, AudioClip>
{
public string SortingLayer { get; set; }

private const float DefaultPPU = 100.0f;
private const float DefaultPivot = 0.5f;

Expand Down Expand Up @@ -78,12 +81,16 @@ protected override void ApplySpriteTransform(Sprite sprite, SpriterObject info)
float deltaX = (spritePivotX - info.PivotX) * size.x * info.ScaleX;
float deltaY = (spritePivotY - info.PivotY) * size.y * info.ScaleY;

renderer.color = new Color(1.0f, 1.0f, 1.0f, info.Alpha);
Color c = renderer.color;
renderer.color = new Color(c.r, c.g, c.b, info.Alpha);
pivot.transform.localEulerAngles = new Vector3(0, 0, info.Angle);
pivot.transform.localPosition = new Vector3(info.X / ppu, info.Y / ppu, 0);
child.transform.localPosition = new Vector3(deltaX, deltaY, child.transform.localPosition.z);
child.transform.localScale = new Vector3(info.ScaleX, info.ScaleY, 1);

renderer.sortingLayerName = SortingLayer;
renderer.sortingOrder = index;

++index;
}

Expand Down Expand Up @@ -135,5 +142,13 @@ protected override void PlaySound(AudioClip sound, SpriterSound info)
audioSource.panStereo = info.Panning;
audioSource.PlayOneShot(sound, info.Volume);
}

/// <summary>
/// In order to compile on console with AOT-Only, we must provide public definitions for some of the generic classes used internally.
/// </summary>
public Dictionary<int, Sprite> aot_SpritesByInt = new Dictionary<int, Sprite>();
public Dictionary<int, IDictionary<int, Sprite>> aot_SpriteByIntNested = new Dictionary<int, IDictionary<int, Sprite>>();
public Dictionary<int, AudioClip> aot_AudioByInt = new Dictionary<int, AudioClip>();
public Dictionary<int, IDictionary<int, AudioClip>> aot_AudioByIntNested = new Dictionary<int, IDictionary<int, AudioClip>>();
}
}
Binary file modified SpriterDotNet.Unity/Assets/SpriterDotNetExamples/GreyGuy.unity
Binary file not shown.
Binary file not shown.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file modified SpriterDotNet.Unity/Assets/SpriterDotNetExamples/Squares.unity
Binary file not shown.
Binary file not shown.
Binary file modified SpriterDotNet.Unity/SpriterDotNet.Unity.Examples.unitypackage
Binary file not shown.
Binary file modified SpriterDotNet.Unity/SpriterDotNet.Unity.unitypackage
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2015 The original author or authors
//
// This software may be modified and distributed under the terms
// of the zlib license. See the LICENSE file for details.

namespace SpriterDotNet.AnimationDataProvider
{
public class DefaultAnimationDataProvider : IAnimationDataProvider
{
private readonly FrameData data = new FrameData();
private readonly FrameMetadata metadata = new FrameMetadata();

public virtual FrameData GetFrameData(float time, float deltaTime, float factor, SpriterAnimation first, SpriterAnimation second = null)
{
data.Clear();

if (second == null)
{
SpriterProcessor.UpdateFrameData(data, first, time);
}
else
{
SpriterProcessor.UpdateFrameData(data, first, second, time, factor);
}

return data;
}

public virtual FrameMetadata GetFrameMetadata(float time, float deltaTime, float factor, SpriterAnimation first, SpriterAnimation second = null)
{
metadata.Clear();

if (second == null)
{
SpriterProcessor.UpdateFrameMetadata(metadata, first, time, deltaTime);
}
else
{
SpriterProcessor.GetFrameMetadata(metadata, first, second, time, deltaTime, factor);
}

return metadata;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2015 The original author or authors
//
// This software may be modified and distributed under the terms
// of the zlib license. See the LICENSE file for details.

using System;
using System.Collections.Generic;

namespace SpriterDotNet.AnimationDataProvider
{
public class SnapshotAnimationDataProvider : DefaultAnimationDataProvider
{
public static IDictionary<string, FrameData[]> GetData(SpriterEntity entity, int interval)
{
IDictionary<string, FrameData[]> data = new Dictionary<string, FrameData[]>();

foreach (SpriterAnimation anim in entity.Animations)
{
int length = (int)Math.Ceiling(anim.Length / interval);
FrameData[] animData = new FrameData[length];

for (int i = 0; i < animData.Length; ++i)
{
float time = i * interval;
if (time > anim.Length) time = anim.Length;

FrameData fd = new FrameData();
SpriterProcessor.UpdateFrameData(fd, anim, time);
animData[i] = fd;
}

data[anim.Name] = animData;
}

return data;
}

private readonly IDictionary<string, FrameData[]> data;

public SnapshotAnimationDataProvider(IDictionary<string, FrameData[]> data)
{
this.data = data;
}

public override FrameData GetFrameData(float time, float deltaTime, float factor, SpriterAnimation first, SpriterAnimation second = null)
{
if (second != null) return base.GetFrameData(time, deltaTime, factor, first, second);

FrameData[] animData = data[first.Name];
int index = (int)(time / first.Length * animData.Length);
if (index == animData.Length) index = animData.Length - 1;
return animData[index];
}
}
}
11 changes: 9 additions & 2 deletions SpriterDotNet/FrameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ namespace SpriterDotNet
public class FrameData
{
public List<SpriterObject> SpriteData { get; private set; }
public IDictionary<string, SpriterObject> PointData { get; private set; }
public IDictionary<int, SpriterObject> BoxData { get; private set; }
public Dictionary<string, SpriterObject> PointData { get; private set; }
public Dictionary<int, SpriterObject> BoxData { get; private set; }

public FrameData()
{
SpriteData = new List<SpriterObject>();
PointData = new Dictionary<string, SpriterObject>();
BoxData = new Dictionary<int, SpriterObject>();
}

public void Clear()
{
SpriterObjectPool.ReturnChildren(SpriteData);
SpriterObjectPool.ReturnChildren(PointData);
SpriterObjectPool.ReturnChildren(BoxData);
}
}
}
Loading

0 comments on commit 24a0edf

Please sign in to comment.