mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-04 14:24:53 +08:00
[unity] Fixed RenderExistingMesh causing unnecessary allocation. Additional code cleanup and added robustness. Closes #2543.
This commit is contained in:
parent
0481a04cef
commit
568e5ef049
@ -31,6 +31,10 @@
|
|||||||
#define NEW_PREFAB_SYSTEM
|
#define NEW_PREFAB_SYSTEM
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if UNITY_2018_2_OR_NEWER
|
||||||
|
#define HAS_GET_SHARED_MATERIALS
|
||||||
|
#endif
|
||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
@ -59,20 +63,28 @@ namespace Spine.Unity.Examples {
|
|||||||
|
|
||||||
private Dictionary<Material, Material> replacementMaterialDict = new Dictionary<Material, Material>();
|
private Dictionary<Material, Material> replacementMaterialDict = new Dictionary<Material, Material>();
|
||||||
private Material[] sharedMaterials = new Material[0];
|
private Material[] sharedMaterials = new Material[0];
|
||||||
|
#if HAS_GET_SHARED_MATERIALS
|
||||||
|
private List<Material> parentMaterials = new List<Material>();
|
||||||
|
#endif
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
private void Reset () {
|
private void Reset () {
|
||||||
if (referenceRenderer == null) {
|
if (referenceRenderer == null) {
|
||||||
referenceRenderer = this.transform.parent.GetComponentInParent<MeshRenderer>();
|
if (this.transform.parent)
|
||||||
if (!referenceRenderer)
|
referenceRenderer = this.transform.parent.GetComponentInParent<MeshRenderer>();
|
||||||
return;
|
if (referenceRenderer == null) return;
|
||||||
}
|
}
|
||||||
|
#if HAS_GET_SHARED_MATERIALS
|
||||||
|
referenceRenderer.GetSharedMaterials(parentMaterials);
|
||||||
|
int parentMaterialsCount = parentMaterials.Count;
|
||||||
|
#else
|
||||||
Material[] parentMaterials = referenceRenderer.sharedMaterials;
|
Material[] parentMaterials = referenceRenderer.sharedMaterials;
|
||||||
if (replacementMaterials.Length != parentMaterials.Length) {
|
int parentMaterialsCount = parentMaterials.Length;
|
||||||
replacementMaterials = new MaterialReplacement[parentMaterials.Length];
|
#endif
|
||||||
|
if (replacementMaterials.Length != parentMaterialsCount) {
|
||||||
|
replacementMaterials = new MaterialReplacement[parentMaterialsCount];
|
||||||
}
|
}
|
||||||
for (int i = 0; i < parentMaterials.Length; ++i) {
|
for (int i = 0; i < parentMaterialsCount; ++i) {
|
||||||
replacementMaterials[i].originalMaterial = parentMaterials[i];
|
replacementMaterials[i].originalMaterial = parentMaterials[i];
|
||||||
replacementMaterials[i].replacementMaterial = parentMaterials[i];
|
replacementMaterials[i].replacementMaterial = parentMaterials[i];
|
||||||
}
|
}
|
||||||
@ -82,9 +94,15 @@ namespace Spine.Unity.Examples {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
void Awake () {
|
void Awake () {
|
||||||
|
ownRenderer = this.GetComponent<MeshRenderer>();
|
||||||
|
ownMeshFilter = this.GetComponent<MeshFilter>();
|
||||||
|
|
||||||
if (referenceRenderer == null) {
|
if (referenceRenderer == null) {
|
||||||
referenceRenderer = this.transform.parent.GetComponentInParent<MeshRenderer>();
|
if (this.transform.parent != null)
|
||||||
|
referenceRenderer = this.transform.parent.GetComponentInParent<MeshRenderer>();
|
||||||
|
if (referenceRenderer == null) return;
|
||||||
}
|
}
|
||||||
|
referenceMeshFilter = referenceRenderer.GetComponent<MeshFilter>();
|
||||||
|
|
||||||
// subscribe to OnMeshAndMaterialsUpdated
|
// subscribe to OnMeshAndMaterialsUpdated
|
||||||
SkeletonAnimation skeletonRenderer = referenceRenderer.GetComponent<SkeletonAnimation>();
|
SkeletonAnimation skeletonRenderer = referenceRenderer.GetComponent<SkeletonAnimation>();
|
||||||
@ -93,9 +111,6 @@ namespace Spine.Unity.Examples {
|
|||||||
skeletonRenderer.OnMeshAndMaterialsUpdated += UpdateOnCallback;
|
skeletonRenderer.OnMeshAndMaterialsUpdated += UpdateOnCallback;
|
||||||
updateViaSkeletonCallback = true;
|
updateViaSkeletonCallback = true;
|
||||||
}
|
}
|
||||||
referenceMeshFilter = referenceRenderer.GetComponent<MeshFilter>();
|
|
||||||
ownRenderer = this.GetComponent<MeshRenderer>();
|
|
||||||
ownMeshFilter = this.GetComponent<MeshFilter>();
|
|
||||||
|
|
||||||
InitializeDict();
|
InitializeDict();
|
||||||
}
|
}
|
||||||
@ -131,6 +146,10 @@ namespace Spine.Unity.Examples {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void UpdateMaterials () {
|
void UpdateMaterials () {
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
if (!referenceRenderer) return;
|
||||||
|
if (!referenceMeshFilter) Reset();
|
||||||
|
#endif
|
||||||
ownMeshFilter.sharedMesh = referenceMeshFilter.sharedMesh;
|
ownMeshFilter.sharedMesh = referenceMeshFilter.sharedMesh;
|
||||||
|
|
||||||
Material[] parentMaterials = referenceRenderer.sharedMaterials;
|
Material[] parentMaterials = referenceRenderer.sharedMaterials;
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
"name": "com.esotericsoftware.spine.spine-unity-examples",
|
"name": "com.esotericsoftware.spine.spine-unity-examples",
|
||||||
"displayName": "spine-unity Runtime Examples",
|
"displayName": "spine-unity Runtime Examples",
|
||||||
"description": "This plugin provides example scenes and scripts for the spine-unity runtime.",
|
"description": "This plugin provides example scenes and scripts for the spine-unity runtime.",
|
||||||
"version": "4.2.33",
|
"version": "4.2.34",
|
||||||
"unity": "2018.3",
|
"unity": "2018.3",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "Esoteric Software",
|
"name": "Esoteric Software",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user