Merge remote-tracking branch 'origin/master'

This commit is contained in:
NathanSweet 2016-05-13 11:16:01 +02:00
commit 37ef9ec176
3 changed files with 136 additions and 43 deletions

View File

@ -711,21 +711,26 @@ namespace Spine.Unity.Editor {
try { try {
obj = Json.Deserialize(new StringReader(asset.text)); obj = Json.Deserialize(new StringReader(asset.text));
} catch (System.Exception) { } catch (System.Exception) {
} }
if (obj == null) { if (obj == null) {
Debug.LogError("Is not valid JSON"); Debug.LogError("Is not valid JSON");
return false; return false;
} }
Dictionary<string, object> root = (Dictionary<string, object>)obj; var root = obj as Dictionary<string, object>;
if (root == null) {
Debug.LogError("Parser returned an incorrect type.");
return false;
}
if (!root.ContainsKey("skeleton")) if (!root.ContainsKey("skeleton"))
return false; return false;
Dictionary<string, object> skeletonInfo = (Dictionary<string, object>)root["skeleton"]; // var skeletonInfo = (Dictionary<string, object>)root["skeleton"];
// string spineVersion = (string)skeletonInfo["spine"];
string spineVersion = (string)skeletonInfo["spine"]; // TODO: Warn users of old version incompatibility.
//TODO: reject old versions
return true; return true;
} }

View File

@ -2,52 +2,134 @@
* SkeletonRendererCustomMaterialsInspector created by Lost Polygon * SkeletonRendererCustomMaterialsInspector created by Lost Polygon
* Full irrevocable rights and permissions granted to Esoteric Software * Full irrevocable rights and permissions granted to Esoteric Software
*****************************************************************************/ *****************************************************************************/
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor; using UnityEditor;
using UnityEngine;
using Spine.Unity.Modules; using Spine.Unity.Modules;
namespace Spine.Unity.Editor { namespace Spine.Unity.Editor {
// This script is not intended for use with code. See the readme.txt file in SkeletonRendererCustomMaterials folder to learn more.
[CustomEditor(typeof(SkeletonRendererCustomMaterials))] [CustomEditor(typeof(SkeletonRendererCustomMaterials))]
public class SkeletonRendererCustomMaterialsInspector : UnityEditor.Editor { public class SkeletonRendererCustomMaterialsInspector : UnityEditor.Editor {
List<SkeletonRendererCustomMaterials.AtlasMaterialOverride> componentCustomMaterialOverrides, _customMaterialOverridesPrev;
List<SkeletonRendererCustomMaterials.SlotMaterialOverride> componentCustomSlotMaterials, _customSlotMaterialsPrev;
SkeletonRendererCustomMaterials component;
const BindingFlags PrivateInstance = BindingFlags.Instance | BindingFlags.NonPublic;
MethodInfo RemoveCustomMaterialOverrides, RemoveCustomSlotMaterials, SetCustomMaterialOverrides, SetCustomSlotMaterials;
#region SkeletonRenderer context menu #region SkeletonRenderer context menu
[MenuItem ("CONTEXT/SkeletonRenderer/Add Basic Serialized Custom Materials")] [MenuItem("CONTEXT/SkeletonRenderer/Add Basic Serialized Custom Materials")]
static void AddSkeletonRendererCustomMaterials (MenuCommand menuCommand) { static void AddSkeletonRendererCustomMaterials (MenuCommand menuCommand) {
var skeletonRenderer = (SkeletonRenderer)menuCommand.context; var skeletonRenderer = (SkeletonRenderer)menuCommand.context;
var newComponent = skeletonRenderer.gameObject.AddComponent<SkeletonRendererCustomMaterials>(); var newComponent = skeletonRenderer.gameObject.AddComponent<SkeletonRendererCustomMaterials>();
Undo.RegisterCreatedObjectUndo(newComponent, "Add Basic Serialized Custom Materials"); Undo.RegisterCreatedObjectUndo(newComponent, "Add Basic Serialized Custom Materials");
} }
[MenuItem ("CONTEXT/SkeletonRenderer/Add Basic Serialized Custom Materials", true)] [MenuItem("CONTEXT/SkeletonRenderer/Add Basic Serialized Custom Materials", true)]
static bool AddSkeletonRendererCustomMaterials_Validate (MenuCommand menuCommand) { static bool AddSkeletonRendererCustomMaterials_Validate (MenuCommand menuCommand) {
var skeletonRenderer = (SkeletonRenderer)menuCommand.context; var skeletonRenderer = (SkeletonRenderer)menuCommand.context;
return (skeletonRenderer.GetComponent<SkeletonRendererCustomMaterials>() == null); return (skeletonRenderer.GetComponent<SkeletonRendererCustomMaterials>() == null);
} }
#endregion #endregion
public override void OnInspectorGUI() { void OnEnable () {
var component = (SkeletonRendererCustomMaterials)target; Type cm = typeof(SkeletonRendererCustomMaterials);
RemoveCustomMaterialOverrides = cm.GetMethod("RemoveCustomMaterialOverrides", PrivateInstance);
RemoveCustomSlotMaterials = cm.GetMethod("RemoveCustomSlotMaterials", PrivateInstance);
SetCustomMaterialOverrides = cm.GetMethod("SetCustomMaterialOverrides", PrivateInstance);
SetCustomSlotMaterials = cm.GetMethod("SetCustomSlotMaterials", PrivateInstance);
}
public override void OnInspectorGUI () {
component = (SkeletonRendererCustomMaterials)target;
var skeletonRenderer = component.skeletonRenderer; var skeletonRenderer = component.skeletonRenderer;
// Draw the default inspector and reapply overrides on any change // Draw the default inspector
EditorGUI.BeginChangeCheck(); DrawDefaultInspector();
{
DrawDefaultInspector(); if (serializedObject.isEditingMultipleObjects)
return;
if (componentCustomMaterialOverrides == null) {
Type cm = typeof(SkeletonRendererCustomMaterials);
componentCustomMaterialOverrides = cm.GetField("customMaterialOverrides", PrivateInstance).GetValue(component) as List<SkeletonRendererCustomMaterials.AtlasMaterialOverride>;
componentCustomSlotMaterials = cm.GetField("customSlotMaterials", PrivateInstance).GetValue(component) as List<SkeletonRendererCustomMaterials.SlotMaterialOverride>;
if (componentCustomMaterialOverrides == null) {
Debug.Log("Reflection failed.");
return;
}
} }
if (EditorGUI.EndChangeCheck()) {
component.ReapplyOverrides(); // Fill with current values at start
if (_customMaterialOverridesPrev == null || _customSlotMaterialsPrev == null) {
_customMaterialOverridesPrev = CopyList(componentCustomMaterialOverrides);
_customSlotMaterialsPrev = CopyList(componentCustomSlotMaterials);
}
// Compare new values with saved. If change is detected:
// store new values, restore old values, remove overrides, restore new values, restore overrides.
// 1. Store new values
var customMaterialOverridesNew = CopyList(componentCustomMaterialOverrides);
var customSlotMaterialsNew = CopyList(componentCustomSlotMaterials);
// Detect changes
if (!_customMaterialOverridesPrev.SequenceEqual(customMaterialOverridesNew) ||
!_customSlotMaterialsPrev.SequenceEqual(customSlotMaterialsNew)) {
// 2. Restore old values
componentCustomMaterialOverrides.Clear();
componentCustomSlotMaterials.Clear();
componentCustomMaterialOverrides.AddRange(_customMaterialOverridesPrev);
componentCustomSlotMaterials.AddRange(_customSlotMaterialsPrev);
// 3. Remove overrides
RemoveCustomMaterials();
// 4. Restore new values
componentCustomMaterialOverrides.Clear();
componentCustomSlotMaterials.Clear();
componentCustomMaterialOverrides.AddRange(customMaterialOverridesNew);
componentCustomSlotMaterials.AddRange(customSlotMaterialsNew);
// 5. Restore overrides
SetCustomMaterials();
if (skeletonRenderer != null) if (skeletonRenderer != null)
skeletonRenderer.LateUpdate(); skeletonRenderer.LateUpdate();
} }
_customMaterialOverridesPrev = CopyList(componentCustomMaterialOverrides);
_customSlotMaterialsPrev = CopyList(componentCustomSlotMaterials);
if (GUILayout.Button(new GUIContent("Clear and Reapply Changes", "Removes all non-serialized overrides in the SkeletonRenderer and reapplies the overrides on this component."))) { if (GUILayout.Button(new GUIContent("Clear and Reapply Changes", "Removes all non-serialized overrides in the SkeletonRenderer and reapplies the overrides on this component."))) {
if (skeletonRenderer != null) { if (skeletonRenderer != null) {
skeletonRenderer.CustomMaterialOverride.Clear(); skeletonRenderer.CustomMaterialOverride.Clear();
skeletonRenderer.CustomSlotMaterials.Clear(); skeletonRenderer.CustomSlotMaterials.Clear();
component.ReapplyOverrides(); RemoveCustomMaterials();
SetCustomMaterials();
skeletonRenderer.LateUpdate(); skeletonRenderer.LateUpdate();
} }
} }
} }
void RemoveCustomMaterials () {
RemoveCustomMaterialOverrides.Invoke(component, null);
RemoveCustomSlotMaterials.Invoke(component, null);
}
void SetCustomMaterials () {
SetCustomMaterialOverrides.Invoke(component, null);
SetCustomSlotMaterials.Invoke(component, null);
}
static List<T> CopyList<T> (List<T> list) {
return list.GetRange(0, list.Count);
}
} }
} }

View File

@ -13,12 +13,8 @@ namespace Spine.Unity.Modules {
#region Inspector #region Inspector
public SkeletonRenderer skeletonRenderer; public SkeletonRenderer skeletonRenderer;
[SerializeField] List<SlotMaterialOverride> customSlotMaterials = new List<SlotMaterialOverride>();
[SerializeField] [SerializeField] List<AtlasMaterialOverride> customMaterialOverrides = new List<AtlasMaterialOverride>();
private List<SlotMaterialOverride> customSlotMaterials = new List<SlotMaterialOverride>();
[SerializeField]
private List<AtlasMaterialOverride> customMaterialOverrides = new List<AtlasMaterialOverride>();
#if UNITY_EDITOR #if UNITY_EDITOR
void Reset () { void Reset () {
@ -45,22 +41,12 @@ namespace Spine.Unity.Modules {
#endif #endif
#endregion #endregion
public List<SlotMaterialOverride> CustomSlotMaterials { get { return customSlotMaterials; } } void SetCustomSlotMaterials () {
public List<AtlasMaterialOverride> CustomMaterialOverrides { get { return customMaterialOverrides; } }
public void ReapplyOverrides () {
if (skeletonRenderer == null) { if (skeletonRenderer == null) {
Debug.LogError("skeletonRenderer == null"); Debug.LogError("skeletonRenderer == null");
return; return;
} }
RemoveCustomMaterialOverrides();
RemoveCustomSlotMaterials();
SetCustomMaterialOverrides();
SetCustomSlotMaterials();
}
void SetCustomSlotMaterials () {
for (int i = 0; i < customSlotMaterials.Count; i++) { for (int i = 0; i < customSlotMaterials.Count; i++) {
SlotMaterialOverride slotMaterialOverride = customSlotMaterials[i]; SlotMaterialOverride slotMaterialOverride = customSlotMaterials[i];
if (slotMaterialOverride.overrideDisabled || string.IsNullOrEmpty(slotMaterialOverride.slotName)) if (slotMaterialOverride.overrideDisabled || string.IsNullOrEmpty(slotMaterialOverride.slotName))
@ -72,6 +58,11 @@ namespace Spine.Unity.Modules {
} }
void RemoveCustomSlotMaterials () { void RemoveCustomSlotMaterials () {
if (skeletonRenderer == null) {
Debug.LogError("skeletonRenderer == null");
return;
}
for (int i = 0; i < customSlotMaterials.Count; i++) { for (int i = 0; i < customSlotMaterials.Count; i++) {
SlotMaterialOverride slotMaterialOverride = customSlotMaterials[i]; SlotMaterialOverride slotMaterialOverride = customSlotMaterials[i];
if (string.IsNullOrEmpty(slotMaterialOverride.slotName)) if (string.IsNullOrEmpty(slotMaterialOverride.slotName))
@ -92,6 +83,11 @@ namespace Spine.Unity.Modules {
} }
void SetCustomMaterialOverrides () { void SetCustomMaterialOverrides () {
if (skeletonRenderer == null) {
Debug.LogError("skeletonRenderer == null");
return;
}
for (int i = 0; i < customMaterialOverrides.Count; i++) { for (int i = 0; i < customMaterialOverrides.Count; i++) {
AtlasMaterialOverride atlasMaterialOverride = customMaterialOverrides[i]; AtlasMaterialOverride atlasMaterialOverride = customMaterialOverrides[i];
if (atlasMaterialOverride.overrideDisabled) if (atlasMaterialOverride.overrideDisabled)
@ -102,6 +98,11 @@ namespace Spine.Unity.Modules {
} }
void RemoveCustomMaterialOverrides () { void RemoveCustomMaterialOverrides () {
if (skeletonRenderer == null) {
Debug.LogError("skeletonRenderer == null");
return;
}
for (int i = 0; i < customMaterialOverrides.Count; i++) { for (int i = 0; i < customMaterialOverrides.Count; i++) {
AtlasMaterialOverride atlasMaterialOverride = customMaterialOverrides[i]; AtlasMaterialOverride atlasMaterialOverride = customMaterialOverrides[i];
Material currentMaterial; Material currentMaterial;
@ -116,7 +117,7 @@ namespace Spine.Unity.Modules {
} }
} }
// OnEnable applies the overrides at runtime and when the editor loads. // OnEnable applies the overrides at runtime, and when the editor loads.
void OnEnable () { void OnEnable () {
if (skeletonRenderer == null) if (skeletonRenderer == null)
skeletonRenderer = GetComponent<SkeletonRenderer>(); skeletonRenderer = GetComponent<SkeletonRenderer>();
@ -126,13 +127,12 @@ namespace Spine.Unity.Modules {
return; return;
} }
skeletonRenderer.Initialize(false); skeletonRenderer.Initialize(false);
SetCustomMaterialOverrides(); SetCustomMaterialOverrides();
SetCustomSlotMaterials(); SetCustomSlotMaterials();
} }
// OnDisable removes the overrides at runtime, and in the editor when the component is disabled or destroyed.
// OnDisable removes the overrides at runtime and in the editor when the component is disabled or destroyed.
void OnDisable () { void OnDisable () {
if (skeletonRenderer == null) { if (skeletonRenderer == null) {
Debug.LogError("skeletonRenderer == null"); Debug.LogError("skeletonRenderer == null");
@ -144,21 +144,27 @@ namespace Spine.Unity.Modules {
} }
[Serializable] [Serializable]
public class MaterialOverride { public struct SlotMaterialOverride : IEquatable<SlotMaterialOverride> {
public bool overrideDisabled; public bool overrideDisabled;
}
[Serializable]
public class SlotMaterialOverride : MaterialOverride {
[SpineSlot] [SpineSlot]
public string slotName; public string slotName;
public Material material; public Material material;
public bool Equals (SlotMaterialOverride other) {
return overrideDisabled == other.overrideDisabled && slotName == other.slotName && material == other.material;
}
} }
[Serializable] [Serializable]
public class AtlasMaterialOverride : MaterialOverride { public struct AtlasMaterialOverride : IEquatable<AtlasMaterialOverride> {
public bool overrideDisabled;
public Material originalMaterial; public Material originalMaterial;
public Material replacementMaterial; public Material replacementMaterial;
public bool Equals (AtlasMaterialOverride other) {
return overrideDisabled == other.overrideDisabled && originalMaterial == other.originalMaterial && replacementMaterial == other.replacementMaterial;
}
} }
} }
} }