查看: 1386|回复: 2
打印 上一主题 下一主题

【转载】unity3d GUI上显示3d模型

[复制链接]
may    

8830

主题

81

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52344
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2012-11-22 01:08:05 |只看该作者 |倒序浏览
作者:smilelance



我们的游戏选择角色的时候需要在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;

        _myTexture.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
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

955

主题

164

听众

7万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
59338
精华
28

活跃会员 荣誉管理 突出贡献 优秀版主 论坛元老

沙发
发表于 2012-11-22 23:26:18 |只看该作者
这是不错的代码,感谢!
回复

使用道具 举报

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-11-24 02:18:25 |只看该作者
学习中!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-20 14:18 , Processed in 0.071975 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部