找回密码
 立即注册
楼主: smilelance

[GUI] unity3d GUI上显示3d模型

[复制链接]
发表于 2012-11-21 22:09 | 显示全部楼层 |阅读模式
本帖最后由 多米诺 于 2012-11-22 13:34 编辑

我们的游戏选择角色的时候需要在2d GUI的上层显示3d模型,默认3d模型是显示在2d GUI下面的,所以需要另外创建一个Camera,并且把新建的Camera 设置 target 到 RenderTexture,然后把这个RenderTexture显示到GUI上就行了。
    代码如下:
Create Camera:
            _myObject = new GameObject(_Name, typeof(Camera), typeof(Skybox));
            _myCamera = _myObject.camera;
            _myCamera .depth = 2;
            _myCamera .clearFlags = CameraClearFlags.Skybox;
            _myCamera .backgroundColor = Color.clear;//Color.black;
            _myCamera .nearClipPlane = 0.3f;
            _myCamera .fieldOfView = 60;
            _myCamera .orthographicSize = 100.0f;
Create RenderTexture:
    private static void InitTexture(int with, int height)
    {
//       _myTexture = new RenderTexture(with, height, 2);

_myTexture= RenderTexture.GetTemporary(with, height, 0, RenderTextureFormat.ARGB32);
        _myTexture.name = "storyTexture" + _storyObject.GetInstanceID();
        _myTexture.isPowerOfTwo = false;
        _myCamera .targetTexture = _storyTexture;
    }
Draw RenderTexture:
//GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), _storyTexture,ScaleMode.StretchToFill);
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), _storyTexture,ScaleMode.ScaleToFit);
移动端有一些需要注意的地方:
1、创建RenderTexture的时候,如果直接new到iphone上背景就是黑的,晚上找到解决办法如下:

When setting up the Camera object, a RenderTexture is needed as the targetTexture for rendering. If you use a dynamically created Camera, you have one by default whose depth is 24. But it seems iOS devices only support depth = 0, so there are some problems.

To walk around, add a statement like this:

myCamera.targetTexture = RenderTexture.GetTemporary(myWidth, myHeight, 0, myFormat);

where the third parameter is the depth of the RenderTexture.
2、把RenderTexture画到屏幕上的时候,如果选择StretchToFill,手机上就会,需要选择ScaleToFit

评分

参与人数 1鲜花 +2 收起 理由
mice714 + 2

查看全部评分

发表于 2014-4-20 07:54 | 显示全部楼层
学习,顶一下!
发表于 2017-4-3 16:21 | 显示全部楼层
楼主是超人
发表于 2017-4-3 15:52 | 显示全部楼层
真心顶
发表于 2017-4-3 16:09 | 显示全部楼层
难得一见的好帖
发表于 2017-4-3 16:25 | 显示全部楼层
说的非常好
发表于 2017-4-3 15:50 | 显示全部楼层
LZ真是人才
发表于 2017-5-9 22:09 | 显示全部楼层
顶顶多好
发表于 2017-5-9 22:18 | 显示全部楼层
真心顶
发表于 2017-5-9 22:35 | 显示全部楼层
说的非常好
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-2 20:00 , Processed in 0.228287 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表