183 lines
6.0 KiB
C#
Raw Normal View History

2024-11-21 09:35:48 +08:00
/******************************************************************************
2024-10-29 15:22:32 +08:00
* Spine Runtimes License Agreement
2024-11-21 09:35:48 +08:00
* Last updated July 28, 2023. Replaces all prior versions.
2024-10-29 15:22:32 +08:00
*
2024-11-21 09:35:48 +08:00
* Copyright (c) 2013-2023, Esoteric Software LLC
2024-10-29 15:22:32 +08:00
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
2024-11-21 09:35:48 +08:00
* Otherwise, it is permitted to integrate the Spine Runtimes into software or
* otherwise create derivative works of the Spine Runtimes (collectively,
2024-10-29 15:22:32 +08:00
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "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 LLC 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
2024-11-21 09:35:48 +08:00
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
* SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2024-10-29 15:22:32 +08:00
*****************************************************************************/
#if UNITY_2018_3 || UNITY_2019 || UNITY_2018_3_OR_NEWER
#define NEW_PREFAB_SYSTEM
#endif
2024-11-21 09:35:48 +08:00
#if UNITY_2018_2_OR_NEWER
#define HAS_GET_SHARED_MATERIALS
#endif
2024-10-29 15:22:32 +08:00
using System.Collections.Generic;
2024-11-21 09:35:48 +08:00
using UnityEngine;
2024-10-29 15:22:32 +08:00
namespace Spine.Unity.Examples {
#if NEW_PREFAB_SYSTEM
[ExecuteAlways]
#else
[ExecuteInEditMode]
#endif
[RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshFilter))]
2024-11-21 09:35:48 +08:00
public class RenderExistingMesh : MonoBehaviour {
2024-10-29 15:22:32 +08:00
public MeshRenderer referenceRenderer;
bool updateViaSkeletonCallback = false;
MeshFilter referenceMeshFilter;
MeshRenderer ownRenderer;
MeshFilter ownMeshFilter;
[System.Serializable]
public struct MaterialReplacement {
public Material originalMaterial;
public Material replacementMaterial;
}
public MaterialReplacement[] replacementMaterials = new MaterialReplacement[0];
private Dictionary<Material, Material> replacementMaterialDict = new Dictionary<Material, Material>();
private Material[] sharedMaterials = new Material[0];
2024-11-21 09:35:48 +08:00
#if HAS_GET_SHARED_MATERIALS
private List<Material> parentMaterials = new List<Material>();
#endif
2024-10-29 15:22:32 +08:00
#if UNITY_EDITOR
private void Reset () {
if (referenceRenderer == null) {
2024-11-21 09:35:48 +08:00
if (this.transform.parent)
referenceRenderer = this.transform.parent.GetComponentInParent<MeshRenderer>();
if (referenceRenderer == null) return;
2024-10-29 15:22:32 +08:00
}
2024-11-21 09:35:48 +08:00
#if HAS_GET_SHARED_MATERIALS
referenceRenderer.GetSharedMaterials(parentMaterials);
int parentMaterialsCount = parentMaterials.Count;
#else
Material[] parentMaterials = referenceRenderer.sharedMaterials;
int parentMaterialsCount = parentMaterials.Length;
#endif
if (replacementMaterials.Length != parentMaterialsCount) {
replacementMaterials = new MaterialReplacement[parentMaterialsCount];
2024-10-29 15:22:32 +08:00
}
2024-11-21 09:35:48 +08:00
for (int i = 0; i < parentMaterialsCount; ++i) {
2024-10-29 15:22:32 +08:00
replacementMaterials[i].originalMaterial = parentMaterials[i];
replacementMaterials[i].replacementMaterial = parentMaterials[i];
}
Awake();
LateUpdate();
}
#endif
void Awake () {
2024-11-21 09:35:48 +08:00
ownRenderer = this.GetComponent<MeshRenderer>();
ownMeshFilter = this.GetComponent<MeshFilter>();
2024-10-29 15:22:32 +08:00
if (referenceRenderer == null) {
2024-11-21 09:35:48 +08:00
if (this.transform.parent != null)
referenceRenderer = this.transform.parent.GetComponentInParent<MeshRenderer>();
if (referenceRenderer == null) return;
2024-10-29 15:22:32 +08:00
}
2024-11-21 09:35:48 +08:00
referenceMeshFilter = referenceRenderer.GetComponent<MeshFilter>();
2024-10-29 15:22:32 +08:00
// subscribe to OnMeshAndMaterialsUpdated
2024-11-21 09:35:48 +08:00
SkeletonAnimation skeletonRenderer = referenceRenderer.GetComponent<SkeletonAnimation>();
2024-10-29 15:22:32 +08:00
if (skeletonRenderer) {
skeletonRenderer.OnMeshAndMaterialsUpdated -= UpdateOnCallback;
skeletonRenderer.OnMeshAndMaterialsUpdated += UpdateOnCallback;
updateViaSkeletonCallback = true;
}
InitializeDict();
}
2024-11-21 09:35:48 +08:00
#if UNITY_EDITOR
// handle disabled scene reload
private void OnEnable () {
if (Application.isPlaying)
Awake();
}
2024-10-29 15:22:32 +08:00
private void Update () {
2024-11-21 09:35:48 +08:00
if (!Application.isPlaying)
2024-10-29 15:22:32 +08:00
InitializeDict();
}
2024-11-21 09:35:48 +08:00
#endif
2024-10-29 15:22:32 +08:00
void LateUpdate () {
2024-11-21 09:35:48 +08:00
#if UNITY_EDITOR
2024-10-29 15:22:32 +08:00
if (!Application.isPlaying) {
UpdateMaterials();
return;
}
2024-11-21 09:35:48 +08:00
#endif
2024-10-29 15:22:32 +08:00
if (updateViaSkeletonCallback)
return;
UpdateMaterials();
}
void UpdateOnCallback (SkeletonRenderer r) {
UpdateMaterials();
}
void UpdateMaterials () {
2024-11-21 09:35:48 +08:00
#if UNITY_EDITOR
if (!referenceRenderer) return;
if (!referenceMeshFilter) Reset();
#endif
2024-10-29 15:22:32 +08:00
ownMeshFilter.sharedMesh = referenceMeshFilter.sharedMesh;
2024-11-21 09:35:48 +08:00
#if HAS_GET_SHARED_MATERIALS
referenceRenderer.GetSharedMaterials(parentMaterials);
int parentMaterialsCount = parentMaterials.Count;
#else
Material[] parentMaterials = referenceRenderer.sharedMaterials;
int parentMaterialsCount = parentMaterials.Length;
#endif
if (sharedMaterials.Length != parentMaterialsCount) {
sharedMaterials = new Material[parentMaterialsCount];
2024-10-29 15:22:32 +08:00
}
2024-11-21 09:35:48 +08:00
for (int i = 0; i < parentMaterialsCount; ++i) {
Material parentMaterial = parentMaterials[i];
2024-10-29 15:22:32 +08:00
if (replacementMaterialDict.ContainsKey(parentMaterial)) {
sharedMaterials[i] = replacementMaterialDict[parentMaterial];
}
}
ownRenderer.sharedMaterials = sharedMaterials;
}
void InitializeDict () {
2024-11-21 09:35:48 +08:00
replacementMaterialDict.Clear();
2024-10-29 15:22:32 +08:00
for (int i = 0; i < replacementMaterials.Length; ++i) {
2024-11-21 09:35:48 +08:00
MaterialReplacement entry = replacementMaterials[i];
2024-10-29 15:22:32 +08:00
replacementMaterialDict[entry.originalMaterial] = entry.replacementMaterial;
}
}
}
}