Unity鼠標旋轉,彈出窗口,物體Mesh(網格)合并,添加BoxCollider,修改Rotation
最近剛接觸Unity,并且要實戰到項目當中,壓力是有點大。
先說一下項目需求:
1.在網頁上運行。
2.跟隨鼠標旋轉
3.鼠標點擊彈出該物體信息
乍一看其實挺簡單的,
首先unity提供了WebGL,第一條就算解決了。跟隨鼠標旋轉的其實也就是一段代碼,如下:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 6 public class MouseView : MonoBehaviour 7 { 8 9 public enum RotationAxes 10 { 11 MouseXAndY = 0, 12 MouseX = 1, 13 MouseY = 2 14 } 15 16 public RotationAxes m_axes = RotationAxes.MouseXAndY; 17 public float m_sensitivityX = 10f; 18 public float m_sensitivityY = 10f; 19 20 // 水平方向的 鏡頭轉向 21 public float m_minimumX = -360f; 22 public float m_maximumX = 360f; 23 // 垂直方向的 鏡頭轉向 (這里給個限度 最大仰角為25°) 24 public float m_minimumY = -25f; 25 public float m_maximumY = 25f; 26 27 float m_rotationY = 0f; 28 29 30 // Use this for initialization 31 void Start() 32 { 33 // 防止 剛體影響 鏡頭旋轉 34 if (GetComponent<Rigidbody>()) 35 { 36 GetComponent<Rigidbody>().freezeRotation = true; 37 } 38 } 39 40 // Update is called once per frame 41 void Update() 42 { 43 if (Input.GetMouseButton(0)) 44 { 45 if (m_axes == RotationAxes.MouseXAndY) 46 { 47 float m_rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * m_sensitivityX; 48 m_rotationY += Input.GetAxis("Mouse Y") * m_sensitivityY; 49 m_rotationY = Mathf.Clamp(m_rotationY, m_minimumY, m_maximumY); 50 51 transform.localEulerAngles = new Vector3(-m_rotationY, m_rotationX, 0); 52 } 53 else if (m_axes == RotationAxes.MouseX) 54 { 55 transform.Rotate(0, Input.GetAxis("Mouse X") * m_sensitivityX, 0); 56 } 57 else 58 { 59 m_rotationY += Input.GetAxis("Mouse Y") * m_sensitivityY; 60 m_rotationY = Mathf.Clamp(m_rotationY, m_minimumY, m_maximumY); 61 62 transform.localEulerAngles = new Vector3(-m_rotationY, transform.localEulerAngles.y, 0); 63 } 64 } 65 } 66 }
第三個點擊彈出信息,由于剛開始不了解里面的機制(現在也不是很懂)在這里跌了一個跟頭,首先點擊事件很簡單,但是要彈出窗口就突然找不到思路了,在網上找了一大圈得到以下解決方法
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class click001 : MonoBehaviour { 6 public bool WindowShow = false; 7 private Rect rect = new Rect(30, 40, 150, 150); 8 9 public Renderer rend; 10 11 public int fontSize; 12 public FontStyle fontStyle; 13 public RectOffset margin; 14 public RectOffset padding; 15 16 void Start() 17 { 18 //獲取renderer組件 19 rend = GetComponent<Renderer>(); 20 } 21 22 23 void OnGUI() 24 { 25 //窗口id 窗口大小 窗口回調(定義窗口內視圖) 窗口標題 26 if (WindowShow) 27 { 28 //項目需要,這是我自己添加的條件 29 if (gameObject.tag == "xxx") 30 { 31 GUI.Window(0, rect, onWindowOne, "xxxx"); 32 } 33 } 34 35 } 36 37 38 void onWindowOne(int winId) 39 { 40 //項目需要,這是我自己添加的條件 41 if (gameObject.tag == "xxxx") 42 { 43 GUI.Label(new Rect(10, 10, 140, 40), "當前窗口是xxxxx"); 44 } 45 46 //GUI.Label(new Rect(10, 10, 140, 40), "當前窗口id是" + winId); 47 if (GUI.Button(new Rect(10, 30, 80, 30), "按鈕1")) 48 { 49 Debug.Log("當前窗口id" + winId); 50 } 51 //定義窗體可以活動的范圍 52 //GUI.DragWindow(new Rect(0, 0, 10000, 10000)); 53 } 54 55 void OnMouseDown() 56 { 57 if (WindowShow) 58 { 59 WindowShow = false; 60 } 61 else 62 { 63 WindowShow = true; 64 } 65 66 } 67 }
到這里一切搞定,可惜高興的不是時候,當我在頁面打開的時候,發現中文字體全部消失了,原來unity默認的字體是Arial,我們只需要替換成支持中文的字體即可,在腳本里加上以下代碼
1 public Font font;
當然,需要在用到字體的位置調用改變量,比如我在這兩個位置有用到
1 GUI.skin.window.font = font;//GUI的彈出窗口
2 GUI.skin.label.font = font;//這個也是GUI彈出窗口里面的內容,但是不這樣用,就沒有效果,不知道為什么
然后這些總算是解決了,不過新的問題又出現了,就是點擊事件需要Collider支持,那就添加吧,再添加的過程中我發現這樣實在是耗時費力,而且還不精確,然后我就請教了大神,大神一語道破其中的問題所在!
原來我是把我需要組成一組的元素都放在了一個空Game Object里,如果給它添加BoxCollider,會很麻煩,可以說根本就不適合添加,因為它沒有Mesh,也就是網格,所以就需要添加網格,既然它是組合成的一個組,
所以需要動態的添加網格,當然也需要動態添加BoxCollider,然后當運行以后,我發現物體的位置發生了變化,好吧,那就再控制一下位置,保持和Scene里看到的一致即可,下面是代碼
1 using UnityEngine; 2 using System.Collections; 3 4 public class Combine_Test : MonoBehaviour 5 { 6 7 // Use this for initialization 8 void Start() 9 { 10 //---------------- 先獲取材質 ------------------------- 11 //獲取自身和所有子物體中所有MeshRenderer組件 12 MeshRenderer[] meshRenderers = GetComponentsInChildren<MeshRenderer>(); 13 //新建材質球數組 14 Material[] mats = new Material[meshRenderers.Length]; 15 for (int i = 0; i < meshRenderers.Length; i++) 16 { 17 //生成材質球數組 18 mats[i] = meshRenderers[i].sharedMaterial; 19 } 20 //---------------- 合并 Mesh ------------------------- 21 //獲取自身和所有子物體中所有MeshFilter組件 22 MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>(); 23 CombineInstance[] combine = new CombineInstance[meshFilters.Length]; 24 for (int i = 0; i < meshFilters.Length; i++) 25 { 26 combine[i].mesh = meshFilters[i].sharedMesh; 27 //矩陣(Matrix)自身空間坐標的點轉換成世界空間坐標的點 28 combine[i].transform = meshFilters[i].transform.localToWorldMatrix; 29 meshFilters[i].gameObject.SetActive(false); 30 } 31 //為新的整體新建一個mesh 32 transform.GetComponent<MeshFilter>().mesh = new Mesh(); 33 //合并Mesh. 第二個false參數, 表示并不合并為一個網格, 而是一個子網格列表 34 transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine, false); 35 transform.gameObject.SetActive(true); 36 37 //為合并后的新Mesh指定材質 ------------------------------ 38 transform.GetComponent<MeshRenderer>().sharedMaterials = mats; 39 //為合并后的新的整體添加BoxCollider ------------------------------ 40 BoxCollider bc = gameObject.AddComponent<BoxCollider>(); 41 //為合并后的新的整體修改rotation ------------------------------ 42 transform.localEulerAngles = new Vector3(0, 0, 0); 43 } 44 45 // Update is called once per frame 46 void Update() 47 { 48 49 } 50 }
好的到這里問題基本上都解決了,不過還有一個彈出窗口的問題沒有解決,就是沒有實現當我點擊其他物體或空白處時,窗口消失,這確實是一個不大不小的問題,以后會進一步完善。
希望路過的大佬能留下您的建議,也希望這篇博文能幫助那些像我一樣在網上瘋狂找資料的小伙伴
補充,上面那樣添加boxcollider非常不完美,然后我換成了 meshcollider,經測試也不好,因為這兩種方法都不能貼合表層添加collider
解決方法:在拉入的模型中勾選 generate Collider,然后動態添加 meshcollider 就可以了,無需勾選conves

浙公網安備 33010602011771號