“This is the 14th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
📢 preface
I briefly wrote two articles on how to switch skyboxes between Unity and code
Ordinary switch sky box or very simple, that can not meet my ideas!
Then this article has written a simple example to do a slightly fancier skybox switch example!
🍉 Skybox click on the rotation to switch
So let’s look at one of the things that’s implemented, just in case I don’t see what I meanBecause I wanted to have a skybox that could be switched at any time in the scene, I came up with this simple solution
Switch skyboxes by displaying a few balls with their own skybox material in the scene and clicking on them.
The following production steps, for the novice will show each step, very simple to learn!
🏳️🌈 Step 1: Simply build a scenario
Let’s build a scene in the scene to make our example more fun
I’m going to randomly place a plane and four little ballsAnd then find oursSkybox material ball
和 Picture of sky box
Drag it directly onto the ball!
So the ball has color
🏳️🌈 Step 2: click the ball through the code to switch the sky box
The code for this step is also very simple. We talked about how to switch skyboxes in code in the previous article
So here directly on the code, the following code is to click the ball to switch the sky box code, the previous article also said!
public class skyDemo1 : MonoBehaviour.IPointerDownHandler
{
public Material skyMaterial;// Skybox material
// Click the ball's callback
public void OnPointerDown(PointerEventData eventData)
{
// Toggle skyboxesRenderSettings.skybox = skyMaterial; }}Copy the code
Then we hung the script on each ball and added our own skybox material to it
As follows:
At this time we can click on each ball to switch the sky box!
🏳️🌈 Step 3: Spin the ball
This step is easy, but there are many ways to implement rotation in Unity, which I won’t cover here
Transform.RotateAround(Vector3,Vector3,float);
- The first parameter: the center point of rotation
- Second argument: some axis at the center point
- The third parameter: the speed of rotation
Add the code for the ball rotation in script skyDemo1, using the API method for the rotation described above
The complete code is as follows:
using UnityEngine;
using UnityEngine.EventSystems;
public class skyDemo1 : MonoBehaviour.IPointerDownHandler
{
public GameObject skyCenter;
public Material skyMaterial;// Skybox material
public void OnPointerDown(PointerEventData eventData)
{
// Toggle skyboxes
RenderSettings.skybox = skyMaterial;
}
void Update()
{
Revolution();
}
public void Revolution()
{
// Keep rotating at one speed around some axis at some central point
transform.RotateAround(skyCenter.transform.position, skyCenter.transform.up, Time.deltaTime * 50); }}Copy the code
Then mount the script into the scene!
The implementation effect is as follows:
💬 summary
A simple switch sky box effect is achieved ~
This gives it a slightly more advanced feel, of course this is only the simplest switching effect, but it is much better than the original effect
Isn’t it easy? If you have a better idea, you can give me some suggestions
I also want some better ideas!!
If you don’t have the right sky box resources, you can download the sky box resources shown in my article to try!
Skybox download link