查看: 2906|回复: 8
打印 上一主题 下一主题

unity3d中制作一个非方形的按钮详细教程第三节,添加代码

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

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

跳转到指定楼层
楼主
发表于 2011-11-8 15:43:00 |只看该作者 |倒序浏览




            制作非方形系列教程的最后一节,用到了较多的代码,还是看原文吧:
         

           This is the last post of this series, which explains how to create non-rectangular GUI buttons for unity3d games. If you haven’t read any of the other posts yet, it is highly recommended that you take a look at part oneand part two before going any further.
         

           So, let’s start from where the second post left: we already have our Unity3D scene set up with the 3D hit test model already imported and placed inside the scene. All we have to do now is import the PNG images and make then appear as a GUI element. To do that, just drag and drop then somewhere inside the Project tab. After the images are inside Unity3D, create a GUISkin by right clicking an empty space in this same tab and select Create->GUISkin as shown:
         

            
           


           Click on 'Create' and then on 'GUISkin'
         

           Name this GUISkin as IrregularShapeSkin. The next step is to add the images we just imported to the recently created GUISkin. To do this, each state of the button, such as normal, hover and down must be assigned to a different Custom Styles slot. To do that, expand the Custom Styles tree and set the size to 3 or whichever number of buttons or button states you currently have. Then, just drag and drop the images from the Project Tab to the Normal->Background of each slot:
         

            
           




           This image shows how to assign a image to a Custom Style slot.
         

           Finally, let’s see the code that makes the non-rectangular buttons work. The code is much simpler than the scene’s setup we’ve being preparing all these posts. It must be attached to the Main Camera. Here it is:
           

            
         

           using UnityEngine;
         

           using System.Collections;
         

           public class CustomShapeGUI : MonoBehaviour
         

           {
         

           //a variable to store the GUISkin
         

           public GUISkin guiskin;
         

           //a variable to store the GUI camera
         

           public Camera cShapeGUIcamera;
         

           //a variable that is used to check if the mouse is over the button
         

           private bool isHovering = false;
         

           //a variable that is used to check if the mouse has been clicked
         

           private bool isDown = false;
         

           //a ray to be cast
         

           private Ray ray;
         

           //create a RaycastHit to query information about the collisions
         

           private RaycastHit hit;
         

           void Update()
         

           {
         

           //cast a ray based on the mouse position on the screen
         

           ray = cShapeGUIcamera.ScreenPointToRay(Input.mousePosition);
         

           //Check for raycast collisions with anything
         

                   if (Physics.Raycast(ray, out hit, 10))
         

           {
         

           //if the name of what we have collided is "irregular
           


            _
           
           shape"
         

           if(hit.transform.name=="irregular
           
            _
           
           shape")
         

           {
         

           //set collided variable to true
         

           isHovering = true;
         

           //if the mouse buton have been pressed while the cursor was over the button
         

           if(Input.GetButton("Fire1"))
         

           {
         

           //if clicked, mouse button is down
         

           isDown = true;
         

           }
         

           else
         

           {
         

           //the mouse button have been released
         

           isDown = false;
         

           }
         

           }
         

                   }
         

           else //ray is not colliding,
         

           {
         

           //set collided to false
         

           isHovering = false;
         

           }
         

           }
         

           void OnGUI()
         

           {
         

           //if mouse cursor is not inside the button area
         

           if(!isHovering)
         

           {
         

           //draws the normal state
         

           GUI.Label(new Rect(10,10,161,145),"",guiskin.customStyles[0]);
         

           //set mouse click down to false
         

           isDown = false;
         

           }
         

           else //mouse is inside the button area
         

           {
         

           //draws the hover state
         

           GUI.Label(new Rect(10,10,161,145),"",guiskin.customStyles[1]);
         

           //if the mouse has been clicked while the cursor was over the button
         

           if(isDown)
         

           {
         

           //draws the 'Pressed' state
         

           GUI.Label(new Rect(10,10,161,145),"",guiskin.customStyles[2]);
         

           }
         

           }
         

           }
         

           }
         

           This is how this code works: the public variables define the Camera that the ray will be cast into and the GUISkin being used. Don’t forget to drag and drop the Camera and specially the GUISkin we created yearlier before running the code. If you don’t, the lack of a defined GUISkin can make Unity3D crash. This is a image of the cShapeGUIcamera variable and the guiskin variable set up, and defined, at the Inspector.
         

            
           






           Image of the Main Camera Inspector showing the CustomShapeGUI script attached to it. Don't forget to assign these variables.
         

           The private variables are used to create the ray, set the button states (normal, hover, pressed) and to query information about the object the ray collided with (that’s the purpose of the RaycastHitvariable named hit). Actually, for this example, this RaycastHit variable wasn’t needed, since we only have one button. It was added to the script to make it possible to add more buttons later.
         

           Basically, the Update() method checks for objects intersecting the ray within a 10 unit range (line 29). The ray has its origin in the dedicated GUI Camera and points forward. The ray’s origin and direction are updated every frame (line 26). If there was a collision, check what is the name of the object we collided with, and then, set the state of the button based on the mouse button input (if statement that starts in line 29 and and ends on line 49).
         

           Finally, the OnGUI() renders the button on the screen at a specified coordinate and with the current state based on the the three boolean variables.
         

           Since the code defines where the button is drawn on the screen, the last thing needed to be done is to scale and place the “irregular shape” 3D model exactly behind the 2D GUI. The 3D model will serve as the hit test area for the button, that’s why it needs to be placed precisely behind the 2D GUI, so it doesn’t appear.
         

            
           






           3D model being positioned behind the 2D GUI Image.
         

           The image above shows the 3D hit test area model being aligned with the 2D image. Note that the button’s PNG file isn’t transparent. The image above is like this, because, to precisely position the 3D hit test model, this line of code was added at the beginning of the OnGUI() method to make all GUI elements semi-transparent.
         

           //place this line at the beginning of the OnGUI method  
           

           GUI.color = new Color(0.5f,0.5f,0.5f,0.5f);  
           

           After you found the position and size to match the 2D image, just delete this line.
           

           Please run this in the Web or the Standalone resolutions, as this code doesn’t resize the GUI or the 3D model based on the screen resolution. That was an intended limitation. It would have required much more code to implement this feature, and at least one more post to explain it.
         

           That’s it!
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

沙发
发表于 2012-2-12 23:32:37 |只看该作者
好可爱的字,学习了
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

板凳
发表于 2012-6-4 23:20:15 |只看该作者
好`我顶``顶顶
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

地板
发表于 2012-6-7 23:20:25 |只看该作者
加精、加亮滴铁子,尤其要多丁页丁页
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

5#
发表于 2012-6-17 23:21:27 |只看该作者
加精、加亮滴铁子,尤其要多丁页丁页
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

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

6#
发表于 2012-7-13 23:21:46 |只看该作者
既来之,则看之!
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

7#
发表于 2012-7-29 23:27:29 |只看该作者
我是老实人,我来也!
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

8#
发表于 2012-9-27 23:23:07 |只看该作者
路过、路过、快到鸟,列位请继续...ing
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

9#
发表于 2013-1-28 23:26:46 |只看该作者
楼主收集的可真全哦
回复

使用道具 举报

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

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

GMT+8, 2025-7-27 07:24 , Processed in 0.104989 second(s), 29 queries .

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

© 2008-2019 Narkii Inc.

回顶部