Play U3D scene conversion + level selection together

Scene Conversion

In games we often have the use of scene transformations, such as teleportation arrays
So how do you convert scenes in U3D?
Suppose we now have two scenarios:

So how do you switch between these two scenarios?
First, open the following interface

Then drag our scene into the open interface


Finally, we want to create a script in two scenarios
Note that namespaces are introduced first in scripts

using UnityEngine.SceneManagement;

public void OnClickLoadScene()
	{
		SceneManager.LoadScene("Complete");
	}

Then bind any button and you can jump to the scene you want to go to.

Stage Select

The end result is as follows:
Just entered level interface:

Click on the first level and make sure (you'll have to pick a few stars to count):
Here I select 2 stars and make sure: you can see that the first level has 2 stars and the second level has been unlocked

I won't show you how to build a UI, just post out the final UI level



The above UI s are explained below:
Canvas: it's the canvas
BackGround: Whole Background
windowBG: Background box (does not matter)
windowHead: The title above (does not matter)

Next Important

Levels: an empty object with a grid component added to it to arrange level buttons. In levels, I wrote 10 sub-objects, which are our levels
level01: button
Select: A picture, note that you only create this in this level, not behind it (or you just don't, don't affect the function, just to look comfortable)
Text: Used to display level number
stars: Star background box
star1-star3-star2: Picture of three stars
Lock: small lock picture
AcceptButton: Enter level scene button

Okay, let's start scripting

First, we add a script, CheckPoint Cotroller, to levels (select level controller)
The code is as follows:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LevelsController : MonoBehaviour {

	[Header("The most challenging level")]
	private int challengingLevel;

	/// <summary>
	///Selection Box
	/// </summary>
	private Transform select;

	private int currentLevelNum;

	void Awake()
	{
		//Find the selection box
		select = GameObject.FindWithTag("Select").transform;
	}

	void Start()
	{
		challengingLevel = Singleton.GetInstance().levelAndStars.Count + 1;

		InitUI();
	}


	public void InitUI()
	{
		//Initialize UI
		for (int i = 0; i < transform.childCount; i++)
		{
			//Find the ith child object (level)
			Transform child = transform.GetChild(i);

			Transform stars = child.Find("stars");

			//Change child level number
			child.Find("Text").GetComponent<Text>().text = (i+1).ToString();

			//Unlock if the current level number is less than the maximum challenging level number
			if (i < challengingLevel)
			{
				//Make the small lock picture inactive
				child.Find("lockImage").gameObject.SetActive(false);
				//Make level buttons clickable
				child.GetComponent<Button>().interactable = true;
				//Brighten level number text after unlocking
				child.Find("Text").GetComponent<Text>().color = new Color32(255, 234, 149,255);
				//The latest unlocked level unlocks the above content directly to continue
				if (i + 1 == challengingLevel)
				{
					continue;
				}
				
				//Activate Star Background Box
				stars.gameObject.SetActive(true);
				//Activate a specified number of stars based on the number of stars obtained at each level
				switch (Singleton.GetInstance().levelAndStars[i + 1])
				{
					case 1:
						stars.GetChild(0).gameObject.SetActive(true);
						break;

					case 2:
						stars.GetChild(0).gameObject.SetActive(true);
						stars.GetChild(2).gameObject.SetActive(true);
						break;

					case 3:
						for (int j = 0; j < 3; j++)
						{
							stars.GetChild(j).gameObject.SetActive(true);
						}
						break;

					default:
						break;
				}


			}
		}
	}

	//Respond to this method when you click on the level button (the level number passed in)
	public void CheckLevelByClick(int levelNum)
	{
		//Get the currently selected level
		Transform currentLevel = transform.GetChild(levelNum - 1);

		//Change the parent object of the selection box to the level currently selected
		select.SetParent(currentLevel);

		//Selection box to bottom
		select.SetSiblingIndex(0);

		//Selection box position changes to the currently selected level
		select.localPosition = Vector3.zero;

		//Save the currently selected level number (which can also be received directly with a singleton object)
		currentLevelNum = levelNum;
	}

	//Execute this method when the check button is clicked
	public void OnClickLoadScene()
	{
		//Save the current level number in a singleton
		Singleton.GetInstance().levelNum = currentLevelNum;
		//Jump Scene
		SceneManager.LoadScene("LevelComplete");
	}

}

Okay, let's write the singleton class

using System.Collections.Generic;

public class Singleton {

	//Current entry level
	public int levelNum;

	//Number of stars in the current level
	public int getStars;

	//Define a dictionary to store all card information
	public Dictionary<int, int> levelAndStars;
	//Define a singleton object
	private static Singleton instance;
	//Initialize dictionary in construction
	private Singleton()
	{
		levelAndStars = new Dictionary<int, int>();
	}
	//Get singletons
	public static Singleton GetInstance()
	{
		if (instance == null)
		{
			instance = new Singleton();
		}
		return instance;
	}
	//Update level information method (called in complete scenario)
	public void UpdateLevelStars()
	{
		if (levelAndStars.ContainsKey(levelNum))
		{
			if (levelAndStars[levelNum] < getStars)
			{
				levelAndStars[levelNum] = getStars;
			}
		}
		else
		{
			levelAndStars.Add(levelNum, getStars);
		}

		
	}

}

Next, the UI for another scenario is as follows


Readers can just look inside the red box without building those fancy things

We add a script, LevelComplete, to each star

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class LevelComplete : MonoBehaviour,IPointerEnterHandler{

	[Header("Which Star")]
	public int starIndex;
	//This script to get all the stars
	public LevelComplete[] stars;
	//Define a picture component to modify transparency
	private Image image;
	//Receive the return value of MouseX
	private float mhor;

	void Awake()
	{
		//Initialize image
		image = transform.GetComponent<Image>();
	}
	
	//When the mouse enters the current picture
	public void OnPointerEnter(PointerEventData eventData)
	{
		//Get Mouse Move Direction
		mhor = Input.GetAxis("Mouse X");
		//If greater than 0 means the mouse is scrolling left to right, we will light up the star
		if (mhor > 0)
		{
			ShowStars();
		}
		else
		{
			HideStars();
		}
	}
	//Decide how many stars to light depending on which one is lit
	public void ShowStars()
	{
		for (int i = 0; i < starIndex; i++)
		{
			stars[i].ChangeStarColorA(1);
		}
		//Number of stars saved with a single case
		Singleton.GetInstance().getStars = starIndex;

	}
	//Decide which stars to extinguish based on which one to extinguish
	public void HideStars()
	{
		for (int i = 2; i >= starIndex - 1; i--)
		{
			stars[i].ChangeStarColorA(0);
		}
		//Number of stars saved with a single case
		Singleton.GetInstance().getStars = starIndex - 1;

	}
	//Change the transparency of the star picture
	public void ChangeStarColorA(int A)
	{
		image.color = new Color(image.color.r, image.color.g, image.color.b, A);
	}


}

Finally, we add the following script to the button in the complete scene

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class CompleteAcceptButton : MonoBehaviour {
	//Define Button
	private Button btn;

	void Awake()
	{	//Initialization button
		btn = transform.GetComponent<Button>();
	}

	void Start()
	{	//Bind OnClickLoadScene event on button
		btn.onClick.AddListener(OnClickLoadScene);
	}

	//Click button response event
	private void OnClickLoadScene()
	{
		//Execute the singleton's update level information method
		Singleton.GetInstance().UpdateLevelStars();

		//Switch Scene
		SceneManager.LoadScene("Checkpoint");
	}


}

Okay, here's a simple introduction to the level's UI.

Just pay attention and give a compliment!

Thirty-seven original articles were published. 27 were praised. 10,000 visits+
Private letter follow

Tags: less

Posted on Fri, 13 Mar 2020 17:54:49 -0700 by jaronblake