安徽科技学院
unity课程实习报告
所在专业地理信息系统
组长姓名杨明明(2206110124)
组内成员刘德旺(2206110114)
孟令康(2206110116)
任宏伟(2206110117)
赵文(2206110130)
所在院系城建与环境学院
指导教师邱银国
日期2014年11月5日
一、实验目的及要求
本次实验的总的目的是通过具体的程序的编写与unity 3d软件相结合,将所学的
知识内化,即将在课堂上学到的知识集成在一起,并实现相关功能,从而锻炼自己的程序编写、程序调试能力以及对unity 3d的掌握能力。本次实验的具体要求是使用Unity3d软件完成一个综合的应用系统。
二、实验方法
本次实验是以windows操作系统为平台,通过unity 3d软件进行本次实验的
项目实现,unity 3d是一个十分优秀的全面整合的专业游戏引擎。通过unity 3d
软件和编程工具的结合可以轻松创建各种3D模型,模拟模型动态以及插入互动
内容。
三、实验内容、步骤及结果
1.实验内容
本次实验是通过unity 3d软件完成一个综合的应用系统。并且本次实验所设
计的系统要实现的功能如下:
(1)漫游浏览(三维);
(2)连接数据库(SQL Server 2008);
(3)音视频播放;
(4)绘制简单图形;
(5)基本GUI元素显示;
2.实验步骤及结果
步骤:
(1)场景设计以及构思;
(2)项目实施及代码编写;
(3)场景代码相结合;
(4)进行调试。
结果:
1)基本GUI元素显示:
代码如下:
#pragma strict
var str:String;
var mm:String;
function Start () {
}
function Update () {
}
function OnGUI(){
GUI.BeginGroup(Rect(Screen.width/2-150,Screen.height/2-100,400,300));
https://www.wendangku.net/doc/3917721375.html,bel(Rect(20,20,80,30),"菜单:");
if(GUI.Button(Rect(100,40,60,20),"自动漫游")){
Application.LoadLevel("自动漫游");
}
if(GUI.Button(Rect(100,10,60,20),"音频播放")){ Application.LoadLevel("音频播放");
}
if(GUI.Button(Rect(100,70,60,20),"画线")){
Application.LoadLevel("画线");
}
if(GUI.Button(Rect(100,100,60,20),"视频播放")){ Application.LoadLevel("视频播放");
}
GUI.EndGroup();
}
2)绘制简单图形:
代码如下:
public class NewBehaviourScript : MonoBehaviour {
ArrayList pX = new ArrayList();
ArrayList pY = new ArrayList();
public Material mat;
//Point3d pts3d = new Point3d();
V ector3 mousePos = new Vector3();
enum DrawMode
{
Null, Lines, Triangle, Rectangle
};
DrawMode dm;
bool TriClose = false;
// Use this for initialization
void Start ()
{
dm = DrawMode.Null;
}
// Update is called once per frame
void Update ()
{
mousePos = Input.mousePosition;
if (Input.GetMouseButton(0))
{
pX.Add(Input.mousePosition.x);
pY.Add(Input.mousePosition.y);
}
}
void OnGUI()
{
//此处略。
}
3)音视频播放:
音频:
代码如下:
using UnityEngine;
using System.Collections;
public class music : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
if(GUI.Button(new Rect(Screen.width/2-100,Screen.height-30,50,20),"Start")){ gameObject.audio.Play();
}
if(GUI.Button(new Rect(Screen.width/2-40,Screen.height-30,50,20),"Pause")){ gameObject.audio.Pause();
}
if(GUI.Button(new Rect(Screen.width/2+20,Screen.height-30,50,20),"Stop")){ gameObject.audio.Stop();
}
if(GUI.Button(new Rect(Screen.width/2+80,Screen.height-30,50,20),"Return")){ Application.LoadLevel("loading");
}
}
}
视频:
代码如下:
using UnityEngine;
using System.Collections;
public class vidio : MonoBehaviour {
public MovieTexture movietexture;
public bool ON=false;
// Use this for initialization
void Start () {
renderer.material.mainTexture=null;
movietexture.loop=true;
gameObject.audio.Stop();
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
if(GUI.Button(new Rect(Screen.width/2-40,Screen.height-40,40,40),"开关")){ if(!ON){
movietexture.Play();
renderer.material.mainTexture=movietexture;
gameObject.audio.Play();
ON=true;
}else{
movietexture.Stop();
gameObject.audio.Stop();
ON=false;
renderer.material.mainTexture=null;
}
}
if(GUI.Button(new Rect(Screen.width/2,Screen.height-40,40,40),"停止")){ movietexture.Stop();
gameObject.audio.Stop();
}
if(GUI.Button(new Rect(Screen.width/2+40,Screen.height-40,40,40),"暂停")){ movietexture.Pause();
gameObject.audio.Pause();
}
if(GUI.Button(new Rect(Screen.width/2+80,Screen.height-40,40,40),"返回")){ Application.LoadLevel("loading");
}
}
}
4)漫游浏览(三维):
代码如下:
using UnityEngine;
using System.Collections;
public class working : MonoBehaviour
{
float hSliderV alue = 1f;
public GameObject[] path;//定义一个数组用来存储路径方块
public static int i = 0;//定义一个整型变量用来存储当前路径点序号// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
gameObject.transform.LookAt(path[i % path.Length].transform);
gameObject.transform.Translate(Vector3.forward * Time.deltaTime * hSliderValue);
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit))
{
print(https://www.wendangku.net/doc/3917721375.html,);
}
}
}
void OnGUI()
{
https://www.wendangku.net/doc/3917721375.html,bel.fontSize = 10;
GUI.color = Color.cyan;
//GUI.Button (new Rect(10,10,80,30),"djfkj");
hSliderValue = GUI.HorizontalSlider(new Rect(Screen.width - 200, 40, 100, 30), hSliderValue, 0.0f, 100.0f);
//GUI.contentColor = Color.black;
https://www.wendangku.net/doc/3917721375.html,bel(new Rect(Screen.width - 200, 15, 200, 30), "漫游速度:" + hSliderValue.ToString()+" m/s");
//空间坐标
//https://www.wendangku.net/doc/3917721375.html,bel(new Rect(10, Screen.height - 20, 100, 30), "X坐标:" + transform.position.x + " m");
//https://www.wendangku.net/doc/3917721375.html,bel(new Rect(120, Screen.height - 20, 100, 30), "Y坐标:" +
transform.position.y + " m");
//https://www.wendangku.net/doc/3917721375.html,bel(new Rect(230, Screen.height - 20, 100, 30), "Z坐标:" +
transform.position.z + " m");
if(GUILayout.Button("返回")){
Application.LoadLevel("loading");
}
}
}
四、实验结果以及心得体会
从普通用户的角度来说,3D Unity功能齐全,容易使用,3D模型导入比较容易,做3D项目比较容易。从开发人员的角度来说,使用3D Unity已有的功能,是很方便,但是一旦涉及到某些地方要定制化就很难做。由于它不能直接调用操作系统底层功能,因此,定制化模块的性能会比较差。3Dunity适合做房产演示、产品演示、小型网络游戏。但是不适合做大型网络游戏,和用户定制化要求比较高的项目。