[Unity3d]虚拟3D汽车展示项目

2020-01-14 15:12:24 浏览数 (2)

今天完善成了虚拟3D汽车展示项目的部分功能,虽然用的汽车模型有点粗糙,但感觉还不错,下面我就贴下源码供初学者学习!

项目展示地址:http://114.92.242.208/aspnet_client/system_web/carshow/dxw2.html,说明:浏览器必须要安装unityplayer插件。

项目操作说明:

1.鼠标左击可以点击左右车门,控制它的开关,还可以点击前后盖控制打开和关闭。

2.鼠标滚轮可以缩放,达到一个远近的效果。

3.按住键盘s键可以显示menu,h键可以隐藏menu菜单。

源码:

代码语言:javascript复制
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* using UnityEngine; using System.Collections;     public class NewBehaviourScript : MonoBehaviour {      public GameObject cube;     public GameObject car;     public GameObject light;     Vector2 p1, p2;//用来记录鼠标的位置,以便计算旋转幅度     Vector3 originalPosition;     float target = 0;     public Texture2D redbtnTexture;      private bool bOpenMenu = false; //是否打开功能框     private Rect myWindow = new Rect(10, 10, 470, 300); //功能选项框架     public GUISkin customSkin;//自定义皮肤     private Rect closeButton = new Rect(415, 0, 26, 22);//关闭按钮     public Texture2D tHero;     private Rect rHero = new Rect(0, 40, 180, 230); //人物的位置      //图像框     private Rect stateBox = new Rect(157,50,280,210);      private string[] functionStr ; //功能选项数组      public Texture2D redBtn;     public Texture2D greenBtn;     public Texture2D blueBtn;     public Texture2D orangeBtn;     public Texture2D whiteBtn;     public Texture2D blackBtn;     private Texture2D[] iBtn;     //iBtn= new Texture2D[6]{redBtn,greenBtn,blueBtn,orangeBtn,whiteBtn,blackBtn};      private Rect[] pBtn = new Rect[6];      //车轮更换     private bool selectToggleWheel = false;     //雨刷器     private bool selectToogleWiper = false;     //转向灯     private bool selectToogleLeftTurningLight = false;     private bool selectToogleRightTurningLight = false;     //前照灯     private bool selectToogleFarLight = false;     private bool selectToogleNearLight = false;        private GUIContent[] guiCon = new GUIContent[4]; //接受字符串     public Rect[] strPosition = new Rect[4];     // 鼠标中间键     int MouseWheelSensitivity = 5;     int MouseZoomMin = 18;     int MouseZoomMax = 90;     float normalDistance = 32;     public GameObject doorL;      // Use this for initialization     void Start()     {         iBtn= new Texture2D[6]{redBtn,greenBtn,blueBtn,orangeBtn,whiteBtn,blackBtn};         originalPosition = transform.position;         doorL = GameObject.Find("doorL");         cube = GameObject.Find("Main Camera");          for (int i=0;i<6;i  )         {             pBtn[i] =  new Rect(170 i*43, 65,40,30);         }         functionStr= new string[4]{"车轮更换","雨刷器","转向灯","前照灯"};          for (int i = 0; i < 4; i  )         {             guiCon[i] = new GUIContent(functionStr[i]);         }         strPosition[0] = new Rect();     }      // Update is called once per frame     void Update()     {         //如果为s键则打开弹出框         if (Input.GetKey(KeyCode.S))         {             bOpenMenu = true;         }         else if (Input.GetKey(KeyCode.H))         {             bOpenMenu = false;         }          //开门         //if (target < 20)         //{         //    doorL.transform.Rotate(transform.forward, 2);         //    target = target   1;         //}                  //旋转         if (Input.GetMouseButtonDown(0))         {             p1 = new Vector2(Input.mousePosition.x, Input.mousePosition.y);//鼠标右键按下时记录鼠标位置p1         }         if (Input.GetMouseButton(0))         {             p2 = new Vector2(Input.mousePosition.x, Input.mousePosition.y);//鼠标右键拖动时记录鼠标位置p2             //下面开始旋转,仅在水平方向上进行旋转             float dx = p2.x - p1.x;             float dy = (float)0.1*(p2.y - p1.y);             transform.RotateAround(car.transform.position, Vector3.up, dx * Time.deltaTime);             //鼠标上下移动             //transform.Translate(dy*Vector3.up * Time.deltaTime);             light.transform.RotateAround(car.transform.position, Vector3.up, dx*Time.deltaTime);         }           //鼠标滚轮控制场景大小         // 如果按住滑轮         if (Input.GetAxis("Mouse ScrollWheel") > 0)         {             Debug.Log(1);             Debug.Log(Input.GetAxis("Mouse ScrollWheel"));                         if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)             {                 normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;             }              if (normalDistance < MouseZoomMin)             {                 normalDistance = MouseZoomMin;             }             if (normalDistance > MouseZoomMax)             {                 normalDistance = MouseZoomMax;             }            // transform.Translate(transform.forward * normalDistance);             transform.camera.fieldOfView = normalDistance;                       }          //后滚         else if (Input.GetAxis("Mouse ScrollWheel") < 0)         {             Debug.Log(-1);             if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)             {                 normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;             }              if (normalDistance < MouseZoomMin)             {                 normalDistance = MouseZoomMin;             }             if (normalDistance > MouseZoomMax)             {                 normalDistance = MouseZoomMax;             }            // transform.Translate(-transform.forward * normalDistance);             transform.camera.fieldOfView = normalDistance;         }                     //按返回键退出程序             //if (Input.GetKey(KeyCode.Escape))         //{         //    print(1);         //    Application.Quit();         //}   		     }      void OnGUI()     {           GUI.skin = customSkin;//赋值个性化皮肤          //弹出功能选项框         if (bOpenMenu)         {             myWindow = GUI.Window(0, myWindow, DoMyWindow, "");             //限制移动的横纵向坐标             //myWindow.x = Mathf.Clamp((float)myWindow.x,(float)0.0,(float)Screen.width-myWindow.width);             //myWindow.y = Mathf.Clamp((float)myWindow.y, (float)0.0, (float)Screen.height - myWindow.height);         } 		 			string s = "作者:丁小未";             GUIStyle bb = new GUIStyle();             bb.normal.background = null;//设置背景             bb.normal.textColor = new Color(1,0,0);//设置颜色             bb.fontSize = 40;             GUI.Label(new Rect(40, 20, 100, 50), s, bb); 		     }       private bool flagLight1 = false; //方向灯     private bool flagLight2 = false; //远近灯          void DoMyWindow(int windowsId)     {         GUI.BeginGroup(new Rect(10, 10, 500, 400));          GUI.Box(stateBox, ""); // // //        //点击关闭按钮         if (GUI.Button(closeButton,"",GUI.skin.GetStyle("ExitButton")))         {             bOpenMenu = false;         }        // GUI.DragWindow();         GUI.DrawTexture(rHero, tHero);          for (int i = 0; i < 6;i   )         {             GUI.DrawTexture(pBtn[i], iBtn[i]);         }          //写文字         //GUI.Label(new Rect(170, 100, functionStr[0].Length, 20),guiCon[0],"TextItem");         for (int i = 0; i < 4; i  )         {             GUI.Label(new Rect(174,108 i*38,functionStr[i].Length,20),guiCon[i],"TextItem");         }          //车轮更换         //绿色开状态         if (selectToggleWheel)         {             //加选择框             selectToggleWheel = GUI.Toggle(new Rect(255, 110, 15, 15), selectToggleWheel, "", "TrueCheckItem");          }         //红色关闭状态         else         {             selectToggleWheel = GUI.Toggle(new Rect(255, 110, 15, 15), selectToggleWheel, "", "FalseCheckItem");          }         //雨刷器         if (selectToogleWiper)         {             //加选择框             selectToogleWiper = GUI.Toggle(new Rect(255, 148, 15, 15), selectToogleWiper, "", "TrueCheckItem");          }         //红色关闭状态         else         {             selectToogleWiper = GUI.Toggle(new Rect(255, 148, 15, 15), selectToogleWiper, "", "FalseCheckItem");          }          //转向灯         //左转         if (selectToogleLeftTurningLight)         {             if (flagLight1&&selectToogleRightTurningLight) //右边打开             {                 selectToogleRightTurningLight = false; //关闭右边             }             //加选择框             selectToogleLeftTurningLight = GUI.Toggle(new Rect(255, 186, 15, 15), selectToogleLeftTurningLight, "", "TrueCheckItem");             flagLight1 = true;         }         //红色关闭状态         else         {             selectToogleLeftTurningLight = GUI.Toggle(new Rect(255, 186, 15, 15), selectToogleLeftTurningLight, "", "FalseCheckItem");             flagLight1 = false;         }         //右转         if (selectToogleRightTurningLight)         {             if (flagLight1 && selectToogleLeftTurningLight) //左边打开             {                 selectToogleLeftTurningLight = false; //关闭右边             }             //加选择框             selectToogleRightTurningLight = GUI.Toggle(new Rect(285, 186, 15, 15), selectToogleRightTurningLight, "", "TrueCheckItem");             flagLight1 = true;         }         //红色关闭状态         else         {             selectToogleRightTurningLight = GUI.Toggle(new Rect(285, 186, 15, 15), selectToogleRightTurningLight, "", "FalseCheckItem");             flagLight1 = false;         }          //前照灯         //近         if (selectToogleNearLight)         {             if (flagLight2 && selectToogleFarLight) //远灯打开             {                 selectToogleFarLight = false; //关闭远灯             }             //加选择框             selectToogleNearLight = GUI.Toggle(new Rect(255, 224, 15, 15), selectToogleNearLight, "", "TrueCheckItem");             flagLight2 = true;         }         //红色关闭状态         else         {             selectToogleNearLight = GUI.Toggle(new Rect(255, 224, 15, 15), selectToogleNearLight, "", "FalseCheckItem");             flagLight2 = false;         }         //远         if (selectToogleFarLight)         {             if (flagLight2 && selectToogleNearLight) //近灯打开             {                 selectToogleNearLight = false;             }             //加选择框             selectToogleFarLight = GUI.Toggle(new Rect(285, 224, 15, 15), selectToogleFarLight, "", "TrueCheckItem");             flagLight2 = true;         }         //红色关闭状态         else         {             selectToogleFarLight = GUI.Toggle(new Rect(285, 224, 15, 15), selectToogleFarLight, "", "FalseCheckItem");             flagLight2 = false;         }          GUI.EndGroup();     }  }
*/

门盖控制:

代码语言:javascript复制
private bool isOpenDoor = false; private bool openDoor = false; private bool closeDoor = false;  private int target = 0;  int flag = 0;  Ray ray; RaycastHit hitobj; private GameObject doorl; //左门  // Use this for initialization 	void Start () {         doorl = GameObject.Find("doorL");//左门 		 	} 	 	// Update is called once per frame 	void Update () {         //画出射线         ray = Camera.main.ScreenPointToRay(Input.mousePosition); 	    if (Input.GetMouseButtonDown(0)) 	    {             //鼠标点击车门             if (Physics.Raycast(ray,out hitobj,1000))             {                 print("ddd");                 Debug.DrawLine(ray.origin, hitobj.point); 				//左车门                 if (hitobj.collider.name == "doorL")                 {                     print("111");                     if (flag == 0)                     {                         openDoor = true;                         closeDoor = false;                     }                     else                     {                         closeDoor = true;                         openDoor = false;                     }                      flag  ;                     flag %= 2;                 } } 		//开左门 	    if (openDoor) 	    {             //开门             if (target < 40 && !isOpenDoor)             {                 doorl.transform.Rotate(Vector3.forward, 1);                 target = target   1;             }             else             {                 isOpenDoor = true;             } 	    } 	    if(closeDoor)         {             //关门             if (isOpenDoor && target > 0)             {                 doorl.transform.Rotate(-Vector3.forward, 1);                 target -= 1;             }             else             {                 isOpenDoor = false;             }         } }

效果图:

0 人点赞