263 lines
9.0 KiB
C#
263 lines
9.0 KiB
C#
|
|
using System;
|
||
|
|
using System.IO;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
using TMPro;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.Networking;
|
||
|
|
using UnityEngine.SceneManagement;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
|
||
|
|
namespace JTN
|
||
|
|
{
|
||
|
|
public class Utils : MonoBehaviour
|
||
|
|
{
|
||
|
|
public bool scrolling;
|
||
|
|
public GameObject objectRot;
|
||
|
|
public GameObject sn;
|
||
|
|
public float movespeed = 3;
|
||
|
|
public float srate = 2;
|
||
|
|
private float tm = 0;
|
||
|
|
private bool once = false;
|
||
|
|
private GameObject currentObjectRot;
|
||
|
|
|
||
|
|
public string GameObjectList;
|
||
|
|
public bool DestroyParentIfNotDevelopment = false;
|
||
|
|
public GameObject Parent;
|
||
|
|
public TMP_InputField timeScaler;
|
||
|
|
public bool UnlockCursorInThisScene = false;
|
||
|
|
public static bool PublicUnlockCursorInThisScene = false;
|
||
|
|
public FirstPersonController playerController;
|
||
|
|
public GameObject Player;
|
||
|
|
public Animation animationToPlay;
|
||
|
|
public GameObject DeactivateObjectOnGameObjectActivate;
|
||
|
|
public GameObject PlayAnimOnActive;
|
||
|
|
public string animName;
|
||
|
|
public bool PlayOnceOnly = true;
|
||
|
|
int timesAnimPlayed = 0;
|
||
|
|
public GameObject LoadSceneOnObjectActivateOrDeactivate;
|
||
|
|
public bool RunOnDeactivate;
|
||
|
|
public string SceneToLoad;
|
||
|
|
public AudioSource audioSource;
|
||
|
|
public AdvLevelMan AdvancedLevelManager;
|
||
|
|
private int LoadedTimes = 0;
|
||
|
|
|
||
|
|
public static void SwitchScene(String SceneName)
|
||
|
|
{
|
||
|
|
Debug.LogError("Attempting to load scene " + SceneName);
|
||
|
|
try
|
||
|
|
{
|
||
|
|
SceneManager.LoadScene(SceneName);
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
Debug.LogError(e.ToString());
|
||
|
|
Debug.LogError("Try using the developer menu to open " + SceneName);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static async Task<AudioClip> LoadClip(string path)
|
||
|
|
{
|
||
|
|
AudioClip clip = null;
|
||
|
|
using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.WAV))
|
||
|
|
{
|
||
|
|
uwr.SendWebRequest();
|
||
|
|
|
||
|
|
// wrap tasks in try/catch, otherwise it'll fail silently
|
||
|
|
try
|
||
|
|
{
|
||
|
|
while (!uwr.isDone) await Task.Delay(5);
|
||
|
|
|
||
|
|
if (uwr.result == UnityWebRequest.Result.ConnectionError) Debug.Log($"{uwr.error}");
|
||
|
|
else
|
||
|
|
{
|
||
|
|
clip = DownloadHandlerAudioClip.GetContent(uwr);
|
||
|
|
Debug.Log("Loaded AudioClip from " + path);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception err)
|
||
|
|
{
|
||
|
|
Debug.Log($"{err.Message}, {err.StackTrace}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return clip;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void TimeScale()
|
||
|
|
{
|
||
|
|
if (timeScaler)
|
||
|
|
{
|
||
|
|
var numTime = float.Parse(timeScaler.text);
|
||
|
|
Time.timeScale = numTime;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void CustomTimeScale(float timeScale)
|
||
|
|
{
|
||
|
|
Time.timeScale = timeScale;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void DisableTextMeshProButton(Button Button)
|
||
|
|
{
|
||
|
|
Button.interactable = false;
|
||
|
|
}
|
||
|
|
public void EnableTextMeshProButton(Button Button)
|
||
|
|
{
|
||
|
|
Button.interactable = true;
|
||
|
|
}
|
||
|
|
private void Start()
|
||
|
|
{
|
||
|
|
if (scrolling)
|
||
|
|
{
|
||
|
|
currentObjectRot = Instantiate(objectRot, sn.transform.position, sn.transform.rotation);
|
||
|
|
}
|
||
|
|
|
||
|
|
GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
|
||
|
|
foreach (GameObject go in allObjects)
|
||
|
|
GameObjectList = GameObjectList + ", " + go.name;
|
||
|
|
if (!Debug.isDebugBuild || Application.platform != RuntimePlatform.WindowsEditor)
|
||
|
|
Destroy(Parent);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void FixedUpdate()
|
||
|
|
{
|
||
|
|
if (scrolling)
|
||
|
|
{
|
||
|
|
if (tm < srate)
|
||
|
|
{
|
||
|
|
tm += Time.deltaTime;
|
||
|
|
}
|
||
|
|
else if (!once && tm < srate)
|
||
|
|
{
|
||
|
|
tm = 0;
|
||
|
|
currentObjectRot = Instantiate(objectRot, sn.transform.position, sn.transform.rotation);
|
||
|
|
}
|
||
|
|
currentObjectRot.GetComponent<RectTransform>().position = currentObjectRot.GetComponent<RectTransform>().position + (Vector3.forward * movespeed) * Time.deltaTime;
|
||
|
|
once = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (LoadSceneOnObjectActivateOrDeactivate && AdvancedLevelManager)
|
||
|
|
if (RunOnDeactivate)
|
||
|
|
{
|
||
|
|
if (!LoadSceneOnObjectActivateOrDeactivate.activeInHierarchy && LoadedTimes == 0)
|
||
|
|
{
|
||
|
|
Debug.Log("Loading scene " + SceneToLoad);
|
||
|
|
AdvancedLevelManager.SwitchSceneAdv(SceneToLoad);
|
||
|
|
LoadedTimes++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else if (!RunOnDeactivate && LoadSceneOnObjectActivateOrDeactivate.activeInHierarchy && LoadedTimes == 0)
|
||
|
|
{
|
||
|
|
Debug.Log("Loading scene " + SceneToLoad);
|
||
|
|
AdvancedLevelManager.SwitchSceneAdv(SceneToLoad);
|
||
|
|
LoadedTimes++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void ToggleActive(GameObject obj)
|
||
|
|
{
|
||
|
|
obj.SetActive(!obj.activeInHierarchy);
|
||
|
|
}
|
||
|
|
public static void ToggleAnimState(Animation anim)
|
||
|
|
{
|
||
|
|
anim.enabled = !anim.enabled;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void SetGameObjectToActive(GameObject obj)
|
||
|
|
{
|
||
|
|
obj.SetActive(true);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void PlayAudioClipOnce(AudioClip clip)
|
||
|
|
{
|
||
|
|
if (!audioSource)
|
||
|
|
Debug.LogError("No audio source was linked when running the script attached to " + gameObject.name);
|
||
|
|
else
|
||
|
|
audioSource.PlayOneShot(clip);
|
||
|
|
}
|
||
|
|
public static void SetGameObjectToInactive(GameObject obj)
|
||
|
|
{
|
||
|
|
obj.SetActive(false);
|
||
|
|
}
|
||
|
|
public void SetFullscreen (bool FullscreenState)
|
||
|
|
{
|
||
|
|
Debug.Log($"Set fullscreen pref to {FullscreenState}");
|
||
|
|
if (FullscreenState)
|
||
|
|
{
|
||
|
|
PlayerPrefs.SetInt("FullscreenState", 1);
|
||
|
|
Screen.fullScreenMode = FullScreenMode.FullScreenWindow;
|
||
|
|
Screen.SetResolution(1533, 720, false);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
PlayerPrefs.SetInt("FullscreenState", 0);
|
||
|
|
Screen.fullScreenMode = FullScreenMode.Windowed;
|
||
|
|
Screen.SetResolution(1533, 720, false);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
public void DisableAnim(Animation anim)
|
||
|
|
{
|
||
|
|
anim.enabled = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void PlayAnimByName(Animation anim, string animName)
|
||
|
|
{
|
||
|
|
anim.PlayQueued(animName);
|
||
|
|
}
|
||
|
|
public void PlayAnimation(string animName)
|
||
|
|
{
|
||
|
|
if (animationToPlay)
|
||
|
|
animationToPlay.PlayQueued(animName);
|
||
|
|
}
|
||
|
|
public void playOutAnim(Animation anim)
|
||
|
|
{
|
||
|
|
PlayAnimByName(anim, "SetPanOUT");
|
||
|
|
}
|
||
|
|
public void playInAnim(Animation anim)
|
||
|
|
{
|
||
|
|
PlayAnimByName(anim, "SetPanOUT");
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Update()
|
||
|
|
{
|
||
|
|
PublicUnlockCursorInThisScene = UnlockCursorInThisScene;
|
||
|
|
if (!UnlockCursorInThisScene)
|
||
|
|
{
|
||
|
|
if (Input.GetKeyDown(KeyCode.LeftAlt))
|
||
|
|
{
|
||
|
|
Cursor.lockState = CursorLockMode.None;
|
||
|
|
if (playerController)
|
||
|
|
playerController.cameraCanMove = false;
|
||
|
|
}
|
||
|
|
else if (Input.GetKeyUp(KeyCode.LeftAlt))
|
||
|
|
{
|
||
|
|
Cursor.lockState = CursorLockMode.Locked;
|
||
|
|
if (playerController)
|
||
|
|
playerController.cameraCanMove = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
Cursor.lockState = CursorLockMode.None;
|
||
|
|
|
||
|
|
if (DeactivateObjectOnGameObjectActivate)
|
||
|
|
if (gameObject.activeInHierarchy)
|
||
|
|
{
|
||
|
|
DeactivateObjectOnGameObjectActivate.SetActive(false);
|
||
|
|
gameObject.SetActive(false);
|
||
|
|
}
|
||
|
|
if (PlayAnimOnActive)
|
||
|
|
if (PlayAnimOnActive.activeInHierarchy)
|
||
|
|
{
|
||
|
|
if (timesAnimPlayed < 1)
|
||
|
|
{
|
||
|
|
if (PlayOnceOnly)
|
||
|
|
timesAnimPlayed++;
|
||
|
|
PlayAnimOnActive.SetActive(false);
|
||
|
|
PlayAnimation(animName);
|
||
|
|
Debug.Log("Looping state: " + animationToPlay.clip.isLooping);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|