找回密码
 立即注册

平台游戏进度条

热度 1已有 1214 次阅读2012-11-29 10:03 |个人分类:U3D| 进度, 如何

平台游戏进度条

这是最近你们很多人都希望我做的——一个关于如何在游戏进程中追踪角色进度的unity教程。

为了在一个平台游戏中保持玩家的注意力,你可能想要向他们展示一些作为他们游戏进度指示的HUD条。这里是一个使用GUI命令和一个纹理来实现这个功能的例子,跟往常一样,受网站宽度的限制,脚本在这里提供——http://www.pasteit4me.com/2269002(这个地址好像显示不出来,代码下面有==!)


第一步

在这个例子中,我们简单地使用两个对象作为起始位置——他们可以使空对象或者碰撞器来停止正在运动的角色,这取决于你。记住,这个例子只是针对二维层次的进度追踪设计的,所以没有将高度考虑进去。


第二步

编写下面的脚本并将它赋予到一个空物体上。在脚本组件的监视器中设定你想要的进度条宽度和高度。然后将角色拖动到角色变量的位置,将起始位置的对象也拖到相应的这些变量上(这些变量均在脚本组件的监视器中可以找到)。


第三步

为进度条的图标设计一个纹理——我很简单地使用了一个在photoshop里快速制作的1 x20像素的线条。

就是这样了!

视频说明稍后呈上btw...





  1. //Script by Will Goldstone at Unity3dstudent.com

  2. // set GUI bar width and height in the Inspector
  3. var barWidth : float = 500;
  4. var barHeight : float = 25;

  5. // drag a texture as the icon to move on the progress bar
  6. var progIcon : Texture;

  7. // where to set the GUI element to
  8. private var barProgress : float;

  9. // empty objects represent the start and end of a level
  10. var startPoint : Transform;
  11. var endPoint : Transform;

  12. // current Player position
  13. var playerPos : Transform;

  14. function Update(){
  15. // get level distance by subtracting start and end
  16. var totalDist : float = endPoint.position.x – startPoint.position.x;

  17. // get player distance from start in X axis only so slopes / height doesn't affect result
  18. var playerDist : float = playerPos.position.x – startPoint.position.x;

  19. //get player's progress as a percentage of the whole distance
  20. var playerProgress : float = playerDist / totalDist * 100;

  21. //turn the playerProgress percentage back into the scale of barWidth
  22. barProgress = playerProgress / 100 * barWidth;

  23. }

  24. function OnGUI() {
  25. // create a GUI group the width of the bar and twice its height
  26. // in order to leave room for 'Start' and 'End' text under the bar
  27. GUI.BeginGroup (new Rect (10, 10, barWidth, barHeight*2));

  28.   //draw a box as the backing for the progress bar, blank text inside
  29.   GUI.Box(Rect(0,0,barWidth,barHeight),"");

  30.   // create a label to draw the progress icon texture, use barProgress var
  31.   // to set its X position, 0 as the Y position and width and height of the texture used
  32.   GUI.Label (Rect (barProgress, 0, progIcon.width, progIcon.height),
  33.         progIcon);

  34.   // add start and end labels
  35.   GUI.Label(Rect(progIcon.width/2, 25, 50, barHeight),"Start");
  36.   GUI.Label(Rect(barWidth-30, 25, 100, barHeight),"End");

  37. GUI.EndGroup();
  38. }

路过

雷人

握手
1

鲜花

鸡蛋

刚表态过的朋友 (1 人)

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

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

GMT+8, 2024-6-4 10:43 , Processed in 0.044274 second(s), 16 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

返回顶部