Advertisement
celinedrules

PortalSO.cs

Mar 2nd, 2023 (edited)
1,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | Source Code | 0 0
  1. using System;
  2. using System.Linq;
  3. using Game;
  4. using UnityEditor;
  5. using UnityEngine;
  6.  
  7. namespace SceneManagement
  8. {
  9.     [CreateAssetMenu(fileName = "New Portal", menuName = "RPG/Portal")]
  10.     [Serializable]
  11.     public class PortalSo : ScriptableObject
  12.     {
  13.         [SerializeField] private SceneReference sceneToLoad;
  14.         [SerializeField, HideInInspector] private PortalSo portalData;
  15.  
  16.         public SceneReference SceneToLoad
  17.         {
  18.             get => sceneToLoad;
  19.             set => sceneToLoad = value;
  20.         }
  21.  
  22.         public PortalSo PortalData
  23.         {
  24.             get => portalData;
  25.             set => portalData = value;
  26.         }
  27.     }
  28.  
  29. #if UNITY_EDITOR
  30.     [CustomEditor(typeof(PortalSo))]
  31.     public class PortalSoEditor : Editor
  32.     {
  33.         private PortalSo portalSo;
  34.         private PortalSo[] portalSos;
  35.         private int selectedIndex;
  36.         private string[] portalNames;
  37.        
  38.         private void OnEnable()
  39.         {
  40.             portalSo = target as PortalSo;
  41.  
  42.             if (portalSo is null)
  43.                 return;
  44.  
  45.             portalSos = Utilities.GetAllInstances<PortalSo>();
  46.            
  47.             portalNames = new string[portalSos.Length];
  48.             selectedIndex = portalSos.ToList().IndexOf(portalSo.PortalData);
  49.  
  50.             for (int i = 0; i < portalSos.Length; i++)
  51.                 portalNames[i] = portalSos[i].name;
  52.         }
  53.  
  54.         public override void OnInspectorGUI()
  55.         {
  56.             base.OnInspectorGUI();
  57.             selectedIndex = EditorGUILayout.Popup(new GUIContent("Portal Data"), selectedIndex, portalNames);
  58.             portalSo.PortalData = portalSos[selectedIndex];
  59.            
  60.             EditorUtility.SetDirty(target);
  61.         }
  62.     }
  63. #endif
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement