Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Nova-1504 · Dec 01, 2016 at 05:53 PM · 2dtriggerplatformergetkeyupslender

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nova-1504 · Dec 09, 2016 at 03:08 PM 0
Share

Hello? is anyone going to answer?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to get bool statement to work in OnTriggerStay2D 1 Answer

Gradually rotate to slope angle 3 Answers

OnTriggerEnter and -Exit not getting called 1 Answer

OnTriggerEnter2D not called after collision (2D game) 2 Answers

Should i use rigidbody for platformer movement? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges