From 115f77bda0246a00f6e17469685c67746bdbd29d Mon Sep 17 00:00:00 2001 From: nirav Date: Sat, 12 Dec 2020 07:18:36 +0000 Subject: Initial commit --- Assets/SampleUI/Scripts/ScrollDetailTexture.cs | 64 ++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 Assets/SampleUI/Scripts/ScrollDetailTexture.cs (limited to 'Assets/SampleUI/Scripts/ScrollDetailTexture.cs') diff --git a/Assets/SampleUI/Scripts/ScrollDetailTexture.cs b/Assets/SampleUI/Scripts/ScrollDetailTexture.cs new file mode 100755 index 0000000..305383b --- /dev/null +++ b/Assets/SampleUI/Scripts/ScrollDetailTexture.cs @@ -0,0 +1,64 @@ +using UnityEngine; +using UnityEngine.UI; + + +[RequireComponent(typeof(Image))] +public class ScrollDetailTexture : MonoBehaviour +{ + public bool uniqueMaterial = false; + public Vector2 scrollPerSecond = Vector2.zero; + + Matrix4x4 m_Matrix; + Material mCopy; + Material mOriginal; + Image mSprite; + Material m_Mat; + + void OnEnable () + { + mSprite = GetComponent(); + mOriginal = mSprite.material; + + if (uniqueMaterial && mSprite.material != null) + { + mCopy = new Material(mOriginal); + mCopy.name = "Copy of " + mOriginal.name; + mCopy.hideFlags = HideFlags.DontSave; + mSprite.material = mCopy; + } + } + + void OnDisable () + { + if (mCopy != null) + { + mSprite.material = mOriginal; + if (Application.isEditor) + UnityEngine.Object.DestroyImmediate(mCopy); + else + UnityEngine.Object.Destroy(mCopy); + mCopy = null; + } + mOriginal = null; + } + + void Update () + { + Material mat = (mCopy != null) ? mCopy : mOriginal; + + if (mat != null) + { + Texture tex = mat.GetTexture("_DetailTex"); + + if (tex != null) + { + mat.SetTextureOffset("_DetailTex", scrollPerSecond * Time.time); + + // TODO: It would be better to add support for MaterialBlocks on UIRenderer, + // because currently only one Update() function's matrix can be active at a time. + // With material block properties, the batching would be correctly broken up instead, + // and would work with multiple widgets using this detail shader. + } + } + } +} -- cgit v1.2.3