Unity之一天一個技術點(十二)---指南針的實現
阿新 • • 發佈:2019-02-17
指南針的實現(可據鏡頭旋轉改變)
程式碼如下:
變數簡述:
compassGUISkin面板用來顯示指南針貼圖
標籤Label貼圖用來作為指南針背景 Box貼圖用來作為箭頭貼圖
程式碼如下:
變數簡述:
compassGUISkin面板用來顯示指南針貼圖
標籤Label貼圖用來作為指南針背景 Box貼圖用來作為箭頭貼圖
把指令碼賦予給主鏡頭即可
GUI.skin = compassGUISkin; var compassAngle : float = transform.rotation.eulerAngles.y;//得到鏡頭的y軸旋轉角度 var compassDiameter : float = 128; var compassWidth : float = 16; var compassWindowRect : Rect = Rect (compassPos.x,compassPos.y, compassDiameter, compassDiameter); GUI.Label (compassWindowRect, ""); var cosAngle : float = Mathf.Cos(compassAngle*Mathf.PI/180); var sinAngle : float = Mathf.Sin(compassAngle*Mathf.PI/180); var xMove : float = (compassDiameter*sinAngle/2.0 + compassWidth*cosAngle/2.0); var yMove : float = (-compassDiameter*cosAngle/2.0 + compassWidth*sinAngle/2.0); var compassPos : Vector3 = new Vector3(compassPos.x + compassDiameter/2.0 + xMove,compassPos.y + compassDiameter/2.0 + yMove, 0); //position for matrix var compassQuat : Quaternion = Quaternion.identity; //rotation for matrix compassQuat.eulerAngles = Vector3(0, 0, compassAngle + 90); //set the rotation to something - rotate around z! GUI.matrix = Matrix4x4.TRS(compassPos, compassQuat, Vector3.one); //Apply the matrix GUI.Box(Rect(0, 0, compassDiameter, compassWidth), ""); //notice how the rect starts at 0/0 and the matrix handles the position