Created
December 17, 2016 01:16
-
-
Save pharan/bacf25177c1d31580d3972430285c442 to your computer and use it in GitHub Desktop.
Test code for spine-unity threaded loading of Spine Skeleton json and binary data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
* Spine Runtimes Software License v2.5 | |
* | |
* Copyright (c) 2013-2016, Esoteric Software | |
* All rights reserved. | |
* | |
* You are granted a perpetual, non-exclusive, non-sublicensable, and | |
* non-transferable license to use, install, execute, and perform the Spine | |
* Runtimes software and derivative works solely for personal or internal | |
* use. Without the written permission of Esoteric Software (see Section 2 of | |
* the Spine Software License Agreement), you may not (a) modify, translate, | |
* adapt, or develop new applications using the Spine Runtimes or otherwise | |
* create derivative works or improvements of the Spine Runtimes or (b) remove, | |
* delete, alter, or obscure any trademarks or any copyright, trademark, patent, | |
* or other intellectual property or proprietary rights notices on or in the | |
* Software, including any copy thereof. Redistributions in binary or source | |
* form must include this license and terms. | |
* | |
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR | |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF | |
* USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
* POSSIBILITY OF SUCH DAMAGE. | |
*****************************************************************************/ | |
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
// WARNING: | |
// Don't have repeating elements! | |
// Don't have invalid Skeleton Jsons! | |
// Atlas loading is not threaded! | |
namespace Spine.Unity { | |
public class ThreadedSkeletonDataAssetLoader : MonoBehaviour { | |
public List<SkeletonDataAsset> skeletonDataAssets = new List<SkeletonDataAsset>(); | |
readonly List<SkeletonDataAsset> workingSkeletonDataAssets = new List<SkeletonDataAsset>(); | |
readonly List<SkeletonDataLoadJob> loadJobs = new List<SkeletonDataLoadJob>(); | |
public UnityEngine.UI.Text text; | |
public bool useThreading = true; | |
IEnumerator Start () { | |
if (text != null) text.text = "IDLE..."; | |
yield return new WaitForSeconds(1f); | |
if (text != null) text.text = "LOADING..."; | |
yield return null; | |
var watch = new System.Diagnostics.Stopwatch(); | |
watch.Start(); | |
// Trim working list to non-null SkeletonData. | |
var workingSkeletonDataAssets = this.workingSkeletonDataAssets; | |
foreach (var sda in skeletonDataAssets) { | |
if (sda != null && sda.skeletonJSON != null) | |
workingSkeletonDataAssets.Add(sda); | |
} | |
if (useThreading) { | |
int n = workingSkeletonDataAssets.Count; | |
// STEP 1: Start loading jobs. | |
{ | |
// Set Capacity | |
loadJobs.Clear(); | |
loadJobs.Capacity = n; | |
for (int i = 0; i < n; i++) loadJobs.Add(null); | |
// Start loading threads | |
for (int i = 0; i < n; i++) { | |
var skeletonDataAsset = workingSkeletonDataAssets[i]; | |
var loadJob = new SkeletonDataLoadJob(skeletonDataAsset); | |
loadJobs[i] = loadJob; | |
loadJob.Start(); | |
} | |
} | |
// STEP 2: Wait until everything finishes loading using a Unity Coroutine. | |
{ | |
bool doneLoading = false; | |
while (!doneLoading) { | |
doneLoading = true; | |
// Don't check every frame. | |
yield return null; | |
yield return null; | |
yield return null; | |
yield return null; | |
yield return null; | |
for (int i = 0; i < n; i++) { | |
if (!loadJobs[i].Update()) { | |
doneLoading = false; | |
break; | |
} | |
} | |
} | |
} | |
// STEP 3: Initialize SkeletonDataAssets with loaded data. | |
for (int i = 0; i < n; i++) | |
workingSkeletonDataAssets[i].InitializeWithData(loadJobs[i].LoadedSkeletonData); | |
} else { | |
// Non-threaded. | |
foreach (var s in workingSkeletonDataAssets) | |
s.GetSkeletonData(false); | |
} | |
// Diagnostic results. | |
{ | |
watch.Stop(); | |
string elapsed = watch.Elapsed.TotalSeconds.ToString("##.000"); | |
if (text != null) text.text = string.Format("DONE...\n{0} seconds.", elapsed); | |
Debug.Log(elapsed + " sec with " + (useThreading ? "threads." : "no threads.")); | |
yield return null; | |
} | |
// Spawn them. Just for fun. And to be sure they loaded. | |
foreach (var s in skeletonDataAssets) { | |
var sa = SkeletonAnimation.NewSkeletonAnimationGameObject(s); | |
sa.Initialize(false); | |
sa.transform.position = Random.insideUnitCircle * 6f; | |
} | |
} | |
} | |
public class SkeletonDataLoadJob : ThreadedJob { | |
readonly byte[] skeletonDataBytes; | |
readonly string skeletonDataString; | |
readonly AttachmentLoader attachmentLoader; | |
readonly float scale; | |
SkeletonData loadedSkeletonData; | |
public SkeletonData LoadedSkeletonData { get { return loadedSkeletonData; } } | |
public SkeletonDataAsset SkeletonDataAsset { get; private set; } | |
public SkeletonDataLoadJob (SkeletonDataAsset sda) { | |
var skeletonJSON = sda.skeletonJSON; | |
bool isBinary = skeletonJSON.name.ToLower().Contains(".skel"); | |
if (isBinary) | |
skeletonDataBytes = skeletonJSON.bytes; | |
else | |
skeletonDataString = skeletonJSON.text; | |
var atlasAssets = sda.GetAtlasArray(); | |
attachmentLoader = new AtlasAttachmentLoader(atlasAssets); | |
this.scale = sda.scale; | |
} | |
protected override void ThreadFunction () { | |
bool isBinary = skeletonDataBytes != null; | |
if (isBinary) | |
loadedSkeletonData = SkeletonDataAsset.ReadSkeletonData(skeletonDataBytes, attachmentLoader, scale); | |
else | |
loadedSkeletonData = SkeletonDataAsset.ReadSkeletonData(skeletonDataString, attachmentLoader, scale); | |
} | |
} | |
public abstract class ThreadedJob { | |
bool m_IsDone = false; | |
object m_Handle = new object(); | |
System.Threading.Thread m_Thread = null; | |
public bool IsDone { | |
get { | |
bool tmp; | |
lock (m_Handle) { | |
tmp = m_IsDone; | |
} | |
return tmp; | |
} | |
protected set { | |
lock (m_Handle) { | |
m_IsDone = value; | |
} | |
} | |
} | |
public virtual void Start () { | |
m_Thread = new System.Threading.Thread(Run); | |
m_Thread.Start(); | |
} | |
public virtual void Abort() { | |
m_Thread.Abort(); | |
} | |
protected abstract void ThreadFunction (); | |
protected virtual void OnFinished () { } | |
public virtual bool Update () { | |
if (this.IsDone) { | |
OnFinished(); | |
return true; | |
} | |
return false; | |
} | |
// public IEnumerator WaitFor () { | |
// while (!this.Update()) yield return null; | |
// } | |
void Run() { | |
ThreadFunction(); | |
IsDone = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment