1、计算机游戏程序设计 实验指导书 实验一计算机游戏程序设计实验指导书实验一 GUI游戏界面的实现一、实验目的与要求1. 熟悉及掌握GUI的高级控件,以及用法。2. 掌握GUI自定义皮肤用法 3. 熟悉GUILayout的使用。4. 熟悉2D贴图的绘制和帧动画的实现方法。二、实验内容及步骤1. 熟悉GUI高级控件,练习使用GUI的高级控件制作23个游戏界面。2. 练习使用GUI自定义皮肤,实现游戏界面的字体,背景颜色等设置。3. 熟悉GUILayout的使用,联系使用GUILayout的水平线性布局和垂直线性布局,并加适当偏移。4. 熟悉2D贴图的绘制和帧动画的实现方法,练习在界面中绘制静态图片和
2、动画。三、实验仪器与软件1. PC计算机2. Unity 3D软件 四、实验报告要求1. 熟悉GUI高级控件,练习使用GUI的高级控件制作23个游戏界面。Label控件:using UnityEngine;using System.Collections;public class kongjian : MonoBehaviour public Texture imageTexture; private int imageWidth; private int imageHeight; private int screenWidth; private int screenHeight; / Use
3、 this for initialization void Start () /得到屏幕宽高 screenWidth = Screen.width; screenHeight = Screen.height; /得到图片宽高 imageWidth = imageTexture.width; imageHeight = imageTexture.height; void OnGUI() /将文字内容显示在屏幕中 GUI.Label(new Rect(100, 10, 100, 30), hello unity); /将贴图显示在屏幕中 GUI.Label(new Rect(100, 120, i
4、mageWidth, imageHeight),imageTexture); / Update is called once per frame void Update () Buttonusing UnityEngine;using System.Collections;public class kongjian : MonoBehaviour public Texture buttonTexture; private string str; / Use this for initialization void Start () str=请点击按钮!; void OnGUI() GUI.La
5、bel(new Rect(10, 10, Screen.width, 30), str); if(GUI.Button(new Rect(10,50,buttonTexture.width,buttonTexture.height),buttonTexture) /点击按钮修改提示信息 str = 您点击了图片按钮; /设置按钮中文字的颜色 GUI.color = Color.green; /设置按钮的背景色 GUI.backgroundColor = Color.black; / Update is called once per frame void Update () 2. 练习使用GU
6、I自定义皮肤,实现游戏界面的字体,背景颜色等设置。using UnityEngine;using System.Collections;public class NewBehaviourScript : MonoBehaviour /自定义皮肤 public GUISkin liuna; /单选是否选中 private bool choose = false; /拖动窗口的位置 private Rect windowRect = new Rect (20, 20, 120, 50); /输入框中默认显示 private string edit= 请输入字符串; / Use this for i
7、nitialization void Start () void OnGUI() /设置GUI皮肤为我们自定义皮肤 GUI.skin = liuna; /绘制自定义按钮 GUI.Button(new Rect (100,100,100,100),点我); /单项选择 choose = GUI.Toggle(new Rect(10, 50, 100, 30), choose, 单项选择); /输入框 edit = GUI.TextField (new Rect (200, 10, 200, 20), edit, 25); /注册窗口 windowRect = GUI.Window (0, win
8、dowRect, setWindow, 这是一个窗口); /设置GUI皮肤为系统定义皮肤 GUI.skin = null; /绘制系统自带按钮 void setWindow (int windowID) /创建一个可以自由拖动的窗口 GUI.DragWindow (); /绘制自定义按钮 / Update is called once per frame void Update () 3. 熟悉GUILayout的使用,练习使用GUILayout的水平线性布局和垂直线性布局,并加适当偏移。using UnityEngine;using System.Collections;public cla
9、ss NewBehaviourScript1 : MonoBehaviour / Use this for initialization void Start () void OnGUI() /开始一个显示区域 GUILayout.BeginArea (new Rect (100,100,200,60); /开始最外层横向布局 GUILayout.BeginHorizontal (); /嵌套一个纵向布局 GUILayout.BeginVertical (); GUILayout.Box(One); /两个box中间偏移10像素 GUILayout.Space (10); GUILayout.
10、Box(Two); /结束嵌套的纵向局部 GUILayout.EndVertical (); /两个纵向布局中间偏移20像素 GUILayout.Space (20); /嵌套一个纵向布局 GUILayout.BeginVertical (); GUILayout.Box(Three); /两个box中间偏移10像素 GUILayout.Space (10); GUILayout.Box(Four); /结束嵌套的纵向局部 GUILayout.EndVertical (); /结束最外层横向布局 GUILayout.EndHorizontal (); /结束显示区域 GUILayout.End
11、Area(); / Update is called once per frame void Update () 4. 熟悉2D贴图的绘制和帧动画的实现方法,练习在界面中绘制静态图2D贴图using UnityEngine;using System.Collections;public class NewBehaviourScript : MonoBehaviour private Texture2D texSingle; /private Object texAll; void OnGUI() if (GUI.Button(new Rect (0, 10, 100, 50), 加载一张图片)
12、 if (texSingle = null) texSingle = (Texture2D)Resources.Load(flower0/0); if (texSingle!=null) GUI.DrawTexture(new Rect(110,10,120,120),texSingle,ScaleMode.StretchToFill,true,0); / Use this for initialization void Start () / Update is called once per frame void Update () 帧动画using UnityEngine;using Sy
13、stem.Collections;public class NewBehaviourScript1 : MonoBehaviour /动画数组 private Object animUp; private Object animDown; private Object animLeft; private Object animRight; /地图贴图 private Texture2D map; /当前人物动画 private Object tex; /人物X坐标 private int x; /人物Y坐标 private int y; /帧序列 private int nowFram; /动
14、画帧的总数 private int mFrameCount; /限制一秒多少帧 private float fps = 10; /限制帧的时间 private float time = 0; / Use this for initialization void Start () x=200; y=200; /得到帧动画中的所有图片资源 animUp = Resources.LoadAll(up); animDown = Resources.LoadAll(down); animLeft = Resources.LoadAll(left); animRight = Resources.LoadA
15、ll(right); /得到地图资源 map = (Texture2D)Resources.Load(map/ground); /设置默认动画 tex = animUp; void OnGUI() /绘制贴图 GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), map, ScaleMode.StretchToFill, true, 0); /绘制帧动画 DrawAnimation(tex,new Rect(x,y,32,48); /点击按钮移动人物 if(GUILayout.RepeatButton(向上) y-=2; tex =
16、 animUp; if(GUILayout.RepeatButton(向下) y+=2; tex = animDown; if(GUILayout.RepeatButton(向左) x-=2; tex = animLeft; if(GUILayout.RepeatButton(向右) x+=2; tex = animRight; /点击上下左右键移动人物 if(Input.GetKey(KeyCode.UpArrow) y-=2; tex = animUp; if(Input.GetKey(KeyCode.DownArrow) y+=2; tex = animDown; if(Input.Ge
17、tKey(KeyCode.LeftArrow) x-=2; tex = animLeft; if(Input.GetKey(KeyCode.RightArrow) x+=2; tex = animRight; void DrawAnimation(Object tex, Rect rect) /绘制当前帧 GUI.DrawTexture(rect, (Texture2D)texnowFram, ScaleMode.StretchToFill, true, 0); /计算限制帧时间 time += Time.deltaTime; /超过限制帧则切换图片 if(time = 2.0 / fps)
18、/帧序列切换 nowFram+; /限制帧清空 time = 0; /超过帧动画总数从第0帧开始 if(nowFram = tex.Length) nowFram = 0; / Update is called once per frame void Update () 实验二 Unity游戏脚本软件122班 刘娜 122513一、实验目的与要求1. 熟悉及掌握MonoDevelop脚本编辑器的使用方法。2. Unity脚本的生命周期。3. 熟练使用脚本来操作游戏对象。二、实验内容及步骤1. 熟悉MonoDevelop脚本编辑器的使用方法,联系实现脚本调试。2. 编程实现创建游戏对象(立方体,
19、球体),给游戏对象命名,改变颜色,添加刚体组件。3. 分别通过名称、标签获得游戏对象,通过标签获得多个游戏对象。4. 通过脚本控制游戏对象,改变游戏对象的位置,旋转游戏对象,缩放游戏对象。三、实验仪器与软件1. PC计算机2. Unity 3D软件 四、实验报告要求1. 熟悉MonoDevelop脚本编辑器的使用方法,联系实现脚本调试。创建C#脚本:在脚本中编辑代码2. 编程实现创建游戏对象(立方体,球体),给游戏对象命名,改变颜色,添加刚体组件。using UnityEngine;using System.Collections;public class NewBehaviourScript
20、 : MonoBehaviour / Use this for initialization void Start () / Update is called once per frame void Update () void OnGUI() if (GUI.Button (new Rect (0, 0, 100, 100), 创建立方体) GameObject objCube=GameObject.CreatePrimitive(PrimitiveType.Cube); objCube.AddComponent(Rigidbody); objCube.name=Cube; objCube.
21、renderer.material.color=Color.blue; if (GUI.Button (new Rect (100, 0, 100, 100), 创建球体) GameObject objSphere=GameObject.CreatePrimitive(PrimitiveType.Sphere); objSphere.AddComponent(Rigidbody); objSphere.name=Sphere; objSphere.renderer.material.color=Color.green; 受到重力降落:3. 分别通过名称、标签获得游戏对象,通过标签获得多个游戏对
22、象。通过名称获得游戏对象:using UnityEngine;using System.Collections;public class NewBehaviourScript1 : MonoBehaviour private GameObject objCube; private GameObject objSphere; private bool isCubeRoate=false; private bool isSphereRoate=false; private string CubeInfo=旋转立方体; private string SphereInfo=旋转球体; / Use th
23、is for initialization void Start () objCube=GameObject.Find(Cube); objSphere=GameObject.Find(Sphere); / Update is called once per frame void Update () if (isCubeRoate) if(objCube)/档立方体不为null时旋转 objCube.transform.Rotate(0.0f,Time.deltaTime*200,0.0f); if (isSphereRoate) if(objSphere)/档立方体不为null时旋转 obj
24、Sphere.transform.Rotate(0.0f,Time.deltaTime*200,0.0f); void OnGUI() if (GUI.Button (new Rect (0, 0, 100, 100), CubeInfo) isCubeRoate = true; CubeInfo = 停止旋转; else isCubeRoate = false; CubeInfo = 旋转立方体; if (GUI.Button (new Rect (0,100 , 100, 100), SphereInfo) isSphereRoate = true; SphereInfo = 停止旋转;
25、else isSphereRoate = false; SphereInfo = 旋转球体; 通过标签来获取游戏对象给Cube添加一个标签代码改为:void Start () objCube=GameObject.FindWithTag(tag1); objSphere=GameObject.Find(Sphere); 通过标签获取多个游戏对象:void Start () objCube=GameObject.FindGameObjectsWithTag(tag1); objSphere=GameObject.Find(Sphere); 4. 通过脚本控制游戏对象,改变游戏对象的位置,旋转游戏
26、对象,缩放游戏对象。改变游戏对象的位置:using UnityEngine;using System.Collections;public class NewBehaviourScript2 : MonoBehaviour private float x=0.0f; private float y=0.0f; private float z=0.0f; private GameObject obj; / Use this for initialization void Start () obj=GameObject.Find(Cube); / Update is called once per
27、 frame void Update () void OnGUI() GUI.Box (new Rect(0,0,100,100),移动立方体x轴); x = GUILayout.HorizontalSlider (x, -10.0f, 10.0f, GUILayout.Width (400); GUI.Box (new Rect(0,100,100,100),移动立方体y轴); y = GUILayout.HorizontalSlider (y, -10.0f, 10.0f, GUILayout.Width (400); GUI.Box (new Rect(0,200,100,100),移动立方体z轴); z= GUILayout.HorizontalSlider (z, -10.0f, 10.0f, GUILayout.Width (400); obj.transform.position = ne
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1