2023-03-18 04:40:20 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace HeavenStudio.Common
|
|
|
|
{
|
|
|
|
public class SuperScroll : MonoBehaviour
|
|
|
|
{
|
|
|
|
#region Private
|
|
|
|
|
2023-04-26 12:43:35 +00:00
|
|
|
[SerializeField] private Material _shader;
|
|
|
|
|
2023-03-18 04:40:20 +00:00
|
|
|
[SerializeField]
|
|
|
|
private Renderer _renderer;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private Sprite _sprite;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Public
|
|
|
|
|
|
|
|
public float NormalizedX = 0.0f;
|
|
|
|
public float NormalizedY = 0.0f;
|
|
|
|
public Vector2 Normalized { get { return new Vector2(NormalizedX, NormalizedY); } set { NormalizedX = value.x; NormalizedY = value.y; } }
|
2023-05-23 04:09:46 +00:00
|
|
|
public bool AutoScroll;
|
|
|
|
public float AutoScrollX;
|
|
|
|
public float AutoScrollY;
|
2023-03-18 04:40:20 +00:00
|
|
|
|
|
|
|
public float TileX = 1.0f;
|
|
|
|
public float TileY = 1.0f;
|
|
|
|
public Vector2 Tile { get { return new Vector2(TileX, TileY); } set { TileX = value.x; TileY = value.y; } }
|
|
|
|
|
|
|
|
public Material Material => _renderer.material;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region MonoBehaviour
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
2023-04-26 12:43:35 +00:00
|
|
|
_renderer.material = _shader;
|
2023-03-18 04:40:20 +00:00
|
|
|
|
|
|
|
var spriteRect = _sprite.rect;
|
2023-05-23 04:09:46 +00:00
|
|
|
var tex = CropTexture(_sprite.texture, new Rect(spriteRect.x, spriteRect.y, spriteRect.width, spriteRect.height));
|
2023-03-18 04:40:20 +00:00
|
|
|
tex.wrapMode = TextureWrapMode.Repeat;
|
|
|
|
Material.mainTexture = tex;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void LateUpdate()
|
|
|
|
{
|
|
|
|
_renderer.material.mainTextureScale = Tile;
|
|
|
|
_renderer.material.mainTextureOffset = new Vector2(NormalizedX, -NormalizedY) * Tile;
|
2023-05-23 04:09:46 +00:00
|
|
|
|
|
|
|
if (AutoScroll) {
|
2024-04-10 05:05:47 +00:00
|
|
|
float songPos = Conductor.instance.unswungSongPositionInBeats/100;
|
2023-05-23 04:09:46 +00:00
|
|
|
NormalizedX = songPos*AutoScrollX;
|
|
|
|
NormalizedY = songPos*AutoScrollY;
|
|
|
|
}
|
2023-03-18 04:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Custom
|
|
|
|
|
|
|
|
private Texture2D CropTexture(Texture2D original, Rect rect)
|
|
|
|
{
|
|
|
|
var colors = original.GetPixels((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height);
|
2023-04-26 12:43:35 +00:00
|
|
|
var newTex = new Texture2D((int)rect.width, (int)rect.height);
|
2023-03-18 04:40:20 +00:00
|
|
|
|
|
|
|
newTex.SetPixels(colors);
|
|
|
|
newTex.Apply();
|
|
|
|
|
|
|
|
return newTex;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|