Sprite fill screen upon button press (2d slender page)
I want to make a 2d slender page effect. Upon pressing enter in a certain spot, a sprite will fill the screen. How do I do this? Can you post the script?
Answer by TBruce · Dec 09, 2016 at 10:34 PM
Hi @Nova-1504,
I am not sure I follow you when you say
Upon pressing enter in a certain spot
You can mouse click any spot on the screen but pressing enter does not apply to a screen location. Here is a script that hopefully does as you requested with a small addition which you can change or ignore - instead of a single sprite the script has a list of sprites allowing you to add as many sprites as you want to it and cycles through the list on every press of the enter key. You do not need to add more than one sprite though. The script is assuming a 2D game
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ScreenFill : MonoBehaviour
{
public List<Sprite> sprites = new List<Sprite>();
private GameObject go;
private SpriteRenderer spriteRenderer;
private int currentSprite = 0;
void Start()
{
if (sprites.Count > 0)
{
// remove any values not set in the inspector
for (int i = sprites.Count - 1; i >= 0; i --)
{
if (sprites[i] == null)
{
sprites.RemoveAt(i);
}
}
}
}
void Update()
{
if ((Input.GetKeyDown(KeyCode.Return)) && (sprites.Count > 0))
{
if (go == null)
{
go = new GameObject();
go.transform.position = Vector3.zero;
go.name = "Sprite Object"; // name this how you like
}
go.transform.localScale = new Vector3(1, 1, 1);
if (go.GetComponent<SpriteRenderer>() == null)
{
spriteRenderer = go.AddComponent<SpriteRenderer>();
}
spriteRenderer.sprite = sprites[currentSprite];
float multiplier = Camera.main.orthographicSize * Camera.main.nearClipPlane;
go.transform.localScale = new Vector3(((float)Screen.width / (float)spriteRenderer.sprite.texture.width) * multiplier, ((float)Screen.height / (float)spriteRenderer.sprite.texture.height) * multiplier, 1);
if (sprites.Count > 1)
{
currentSprite++;
if (currentSprite >= sprites.Count)
{
currentSprite = 0;
}
}
}
}
}
But here another method using a Canvas and and an Image which may be more what you are looking for (requires a Canvas)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class ScreenFill : MonoBehaviour
{
public List<Sprite> sprites = new List<Sprite>();
public Canvas canvas;
private SpriteRenderer spriteRenderer;
private Image image;
private int currentSprite = 0;
void Start()
{
if (canvas == null)
{
Debug.LogError("The canvas field was not set in the inspector, please set and try again.");
if (GameObject.Find("Canvas") != null)
{
canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
}
}
if (sprites.Count > 0)
{
// remove any values not set in the inspector
for (int i = sprites.Count - 1; i >= 0; i --)
{
if (sprites[i] == null)
{
sprites.RemoveAt(i);
}
}
}
}
void Update()
{
if ((Input.GetKeyDown(KeyCode.Return)) && (sprites.Count > 0) && (canvas != null))
{
if (image == null)
{
GameObject go = new GameObject();
go.name = "Full Screen Panel"; // name this how you like
RectTransform rt = go.AddComponent<RectTransform>();
go.transform.SetParent(canvas.gameObject.transform, false);
rt.sizeDelta = Vector2.zero;
rt.anchorMin = Vector2.zero;
rt.anchorMax = new Vector2(1, 1);
image = go.AddComponent<Image>();
image.color = Color.white;
}
image.sprite = sprites[currentSprite];
if (sprites.Count > 1)
{
currentSprite++;
if (currentSprite >= sprites.Count)
{
currentSprite = 0;
}
}
}
}
}
Both methods have been tested and work I personally like the second method best.
Edit: If you do want to perform the screen fill on a mouse click instead of the ENTER
key, there are two ways to go about it, method one uses the OnMouseDown()
function placed somewhere in your class , and method two uses Input.GetMouseButton()
generally called in the Update()
function though it can be used in combination with method one.
With method one, a collider is required on the game object the script with OnMouseDown()
is placed. OnMouseDown()
will be entered when the object itself is clicked. As mentioned you can also use Input.GetMouseButton()
within the OnMouseDown()
function to check for a specific button press.
With method two the player can click any part of the screen when it is used within the Update
update function.
Below are three examples you can use with any if the scripts above
Example 1
void Update()
{
// instead of on the return key on the left click mouse button
if ((Input.GetMouseButtonDown(0)) && (sprites.Count > 0) && (canvas != null))
{
...
}
}
Example 2
// this function will be called when any mouse button is
// clicked on the game object this script is attached to
void OnMouseDown()
{
if ((sprites.Count > 0) && (canvas != null))
{
...
}
}
Example 3
// this function will be called when the left mouse button is
// clicked on the game object this script is attached to
void OnMouseDown()
{
if ((Input.GetMouseButtonDown(0)) && (sprites.Count > 0) && (canvas != null))
{
...
}
}
You only need to place the code I provided from one of the classes above where you see the ...
above.