2019年6月16日 星期日

Unity 學習筆記 - 設置手機螢幕邊框的透明圍牆


1. 新建立GameObject ,並重新命名

2. 結構如下,位置擺在座標(0,0,0)即可,將會用腳本另外控制所在位置

3. WallManager下掛在新建之腳本WallManager.cs

4. 腳本內容
public class WallManager : MonoBehaviour {

    private BoxCollider2D rightWall;
    private BoxCollider2D leftWall;
    private BoxCollider2D upWall;
    private BoxCollider2D downWall;

    // Use this for initialization
    void Start () {
        ResetWall();
    }
    
     // Update is called once per frame
     void Update () {
         
     }

    void ResetWall()
    {
        // 定義螢幕外的圍牆位置
        rightWall = transform.Find("rightWall").GetComponent<BoxCollider2D>();
        leftWall = transform.Find("leftWall").GetComponent<BoxCollider2D>();
        upWall = transform.Find("upWall").GetComponent<BoxCollider2D>();
        downWall = transform.Find("downWall").GetComponent<BoxCollider2D>();

        Vector3 tempPosition = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
        upWall.transform.position = new Vector3(0, tempPosition.y + 0.5f, 0);
        upWall.size = new Vector2(tempPosition.x * 2, 1);
        downWall.transform.position = new Vector3(0, -tempPosition.y - 0.5f, 0);
        downWall.size = new Vector2(tempPosition.x * 2, 1);
        leftWall.transform.position = new Vector3(-tempPosition.x - 0.5f, 0, 0);
        leftWall.size = new Vector2(1, tempPosition.y * 2);
        rightWall.transform.position = new Vector3(tempPosition.x + 0.5f, 0, 0);
        rightWall.size = new Vector2(1, tempPosition.y * 2);
    }
}

沒有留言:

張貼留言

[Android][App] Pin Poke Challenge 隱私權保護政策

非常歡迎您使用「 Pin Poke Challenge 」,為了讓您能夠安心使用此 App 的各項服務與資訊,特此向您說明 Pin Poke Challenge的隱私權保護政策,以保障您的權益,請您詳閱下列內容: 一、隱私權保護政策的適用範圍 隱私權保護政策內容,包括...