Code Summary Examples: Ask AI to document your code
//IntroSceneManager.cs
----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Video;
public class IntroSceneManager : MonoBehaviour
{
public VideoPlayer introVideoPlayer;
public VideoClip[] videoClips; // Add this line to store your video clips
public string mainMenuSceneName;
private bool videoFinished = false;
private void Start()
{
int randomIndex = Random.Range(0, videoClips.Length); // Select a random index
introVideoPlayer.clip = videoClips[randomIndex]; // Set the video clip to the randomly chosen one
introVideoPlayer.loopPointReached += OnIntroVideoFinished;
introVideoPlayer.prepareCompleted += OnIntroVideoPrepared;
introVideoPlayer.Prepare();
}
private void Update()
{
if (!videoFinished && Input.GetKeyDown(KeyCode.S))
{
SkipIntroVideo();
}
}
private void OnIntroVideoPrepared(VideoPlayer vp)
{
introVideoPlayer.Play();
}
private void OnIntroVideoFinished(VideoPlayer vp)
{
videoFinished = true;
StartCoroutine(LoadMainMenuScene());
}
private IEnumerator LoadMainMenuScene()
{
yield return new WaitForSeconds(1f);
SceneManager.LoadScene(mainMenuSceneName);
}
private void SkipIntroVideo()
{
introVideoPlayer.Stop();
videoFinished = true;
StartCoroutine(LoadMainMenuScene());
}
}
This is a C# script for a Unity game that manages the intro scene. It plays a video clip randomly chosen from an array of video clips, and then loads the main menu scene when the video finishes. If the player presses the "S" key, they can skip the video and go straight to the main menu.
Here is a breakdown of the script:
public VideoPlayer introVideoPlayer; - a public VideoPlayer variable for the VideoPlayer component that will play the intro video.
public VideoClip[] videoClips; - a public array of VideoClip variables to store multiple video clips that can be played randomly.
public string mainMenuSceneName; - a public string variable for the name of the main menu scene to load after the video finishes.
private bool videoFinished = false; - a private boolean variable to keep track of whether the video has finished playing or not.
void Start() - a method that runs when the script starts. It selects a random video clip from the array and sets it as the intro video. It also adds event listeners for when the video is finished playing and when it's prepared to play.
void Update() - a method that runs every frame. It checks if the "S" key is pressed and if the video hasn't finished playing yet. If both conditions are met, it calls the SkipIntroVideo() method.
void OnIntroVideoPrepared(VideoPlayer vp) - a method that runs when the intro video is prepared to play. It simply starts playing the video.
void OnIntroVideoFinished(VideoPlayer vp) - a method that runs when the intro video finishes playing. It sets the videoFinished boolean to true and calls the LoadMainMenuScene() coroutine.
IEnumerator LoadMainMenuScene() - a coroutine that waits for 1 second before loading the main menu scene. This delay gives the player a chance to see the end of the video before moving on.
void SkipIntroVideo() - a method that stops the intro video, sets the videoFinished boolean to true, and calls the LoadMainMenuScene() coroutine. This method is called when the player presses the "S" key.
----------------------------------------------------------------------------------------------------------------------------------------------------------
//HideCursor.cs
----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
public class HideCursor : MonoBehaviour
{
void Start()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
}
This is a C# script for a Unity game that hides the mouse cursor and locks it to the center of the screen. Here is a breakdown of the script:
void Start() - a method that runs when the script starts. It sets the visible property of the Cursor object to false to hide the mouse cursor, and sets the lockState property to CursorLockMode.Locked to lock the cursor to the center of the screen.
The script is short and simple, but it can be very useful in certain types of games where the mouse cursor is not needed or should not be visible to the player. By hiding the cursor and locking it to the center of the screen, the player can still use the mouse to control the game, but the cursor won't distract them or interfere with the game's visuals
----------------------------------------------------------------------------------------------------------------------------------------------------------
//HelpTextToggle.cs
----------------------------------------------------------------------------------------------------------------------------------------------------------
using System.Collections;
using UnityEngine;
public class HelpTextToggle : MonoBehaviour
{
public GameObject helpTextObject; // Reference to the HelpText GameObject
private bool isHelpTextVisible; // Keep track of the HelpText visibility state
private float hideTimer = 7.0f; // Duration to wait before auto-hiding the HelpText
private void Start()
{
isHelpTextVisible = helpTextObject.activeSelf; // Get the initial state of HelpText GameObject
}
private void Update()
{
ToggleHelpText();
}
private void ToggleHelpText()
{
// Check if the user pressed the "H" key
if (Input.GetKeyDown(KeyCode.H))
{
// Toggle the visibility of the HelpText GameObject
isHelpTextVisible = !isHelpTextVisible;
helpTextObject.SetActive(isHelpTextVisible);
// If the HelpText is visible, start the auto-hide timer
if (isHelpTextVisible)
{
StartCoroutine(AutoHideHelpText());
}
else
{
// If the HelpText is manually hidden, stop the auto-hide timer
StopCoroutine(AutoHideHelpText());
}
}
}
private IEnumerator AutoHideHelpText()
{
// Wait for the specified duration
yield return new WaitForSeconds(hideTimer);
// Hide the HelpText GameObject and update the visibility state
helpTextObject.SetActive(false);
isHelpTextVisible = false;
}
}
This is a C# script for a Unity game that toggles the visibility of a help text object when the "H" key is pressed. The script also includes a timer to automatically hide the help text after a certain duration of time.
Here is a breakdown of the script:
public GameObject helpTextObject; - a public GameObject variable to reference the help text object that will be toggled.
private bool isHelpTextVisible; - a private boolean variable to keep track of the visibility state of the help text object.
private float hideTimer = 7.0f; - a private float variable to set the duration of time to wait before automatically hiding the help text.
void Start() - a method that runs when the script starts. It gets the initial visibility state of the help text object.
void Update() - a method that runs every frame. It calls the ToggleHelpText() method to check for user input and toggle the visibility of the help text object accordingly.
void ToggleHelpText() - a method that checks if the "H" key is pressed and toggles the visibility of the help text object. If the help text object is manually hidden, the method stops the auto-hide timer. If the help text object is manually shown, the method starts the auto-hide timer.
IEnumerator AutoHideHelpText() - a coroutine that waits for the specified duration of time before hiding the help text object and updating the visibility state.
The script provides a convenient way for players to toggle the visibility of the help text object and also ensures that the help text is automatically hidden after a certain duration to prevent clutter on the screen.
Comments
Post a Comment