Эх сурвалжийг харах

[unity] SlotTintBlackFollower module

pharan 8 жил өмнө
parent
commit
74e688d548

+ 9 - 0
spine-unity/Assets/spine-unity/Modules/SlotTintBlackFollower.meta

@@ -0,0 +1,9 @@
+fileFormatVersion: 2
+guid: 7e364d7a84acb894ea3f4bde752a09e8
+folderAsset: yes
+timeCreated: 1489227060
+licenseType: Free
+DefaultImporter:
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 60 - 0
spine-unity/Assets/spine-unity/Modules/SlotTintBlackFollower/SlotTintBlackFollower.cs

@@ -0,0 +1,60 @@
+using UnityEngine;
+using Spine.Unity;
+
+namespace Spine.Unity.Modules {
+
+	/// <summary>
+	/// Add this component to a Spine GameObject to apply a specific slot's Colors as MaterialProperties.
+	/// This allows you to apply the two color tint to the whole skeleton and not require the overhead of an extra vertex stream on the mesh.
+	/// </summary>
+	public class SlotTintBlackFollower : MonoBehaviour {
+
+		#region Inspector
+		/// <summary>
+		/// Serialized name of the slot loaded at runtime. Change the slot field instead of this if you want to change the followed slot at runtime.</summary>
+		[SpineSlot]
+		[SerializeField]
+		protected string slotName;
+
+		[SerializeField]
+		protected string colorPropertyName = "_Color";
+		[SerializeField]
+		protected string blackPropertyName = "_Black";
+		#endregion
+
+		public Slot slot;
+		MeshRenderer mr;
+		MaterialPropertyBlock mb;
+		int colorPropertyId, blackPropertyId;
+
+		void Start () {
+			Initialize(false);
+		}
+
+		public void Initialize (bool overwrite) {
+			if (overwrite || mb == null) {
+				mb = new MaterialPropertyBlock();
+				mr = GetComponent<MeshRenderer>();
+				slot = GetComponent<ISkeletonComponent>().Skeleton.FindSlot(slotName);
+
+				colorPropertyId = Shader.PropertyToID(colorPropertyName);
+				blackPropertyId = Shader.PropertyToID(blackPropertyName);
+			}
+		}
+
+		public void Update () {
+			Slot s = slot;
+			if (s == null) return;
+
+			mb.SetColor(colorPropertyId, new Color(s.r, s.g, s.b, s.a));
+			mb.SetColor(blackPropertyId, new Color(s.r2, s.g2, s.b2, 1f));
+
+			mr.SetPropertyBlock(mb);
+		}
+
+		void OnDisable () {
+			mb.Clear();
+			mr.SetPropertyBlock(mb);
+		}
+	}
+}

+ 12 - 0
spine-unity/Assets/spine-unity/Modules/SlotTintBlackFollower/SlotTintBlackFollower.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 49a62759c814e7a458b9026d504e0898
+timeCreated: 1489227143
+licenseType: Free
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: