[Unity3D] how to recognize screen edges and unity3d Recognition

Source: Internet
Author: User

[Unity3D] how to recognize screen edges and unity3d Recognition
Problems

  • The unit of length in Unity3D is meters.
  • The unit of Screen information obtained using Screen. resolutions is pixel.

That is to say, even if the screen information and parameters are obtained, the information cannot be converted into information that can be used in the editor. At that time, I wanted to do Protect The Red.

So we started our Google journey.

 

I flipped through StackExchange and Unity Answers, and finally understood it. The general idea is like this.

 

Solution

 

Code Implementation

 

    [HideInInspector]public float leftBorder;    [HideInInspector]public float rightBorder;    [HideInInspector]public float topBorder;    [HideInInspector]public float downBorder;
    private float width;
    private float height;

    void SetBasicValues(){
      

        //the up right corner
        Vector3 cornerPos=Camera.main.ViewportToWorldPoint(new Vector3(1f,1f,
                                                                       Mathf.Abs(-Camera.main.transform.position.z)));
        
        leftBorder=Camera.main.transform.position.x-(cornerPos.x-Camera.main.transform.position.x);
        rightBorder=cornerPos.x;
        topBorder=cornerPos.y;
        downBorder=Camera.main.transform.position.y-(cornerPos.y-Camera.main.transform.position.y);

        width=rightBorder-leftBorder;
        height=topBorder-downBorder;

    }

 

Achievements-keep red

The final result is that the wall game remains on the four sides of the screen after running. Control the ball in the screen.

 

APK Download

Google Play

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.