Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Reflection;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.UI;
- [CreateAssetMenu(menuName = "Scriptable Object Variables/Interface")]
- public class InterfaceVariable : ScriptableObjectInterface<IInterface>
- {
- }
- public class ScriptableObjectInterface<T> : ScriptableObjectVariable<T>
- {
- public int implementationTypeIndex;
- private T _implementation;
- public override T Value
- {
- get
- {
- if (_implementation == null)
- {
- _implementation = (T)Activator.CreateInstance(GetImplementations()[implementationTypeIndex]);
- }
- return _implementation;
- }
- set => _implementation = value;
- }
- public Type[] GetImplementations()
- {
- var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes());
- var interfaceType = typeof(T);
- return types.Where(p => interfaceType.IsAssignableFrom(p) && !p.IsAbstract).ToArray();
- }
- }
- [CustomEditor(typeof(InterfaceVariable))]
- public class ScriptableObjectInterfaceVariable : ScriptableObjectInterfaceVariable<IInterface,ScriptableObjectVariable<IInterface>>
- {
- }
- public class ScriptableObjectInterfaceVariable<TBase,TVar> : Editor where TVar : ScriptableObjectVariable<TBase>
- {
- private Type[] _implementations;
- [SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
- public override void OnInspectorGUI()
- {
- var scriptableObject = target as ScriptableObjectInterface<TBase>;
- if (scriptableObject == null)
- {
- return;
- }
- if (_implementations == null || GUILayout.Button("Refresh implementations"))
- {
- _implementations = scriptableObject.GetImplementations();
- }
- EditorGUILayout.LabelField($"Found {_implementations.Count()} implementations");
- scriptableObject.implementationTypeIndex = EditorGUILayout.Popup(new GUIContent("Implementation"),
- scriptableObject.implementationTypeIndex, _implementations.Select(impl => impl.FullName).ToArray());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment