In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to implement Mini Game in Unity using all the available space in FlyPin. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. Brief introduction
Unity game example development collection, using a simple and easy-to-understand way to explain the development and implementation process of common games, to facilitate later reference and reuse of similar game development.
This section introduces the rapid realization of FlyPin (use all available space) leisure Mini Game, I hope it can help you, if there is anything wrong, please leave a message.
II. FlyPin (make use of all available space) game content and operation
1. When the game begins, the needle Pin is automatically ready.
2. Click the left mouse button to launch Pin, fly to the target, and automatically prepare the next needle Pin and increase the score.
3. After the needle Pin is inserted into the target, it will rotate with it.
4. When two needles Pin collide, the game ends
5. After the animation is completed, restart the game automatically.
Third, the game code framework
4. Knowledge points
1. MonoBehaviour lifecycle function: Awake,Start,Update,OnGUI
2. Monitoring of Input button
3. Generation of GameObject.Instantiate objects and destruction of GameObject.Destroy objects
4. Camera.orthographicSize orthographic camera viewport size modification, Camera.backgroundColor camera SolidColor background color modification
5. Rigidbody2D cancels gravity effect, adds Collider, and performs Trigger collision detection.
6. GUIStyle GUI style, the function of adding text to GUI.Label is used.
7. Use of Vector3.Lerp displacement vector interpolation and Vector3.Distance position distance function.
8. The use of Mathf.Lerp, Color.Lerp numerical value and color interpolation calculation
9. Use of Transform.Rotate rotation
10. IEnumerator collaboration, StartCoroutine starts collaboration and StopAllCoroutines stops the use of all collaboration
11. SimpleMessageCenter simple message center use
12. The use of delegation in the change of Action OnChangeValue attribute
13. The use of Resources.Load () code loading preform
14. The use of OnTriggerEnter2D 2D collision detection
15. SceneManager.LoadScene loading, and SceneManager.GetActiveScene () acquisition of the current scene
16, etc.
5. Preview of the game effect
VI. Implementation steps
This is a 2D game, which mainly uses resource components such as SpriteRenderer, 2D Collider,2D Rigidbody, and TextMesh. All resources are included in Unity, and no other external map model resources are imported.
1. Open Unity and create a project
2. Build the scene, add the place where the Pin is initially generated, prepared and flown, as well as the target circle that rotates, and the 3DText that shows the score
3. ScoreText, which is created as 3D Object-3D Text, which is controlled by Character Size and Font Size according to the font size of TextMesh.
4. If the TextMesh looks blurry, you can change the Character Size to smaller and enlarge the Font Size, as shown in the figure
5. TargetCircle is SpriteRenderer. Set the color as needed. Sprite is the Knob that comes with Unity.
It shows that TargetCircle is also the target for Pin to fly to.
6. PinSpawnPos and PinReadyPos are the initial positions and prepared positions generated by the needle Pin. The Cube under PinSpawnPos and PinReadyPos is easy to observe and set. After determining the position, you can delete or hide the Cube.
7. Note that ScoreText, TargetCircle, PinSpawnPos and PinReadyPos,Position.z are all 0, in order to meet the requirements of 2D games on the same plane.
8. Main Camera, set as shown, ClearFlags is Solid Color, Background color is set as needed, Projection is Orthographic orthogonal (2D game setting camera), Size is 5, you can set it according to your needs
9. Finally, the effect is as shown in the picture
10. Pin preform, including PinHead and PinBody
11. PinHead of Pin, Knob of Sprite of SpriteRenderer, color is set according to your own needs, add CircleCollider2D collision body, and check IsTrigger (avoid collision, produce collision effect, if unchecked, there will be physical effect of collision), the size of CircleCollider2D can be adjusted according to the actual situation (generally, Unity will automatically set the default size according to Renderer size) Add Rigidbody2D (one of the necessary conditions for collision), and BodyType is set to Kinematic to be free from gravity drop (or BodyType is Dynamic, Gravity Scale is set to 0, or gravity is not affected)
12. The Background that comes with Sprite in the PinBody,SpriteRenderer in Pin. The color is optional. Order in Layer defaults to 0 and is set to-1, which is for needle insertion TargetCircle effect, and Scale is set to (1mem15 and 1) for the effect that is as slender as a needle.
13. As for Pin, the Prefabs folder under the Resources folder is used to load the preform using the Resources.Load code in Unity. There is no need to manually mount the preform, while Resources.Load loading requires that the preform be placed under the Resources folder.
14. PinHead monitors whether two needles collide in a close position, and sends a game end message if they collide.
15. OnTriggerEnter2D (Collider2D collision) in PinHead listens to the collision entry, and the collision sends a game accommodation message.
/ monitor whether two pins collide in close position / send game end message / private void OnTriggerEnter2D (Collider2D collision) {/ / whether two PinHead collide if (collision.name.Equals (ConstStr.PIN_HEAD_NAME)) {/ / if you collide, send the game end message SimpleMessageCenter.Instance.SendMsg (MsgType.GameOver) }}
16. Pin class to manage the state and movement of needle Pin
17. Pin class, Init initialization function, get the relevant location and mount the PinHead script to the PinHead object of Pin
Parameter description:
/ / Pin ready position private Vector3 masking PinReadyPos; / / Pin flying target position private Vector3 masking PinFlyTargetPos; / / the distance between Pin inserting the target position private float masking PinFlyTargetPosdistance; / / Pin moving speed private float masking PinMoveSpeed; / / Pin flying to the target position Transfrorm private Transform m_PinTargetParentTrans / / PinHead private PinHead m_PinHead / / initialize function Get the relevant location and PinHead / public void Init (PinsManager pinsManager, Vector3 pinReadyPos, Vector3 flyTargetPos, float flyTargetPosDistance, float pinMoveSpeed) Transform pinTargetParentTrans) {m_PinsManager = pinsManager M_PinReadyPos = pinReadyPos; m_PinFlyTargetPos = flyTargetPos; m_PinFlyTargetPosDistance = flyTargetPosDistance; m_PinMoveSpeed = pinMoveSpeed; m_PinTargetParentTrans = pinTargetParentTrans / / add PinHead script m_PinHead = transform.Find (ConstStr.PIN_HEAD_NAME) .gameObject.AddComponent () to PinHead sub-object; / / set state to ready state m_CurPinState = PinState.Readying;}
18. Status monitoring of Pin class, UpdatePinState (), and Pin
/ status monitoring / void UpdatePinState () {switch (CurPinState) {case PinState.Idle: break; case PinState.Readying: Readying () Break; case PinState.ReadyOK: m_PinsManager.CurPin = this; break Case PinState.Fly: Fly (); m_PinsManager.CurPin = null; break; default: break;}}
19. Pin class, the state function that Readying () is preparing, Fly () the position state of the flying target, including the real motion of the Pin (Vector3.Lerp), and the state switch corresponding to the condition (Vector3.Distance) (Note: putting the Pin entity into the TargetCircle can make the Pin rotate with the TargetCircle (this.transform.SetParent (m_PinTargetParentTrans);)
/ the status function being prepared / void Readying () {/ / move to the ready location transform.position = Vector3.Lerp (transform.position, m_PinReadyPos, Time.deltaTime * m_PinMoveSpeed) / / determine whether to reach the prepared position if (Vector3.Distance (transform.position, m_PinReadyPos) {ScoreText.text = score.ToString ();};})
34. GameManager game management class, Update () Unity has its own function, the game is not over, TargetCircle rotates every frame, and monitors the left mouse button press, Fly Pin and generate the next Pin ready
Private void Update () {/ / Game end if (m_IsGameOver = = true) {return;} / / TargetCircle Update rotation m_TargetCircleManager.UpdateRotateSelf () / / if you press the left mouse button and press if (Input.GetMouseButtonDown (0)) {/ / Pin to fly to the target, the score will be increased and the next Pin bool isFly= m_PinsManager.FlyPin () will be generated; if (isFly==true) {masked ScoreManager. Scorekeeper + M_PinsManager.SpawnPin (); / / generate the next}
35. GameManager game management class, OnDestroy () does some data cleaning and emptying when the game is reloaded and destroyed, and stops all possible cooperative programs (Mini Program, which does not hinder the running of the main program). OnGUI () does some game operation instructions.
Private void OnDestroy () {/ / destroy all Pin m_PinsManager.DestroyAllPins (); / / clear message center SimpleMessageCenter.Instance.ClearAllMsg (); / / empty score update event m_ScoreManager.OnChangeValue = null; / / empty related parameter m_PinsManager = null M_TargetCircleManager = null; m_ScoreManager = null; / / stop all cooperative programs StopAllCoroutines ();} / / Unity periodic function calls private void OnGUI () {/ / game operation instructions GUIStyle fontStyle = new GUIStyle (); fontStyle.normal.background = null / / set background fill fontStyle.normal.textColor = new Color (1,0,0); / / set font color fontStyle.fontSize = 40; / / Font size GUI.Label (new Rect (10,10,200,200), "instructions:\ N1, click the left mouse button to launch the sphere. \ n 2. The collision of two Pin stitches will automatically trigger the restart of the game; ", fontStyle);}
36. GameManager game management class, ToGameOver () game end event, stop rotation, start cooperation, perform special effects at the end of the game, and then automatically reload the current scene and restart the game
/ Game over / void ToGameOver () {/ / Game end if (m_IsGameOver==true) {return;} / / Game end game IsGameOverOver= true / / stop target rotation m_TargetCircleManager.StopRotateSelf (); / / start and end StartCoroutine (GameOver ()) } / IEnumerator GameOver () {while (true) {/ / wait for the last yield return new WaitForEndOfFrame of the frame () / / Update the main Camera viewport m_MainCamera.orthographicSize = Mathf.Lerp (m_MainCamera.orthographicSize, massively OrthographicSizeTime time.deltaTime * 10); / / Update the main Camera background color m_MainCamera.backgroundColor = Color.Lerp (m_MainCamera.backgroundColor, Color.red,Time.deltaTime * 5) / / Update the main Camera viewport in place and jump out of the loop if ((m_MainCamera.orthographicSize-m_OrthographicSize))
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.