This article shares the specific code of the unity mobile game virtual rocker for your reference. The specific content is as follows
using ; using ; using UnityEngine; using ; /// <summary> /// Rocker class bound to the rocker, reference radius 50/// </summary> public class Rocker : MonoBehaviour { Vector2 m_offet;//Offset vector Vector2 m_originalPos;//Original screen coordinates of the rocker Touch[] touches;//Array of touch points on the screen int touch_Id = -1;//Touch point array subscript bool isMove = false;//Whether to move float m_ScreenScale; /// <summary> /// Inform the rocker parameters to the offset vector called externally /// </summary> public Vector3 Offet { get { return m_offet; } } // Use this for initialization void Start () { m_originalPos = ;//The screen coordinate position of the center of the rocker m_ScreenScale = / 800f; } // Update is called once per frame void Update () { //Get the screen touch array touches = ; if ( > 0)//If the contact is turned on { //Get the contact index closest to the center of the rocker to touch_Id; if ( == 1)//When there is only one contact { touch_Id = 0; } else if ( > 1)//When the contact is greater than 1 { touch_Id = 0;//Suppose the subscript is 0 first for (int i = 1; i < ; i++)//Transfer the contact array { if ((touches[i].position - m_originalPos) < (touches[touch_Id].position - m_originalPos))//The i-th point is closer than the hypothetical point { touch_Id = i;//The assumption point is changed to the i-th point } } } //If the contacts obtained are not cancelled or lifted if ((touch_Id).phase != && (touch_Id).phase != ) { //The contacts are within the range of the rocker if((touches[touch_Id].position - m_originalPos) <= 50*50 * m_ScreenScale * m_ScreenScale)//50 is the background radius { isMove = true;//Open remote control //The joystick starts to control and calculates the offset SetOffetIn(); } else if(isMove)//The contact is outside the rocker range, but the remote control has been turned on { SetOffetOut(); } } else// Lift your fingers and return to the original position { = m_originalPos; m_offet = ; isMove = false; touch_Id = -1; } } } /// <summary> /// When the contact is in the operating disk /// How to control the rocker /// </summary> void SetOffetIn() { //The distance is too small and is considered to be unchanged without offsetting the position of the rocker. if((touches[touch_Id].position - m_originalPos) < 5 * m_ScreenScale) { GetComponent<Image>(). = m_originalPos;//The rocker is positioned in the original position m_offet = ; } else { //Rocator position tracking GetComponent<Image>(). = touches[touch_Id].position; m_offet = touches[touch_Id].position - m_originalPos;//Assign offset value m_offet = m_offet.normalized;//Normalization } } /// <summary> /// When the contact is outside the operating panel /// How to control the rocker /// </summary> void SetOffetOut() { Vector2 tempDir;//Temporary offset vector tempDir = touches[touch_Id].position - m_originalPos; //Update the rocker position: 127 units away from the original position GetComponent<Image>(). = m_originalPos + () * 25*m_ScreenScale; //Offset m_offet = ;//Normalization } private void OnGUI() { GUIStyle style = new GUIStyle(); //Instantiate a new GUIStyle with the name style, and use it later = 50; //The larger the font size setting value, the larger the font, the default color is black = new Color(1, 1, 1); //Set the text color to new color (0,0,0) Modify the value - represents different colors, the value is integer. I personally think it feels a bit like RGB (new Rect(20, 30, 300, 60), "Original location:" + m_originalPos.ToString(),style); (new Rect(20, 100, 300, 60), "Rocator Position:" + GetComponent<Image>().(), style); (new Rect(20, 170, 300, 60), "Contact Position:" + touches[touch_Id].(), style); (new Rect(20, 240, 300, 60), "Screen resolution:" + , style); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.