Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Casting from Type

Discussion in 'Scripting' started by Grzp, Nov 20, 2022.

  1. Grzp

    Grzp

    Joined:
    Jan 25, 2018
    Posts:
    31
    Hi, I have a question.
    There is posibility to create gameobject based on Type type. I can give an example.
    Code (CSharp):
    1. public class Items<T, U> : CreateDataBaseClass where T : Item where U : ItemObject<T>
    2. {
    3.     private const string PATH_FOR_ITEMS = "Assets/ScriptableObjects/Items/";
    4.     private const string PATH_FOR_ITEMS_OBJECT = "Assets/Prefabs/Items";
    5.     private const string PREFAB_EXTENSION = ".prefab";
    6.  
    7.     [ShowInInspector, ValueDropdown(nameof(FindItemClass))] public T NewItem { get; private set; }
    8.     [ShowInInspector, ValueDropdown(nameof(FindItemObject))] public Type ItemObject {get; private set;}
    9.  
    10.     [ShowInInspector, ValueDropdown(nameof(FindComponents))] public Type[] ComponetsToAdd { get; private set; }
    11.     public Items()
    12.     {
    13.         NewItem = ScriptableObject.CreateInstance<T>();
    14.     }
    15.  
    16.     [Button("Create item")]
    17.     protected override void CreateData()
    18.     {
    19.         if (Directory.Exists(PATH_FOR_ITEMS + typeof(T).Name) && m_scriptableObjectName != string.Empty)
    20.         {
    21.             AssetDatabase.CreateAsset(NewItem, PATH_FOR_ITEMS + typeof(T).Name + "/" + m_scriptableObjectName + ASSET_EXTENSION);
    22.             AssetDatabase.CreateAsset(ItemObject as U, PATH_FOR_ITEMS_OBJECT + typeof(T).Name + "/" + m_scriptableObjectName + PREFAB_EXTENSION);
    23.             AssetDatabase.SaveAssets();
    24.         }
    25.         else
    26.         {
    27.             Directory.CreateDirectory(PATH_FOR_ITEMS + typeof(T).Name);
    28.             AssetDatabase.CreateAsset(NewItem, PATH_FOR_ITEMS + typeof(T).Name + "/" + m_scriptableObjectName + ASSET_EXTENSION);
    29.             //AssetDatabase.CreateAsset(NewItemObject, PATH_FOR_ITEMS_OBJECT + typeof(U).Name + "/" + m_scriptableObjectName + PREFAB_EXTENSION);
    30.             AssetDatabase.SaveAssets();
    31.         }
    32.  
    33.         for (int i = 0; i < ComponetsToAdd.Length; i++)
    34.         {
    35.             //ItemObject.AddComponent(ComponetsToAdd[i]);
    36.         }
    37.  
    38.     }
    39.     protected virtual IEnumerable<T> FindItemClass => TypeCache
    40.      .GetTypesDerivedFrom<T>()
    41.      .Select(type => (T)Activator.CreateInstance(type));
    42.  
    43.     private IEnumerable<Type> FindComponents => from assembly in AppDomain.CurrentDomain.GetAssemblies()
    44.                                                  from type in assembly.GetTypes()
    45.                                                  where typeof(Component).IsAssignableFrom(type)
    46.                                                  select type;
    47.     private IEnumerable<Type> FindItemObject => from assembly in AppDomain.CurrentDomain.GetAssemblies()
    48.                                             from type in assembly.GetTypes()
    49.                                             where typeof(U).IsAssignableFrom(type)
    50.                                             select type;
    51.  
    52. }
    So is this possible to cast ItemObject and then create asset? ItemObject<T> classs is MonoBehaviour.
     
  2. Grzp

    Grzp

    Joined:
    Jan 25, 2018
    Posts:
    31
    In 22 line a I have a bug, beacuse casting this Type in not properly.