Unity3d根据位置渲染纹理

时间:2013-07-30 09:58:12

标签: c# unity3d

我目前正在尝试在Unity中创建一个容器,内部有多个四边形,我正在寻找一种方法来阻止四边形部分超出容器界限而不被渲染?

我花了二十分钟试图找出如何正确解释问题(并且失败了),所以创造了这张漂亮的照片。红线代表黑色方块的边界,代表我的纹理四边形。

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个:

private Rect ClipRectToScreen (Rect input)
                    {
                            // Special handling for screen reading of render texture so it always stay in visible area.
                            // Will throw error withoud this fix.
                            Rect r;
                            r = new Rect (input.x, (Screen.height - input.y - input.height) , input.width, input.height);
                            if (r.x < 0f) {
                                    r.width = r.width - UnityEngine.Mathf.Abs (r.x);
                                    r.x = 0f;
    //                              Debug.Log ("x < 0");
                            }
                            if (r.y < 0f) {
                                    r.height = r.height - UnityEngine.Mathf.Abs (r.y);
                                    r.y = 0f;
    //                              Debug.Log ("y < 0");
                            }
                            if (r.x > Screen.width) {
                                    r.width = r.width - UnityEngine.Mathf.Abs (r.x);
                                    r.x = 0f;
    //                              Debug.Log ("x > Screen.width");
                            }
                            if (r.y > Screen.height) {
                                    r.height = r.height - UnityEngine.Mathf.Abs (r.y);
                                    r.y = 0f;
    //                              Debug.Log ("y > Screen.height");
                            }
                            if ((r.x + r.width) > Screen.width) {
                                    r.width = Screen.width - r.x;
    //                              Debug.Log ("r.x + r.width > Screen.width");
                            }
                            if ((r.y + r.height) > Screen.height) {
                                    r.height = Screen.height - r.y;
    //                              Debug.Log ("r.y + r.height > Screen.height");
                            }

                            return r;
                    }