Files
Continentis/Assets/OtherPlugins/Sprite Shaders Ultimate/Demo/Scripts/Demo_Shaders.cs

140 lines
4.1 KiB
C#
Raw Normal View History

2026-04-01 12:23:27 -04:00
using System.Collections;
2025-11-25 21:49:03 -05:00
using System.Collections.Generic;
using UnityEngine;
namespace SpriteShadersUltimate.Demo
{
public class Demo_Shaders : MonoBehaviour
{
public static Demo_Shaders instance;
public static float zoomFactor;
2026-04-01 12:23:27 -04:00
GameObject environmentGO;
List<SpriteRenderer> environmentSprites;
Vector3 currentPosition;
2025-11-25 21:49:03 -05:00
2026-04-01 12:23:27 -04:00
float lastZoomFactor;
2025-11-25 21:49:03 -05:00
2026-04-01 12:23:27 -04:00
void Awake()
2025-11-25 21:49:03 -05:00
{
//Reference:
instance = this;
//Environment:
2026-04-01 12:23:27 -04:00
Transform environment = GameObject.Find("Environment").transform;
2025-11-25 21:49:03 -05:00
environmentSprites = new List<SpriteRenderer>();
2026-04-01 12:23:27 -04:00
foreach(SpriteRenderer sr in environment.GetComponentsInChildren<SpriteRenderer>())
{
environmentSprites.Add(sr);
}
2025-11-25 21:49:03 -05:00
environmentGO = environment.gameObject;
//Initialize:
Demo_Display.selected = null;
currentPosition = Vector3.zero;
zoomFactor = 0f;
lastZoomFactor = -1000;
}
2026-04-01 12:23:27 -04:00
void Update()
2025-11-25 21:49:03 -05:00
{
//Zoom Factor:
if (Demo_Display.selected != null)
{
zoomFactor += Time.unscaledDeltaTime * 2f;
if (zoomFactor > 1f) zoomFactor = 1f;
}
else
{
zoomFactor -= Time.unscaledDeltaTime * 2f;
if (zoomFactor < 0f) zoomFactor = 0f;
}
//Scale:
2026-04-01 12:23:27 -04:00
float scale = 1f + 6.2f * zoomFactor;
2025-11-25 21:49:03 -05:00
transform.localScale = new Vector3(scale, scale, 1);
if (zoomFactor != lastZoomFactor)
{
//Environment:
2026-04-01 12:23:27 -04:00
float alpha = Mathf.Clamp01((zoomFactor - 0.75f) / 0.25f);
foreach (SpriteRenderer sprite in environmentSprites)
2025-11-25 21:49:03 -05:00
{
2026-04-01 12:23:27 -04:00
Color color = sprite.color;
2025-11-25 21:49:03 -05:00
color.a = alpha;
sprite.color = color;
}
2026-04-01 12:23:27 -04:00
if(alpha > 0f)
2025-11-25 21:49:03 -05:00
{
2026-04-01 12:23:27 -04:00
if(!environmentGO.activeSelf)
{
environmentGO.SetActive(true);
}
2025-11-25 21:49:03 -05:00
}
else
{
2026-04-01 12:23:27 -04:00
if (environmentGO.activeSelf)
{
environmentGO.SetActive(false);
}
2025-11-25 21:49:03 -05:00
}
//Other:
lastZoomFactor = zoomFactor;
}
//Position:
2026-04-01 12:23:27 -04:00
if(Demo_Display.selected != null)
2025-11-25 21:49:03 -05:00
{
2026-04-01 12:23:27 -04:00
currentPosition = Vector3.Lerp(currentPosition, -Demo_Display.selected.transform.localPosition, Time.unscaledDeltaTime * 10f);
2025-11-25 21:49:03 -05:00
}
else
{
2026-04-01 12:23:27 -04:00
float movement = 0f;
if(AllowMovement())
2025-11-25 21:49:03 -05:00
{
2026-04-01 12:23:27 -04:00
movement = 2f * (Screen.width * 0.5f - Input.mousePosition.x) / (float)Screen.width;
if(Mathf.Abs(movement) < 0.6f)
2025-11-25 21:49:03 -05:00
{
movement = 0;
}
else if (Input.mousePosition.x < Screen.width && Input.mousePosition.x > 0)
{
movement += movement < 0 ? 0.6f : -0.6f;
movement *= 2f;
movement = Mathf.Clamp(movement, -1f, 1f);
}
else
{
movement = 0;
}
}
2026-04-01 12:23:27 -04:00
currentPosition = Vector3.Lerp(currentPosition, new Vector3(currentPosition.x + movement, 0, 0), Time.unscaledDeltaTime * 14f / scale);
2025-11-25 21:49:03 -05:00
}
transform.position = currentPosition * scale;
//Controls:
2026-04-01 12:23:27 -04:00
if(Demo_Display.selected != null)
{
2025-11-25 21:49:03 -05:00
if (Input.GetKeyDown(KeyCode.Escape))
2026-04-01 12:23:27 -04:00
{
2025-11-25 21:49:03 -05:00
Demo_Display.selected.Deselect();
2026-04-01 12:23:27 -04:00
}
}
2025-11-25 21:49:03 -05:00
}
public bool AllowMovement()
{
return zoomFactor < 0.1f;
}
public bool FadeInGUI()
{
return zoomFactor > 0.9f;
}
}
2026-04-01 12:23:27 -04:00
}