Пример #1
0
    // 如果screenCount只在adaptScreen为AS_MULTI_SCREEN时才有效,且screenCount不能小于2
    protected void processResolution(int width, int height, int screenCount, ADAPT_SCREEN adaptScreen)
    {
        GameObject uiRootObj       = UnityUtility.getGameObject(null, "NGUIRoot");
        GameObject rootMultiScreen = UnityUtility.getGameObject(null, "NGUIRootMultiScreen");
        GameObject rootStretch     = UnityUtility.getGameObject(null, "NGUIRootStretch");

        // 首先默认禁用
        if (rootMultiScreen != null)
        {
            rootMultiScreen.SetActive(false);
        }
        if (rootMultiScreen != null)
        {
            rootStretch.SetActive(false);
        }
        if (adaptScreen == ADAPT_SCREEN.AS_SIMPLE_STRETCH)
        {
            rootStretch.SetActive(true);
            GameObject camera        = UnityUtility.getGameObject(rootStretch, "Camera");
            GameObject cameraTexture = UnityUtility.getGameObject(rootStretch, "UICameraTexture");
            camera.SetActive(true);
            cameraTexture.SetActive(true);
            UIRoot stretchRoot = rootStretch.GetComponent <UIRoot>();
            stretchRoot.scalingStyle       = UIRoot.Scaling.Flexible;
            stretchRoot.manualWidth        = width;
            stretchRoot.manualHeight       = height;
            camera.transform.localPosition = new Vector3(0.0f, 0.0f, -height / 2.0f);
            setTextureWindowSize(cameraTexture, 0, width, height);
            // 设置各个摄像机的渲染目标
            UITexture uiTexture = cameraTexture.GetComponent <UITexture>();
            setCameraTargetTexture(null, "MainCamera", uiTexture);
            setCameraTargetTexture(uiRootObj, "UICamera", uiTexture);
            setCameraTargetTexture(uiRootObj, "UIBackEffectCamera", uiTexture);
            setCameraTargetTexture(uiRootObj, "UIForeEffectCamera", uiTexture);
            setCameraTargetTexture(uiRootObj, "UIBlurCamera", uiTexture);
        }
        else
        {
            if (adaptScreen == ADAPT_SCREEN.AS_BASE_ON_ANCHOR)
            {
                setCameraTargetTexture(null, "MainCamera", null);
                setCameraTargetTexture(uiRootObj, "UICamera", null);
                setCameraTargetTexture(uiRootObj, "UIBackEffectCamera", null);
                setCameraTargetTexture(uiRootObj, "UIForeEffectCamera", null);
                setCameraTargetTexture(uiRootObj, "UIBlurCamera", null);
            }
            else if (adaptScreen == ADAPT_SCREEN.AS_MULTI_SCREEN && screenCount > 1)
            {
                // 激活渲染目标
                rootMultiScreen.SetActive(true);
                GameObject camera         = UnityUtility.getGameObject(rootMultiScreen, "Camera");
                GameObject cameraTexture0 = UnityUtility.getGameObject(rootMultiScreen, "UICameraTexture0");
                GameObject cameraTexture1 = UnityUtility.getGameObject(rootMultiScreen, "UICameraTexture1");
                camera.SetActive(true);
                cameraTexture0.SetActive(true);
                cameraTexture1.SetActive(true);
                // 设置渲染目标属性
                UIRoot uiRoot = rootMultiScreen.GetComponent <UIRoot>();
                uiRoot.scalingStyle            = UIRoot.Scaling.Constrained;
                uiRoot.manualWidth             = width;
                uiRoot.manualHeight            = height;
                camera.transform.localPosition = new Vector3(0.0f, 0.0f, -height / 2.0f);
                setTextureWindowSize(cameraTexture0, -width / screenCount, width / screenCount, height);
                setTextureWindowSize(cameraTexture1, -width / screenCount + width / 2, width / screenCount * (screenCount - 1), height);
                // 设置各个摄像机的渲染目标
                UITexture uiTexture = cameraTexture0.GetComponent <UITexture>();
                setCameraTargetTexture(null, "MainCamera", uiTexture);
                setCameraTargetTexture(uiRootObj, "UICamera", uiTexture);
                setCameraTargetTexture(uiRootObj, "UIBackEffectCamera", uiTexture);
                setCameraTargetTexture(uiRootObj, "UIForeEffectCamera", uiTexture);
                setCameraTargetTexture(uiRootObj, "UIBlurCamera", uiTexture);
            }
        }
    }
Пример #2
0
    // 如果screenCount只在adaptScreen为AS_MULTI_SCREEN时才有效,且screenCount不能小于2
    protected void processScreen(int width, int height, int screenCount, ADAPT_SCREEN adaptScreen, int fullScreen)
    {
#if UNITY_ANDROID || UNITY_IOS
        // 移动平台下固定为全屏
        fullScreen  = 1;
        screenCount = 1;
#endif
        if (fullScreen == 1)
        {
            width  = Screen.width;
            height = Screen.height;
        }
#if UNITY_EDITOR
        width  = GameDefine.STANDARD_WIDTH;
        height = GameDefine.STANDARD_HEIGHT;
#endif
        Screen.SetResolution(width, height, fullScreen == 1 || fullScreen == 3);
#if UNITY_EDITOR || (!UNITY_ANDROID && !UNITY_IOS)
        // 设置为无边框窗口
        if (fullScreen == 2)
        {
            // 无边框的设置有时候会失效,并且同样的设置,如果上一次设置失效后,即便恢复设置也同样会失效,也就是说本次的是否生效与上一次的结果有关
            // 当设置失效后,可以使用添加启动参数-popupwindow来实现无边框
            long curStyle = User32.GetWindowLong(User32.GetForegroundWindow(), CommonDefine.GWL_STYLE);
            curStyle &= ~CommonDefine.WS_BORDER;
            curStyle &= ~CommonDefine.WS_DLGFRAME;
            User32.SetWindowLong(User32.GetForegroundWindow(), CommonDefine.GWL_STYLE, curStyle);
        }
#endif
        GameObject uiRootObj       = UnityUtility.getGameObject(null, "NGUIRoot");
        GameObject rootMultiScreen = UnityUtility.getGameObject(null, "NGUIRootMultiScreen");
        GameObject rootStretch     = UnityUtility.getGameObject(null, "NGUIRootStretch");
        // 首先默认禁用
        if (rootMultiScreen != null)
        {
            rootMultiScreen.SetActive(false);
        }
        if (rootStretch != null)
        {
            rootStretch.SetActive(false);
        }
        // 简单拉伸自适应分辨率,将所有画面都渲染到NGUIRootStretch中的UICameraTexture上,然后拉伸显示UICameraTexture
        if (adaptScreen == ADAPT_SCREEN.AS_SIMPLE_STRETCH && rootStretch != null)
        {
            rootStretch.SetActive(true);
            GameObject camera        = UnityUtility.getGameObject(rootStretch, "Camera");
            GameObject cameraTexture = UnityUtility.getGameObject(rootStretch, "UICameraTexture");
            camera.SetActive(true);
            cameraTexture.SetActive(true);
            UIRoot stretchRoot = rootStretch.GetComponent <UIRoot>();
            stretchRoot.scalingStyle       = UIRoot.Scaling.Flexible;
            stretchRoot.manualWidth        = width;
            stretchRoot.manualHeight       = height;
            camera.transform.localPosition = new Vector3(0.0f, 0.0f, -height / 2.0f);
            setTextureWindowSize(cameraTexture, 0, width, height);
            // 设置各个摄像机的渲染目标
            UITexture uiTexture = cameraTexture.GetComponent <UITexture>();
            setCameraTargetTexture(null, "MainCamera", uiTexture);
            setCameraTargetTexture(uiRootObj, "UICamera", uiTexture);
            setCameraTargetTexture(uiRootObj, "UIBackEffectCamera", uiTexture);
            setCameraTargetTexture(uiRootObj, "UIForeEffectCamera", uiTexture);
            setCameraTargetTexture(uiRootObj, "UIBlurCamera", uiTexture);
        }
        else
        {
            // 多屏横向组合为高分辨率屏幕
            if (adaptScreen == ADAPT_SCREEN.AS_MULTI_SCREEN && screenCount > 1 && rootMultiScreen != null)
            {
                // 激活渲染目标
                rootMultiScreen.SetActive(true);
                GameObject camera         = UnityUtility.getGameObject(rootMultiScreen, "Camera");
                GameObject cameraTexture0 = UnityUtility.getGameObject(rootMultiScreen, "UICameraTexture0");
                GameObject cameraTexture1 = UnityUtility.getGameObject(rootMultiScreen, "UICameraTexture1");
                camera.SetActive(true);
                cameraTexture0.SetActive(true);
                cameraTexture1.SetActive(true);
                // 设置渲染目标属性
                UIRoot uiRoot = rootMultiScreen.GetComponent <UIRoot>();
                uiRoot.scalingStyle            = UIRoot.Scaling.Constrained;
                uiRoot.manualWidth             = width;
                uiRoot.manualHeight            = height;
                camera.transform.localPosition = new Vector3(0.0f, 0.0f, -height / 2.0f);
                setTextureWindowSize(cameraTexture0, -width / screenCount, width / screenCount, height);
                setTextureWindowSize(cameraTexture1, -width / screenCount + width / 2, width / screenCount * (screenCount - 1), height);
                // 设置各个摄像机的渲染目标
                UITexture uiTexture = cameraTexture0.GetComponent <UITexture>();
                setCameraTargetTexture(null, "MainCamera", uiTexture);
                setCameraTargetTexture(uiRootObj, "UICamera", uiTexture);
                setCameraTargetTexture(uiRootObj, "UIBackEffectCamera", uiTexture);
                setCameraTargetTexture(uiRootObj, "UIForeEffectCamera", uiTexture);
                setCameraTargetTexture(uiRootObj, "UIBlurCamera", uiTexture);
            }
        }
    }