Advertisement
celinedrules

Portal.cs

Mar 2nd, 2023
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.AI;
  7. using UnityEngine.SceneManagement;
  8.  
  9. namespace SceneManagement
  10. {
  11.     [Serializable]
  12.     public class Portal : MonoBehaviour
  13.     {
  14.         [SerializeField] private Transform spawnPoint;
  15.         [SerializeField, HideInInspector] private PortalSo portalData;
  16.  
  17.         public PortalSo SelectedPortal
  18.         {
  19.             get => portalData;
  20.             set => portalData = value;
  21.         }
  22.  
  23.         private void OnTriggerEnter(Collider other)
  24.         {
  25.             if (other.CompareTag("Player"))
  26.             {
  27.                 StartCoroutine(Transition());
  28.             }
  29.         }
  30.  
  31.         private IEnumerator Transition()
  32.         {
  33.             DontDestroyOnLoad(gameObject);
  34.             yield return SceneManager.LoadSceneAsync(portalData.SceneToLoad);
  35.  
  36.             Portal otherPortal = GetOtherPortal();
  37.             UpdatePlayer(otherPortal);
  38.  
  39.             Destroy(gameObject);
  40.         }
  41.  
  42.         private void UpdatePlayer(Portal otherPortal)
  43.         {
  44.             GameObject player = GameObject.FindWithTag("Player");
  45.             player.GetComponent<NavMeshAgent>().Warp(otherPortal.spawnPoint.position);
  46.             player.transform.rotation = otherPortal.spawnPoint.rotation;
  47.         }
  48.  
  49.         private Portal GetOtherPortal()
  50.         {
  51.             var portals = FindObjectsOfType<Portal>();
  52.  
  53.             return portals.FirstOrDefault(portal => portal.portalData == portalData.DestinationPortal);
  54.         }
  55.  
  56.         private void OnDrawGizmos()
  57.         {
  58.             BoxCollider col = GetComponent<BoxCollider>();
  59.             DrawBoxCollider(new Color(0, 0, 255, .25f), col);
  60.         }
  61.  
  62.         private void DrawBoxCollider(Color color, BoxCollider boxCollider)
  63.         {
  64.             Gizmos.matrix = Matrix4x4.TRS(transform.TransformPoint(boxCollider.center), transform.rotation, transform.lossyScale);
  65.             Gizmos.color = color;
  66.             Gizmos.DrawCube(Vector3.zero, boxCollider.size);
  67.         }
  68.     }
  69.  
  70. #if UNITY_EDITOR
  71.     [CustomEditor(typeof(Portal))]
  72.     public class PortalEditor : Editor
  73.     {
  74.         private PortalSo[] portals;
  75.         private Portal portal;
  76.         private int selectedIndex;
  77.         private string[] portalNames;
  78.  
  79.         private void OnEnable()
  80.         {
  81.             portal = target as Portal;
  82.  
  83.             if (portal is null)
  84.                 return;
  85.  
  86.             portals = GetAllInstances<PortalSo>();
  87.  
  88.             portalNames = new string[portals.Length];
  89.             selectedIndex = portals.ToList().IndexOf(portal.SelectedPortal);
  90.  
  91.             for (int i = 0; i < portals.Length; i++)
  92.                 portalNames[i] = portals[i].name;
  93.         }
  94.  
  95.         public override void OnInspectorGUI()
  96.         {
  97.             base.OnInspectorGUI();
  98.  
  99.             selectedIndex = EditorGUILayout.Popup(new GUIContent("Portal Data"), selectedIndex, portalNames);
  100.             portal.SelectedPortal = portals[selectedIndex];
  101.  
  102.             EditorUtility.SetDirty(target);
  103.         }
  104.  
  105.         private T[] GetAllInstances<T>() where T : ScriptableObject
  106.         {
  107.             string[] guids = AssetDatabase.FindAssets("t:" + typeof(T).Name);
  108.             var a = new T[guids.Length];
  109.  
  110.             for (int i = 0; i < guids.Length; i++)
  111.             {
  112.                 string path = AssetDatabase.GUIDToAssetPath(guids[i]);
  113.                 a[i] = AssetDatabase.LoadAssetAtPath<T>(path);
  114.             }
  115.  
  116.             return a;
  117.         }
  118.     }
  119. #endif
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement