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. Dismiss Notice

Generate Assets for each subscript

Discussion in 'Scripting' started by LemonadeSergeant, Jan 23, 2021.

  1. LemonadeSergeant

    LemonadeSergeant

    Joined:
    Mar 26, 2019
    Posts:
    3
    So i have a base class similar to the below.

    Code (CSharp):
    1. public abstract class EnchantmentBase : monobehaviour
    2. {
    3.  
    4. public virtual void onHit()
    5.  
    6. }
    7. public virtual void onKill(){
    8. return desc;
    9. }
    10. }
    I then have currently around 20 classes that extend this class.
    now for me to use the class i'm having to manually create a prefab and assign the script to them which is getting a bit tideous.

    I've looked into a few ways to try and implement some way to automate this. I found the info on here https://docs.unity3d.com/ScriptReference/AssetDatabase.FindAssets.html

    The issue with this is you have to specifically set what class you using. Does anyone know a way i can have it so it will find all the classes that extend my base class create an asset from these.

    Cheers for any help or links.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You can't use the AssetDatabase for this because MonoBehaviours aren't considered assets for all intents and purposes. You'll need to use reflection to search for the various components:

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Reflection;
    5. using UnityEngine;
    6. using UnityEditor;
    7.  
    8. public sealed class FindAllChildClasses : Editor
    9. {
    10.    [MenuItem("Experiments/Find All Child Classes")]
    11.    public static void Search()
    12.    {
    13.        Debug.LogFormat("Starting search...");
    14.  
    15.        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
    16.        List<Type> types = new List<Type>();
    17.  
    18.        foreach (Assembly assembly in assemblies)
    19.            types.AddRange(assembly.GetTypes());
    20.  
    21.        Debug.LogFormat("Searching {0} types", types.Count);
    22.  
    23.        foreach (Type type in types)
    24.        {
    25.            if (type.IsSubclassOf(typeof(Component)) == true && type.IsAbstract == false)
    26.            {
    27.                GameObject protoGameObject = new GameObject
    28.                {
    29.                    name = type.Name
    30.                };
    31.  
    32.                protoGameObject.AddComponent(type);
    33.  
    34.                Debug.LogFormat("Created GameObject with component: {0}", type.Name);
    35.            }
    36.        }
    37.    }
    38. }
    39.  
    Replace typeof(Component) in the if statement with the type you want a child of. I don't recommend running this as is, or you'll end up with tens of thousands of GameObjects in your scene like I did :D

    I'll leave it to you to automate saving them as prefabs since you know your project and structure.

    EDIT: And obviously be careful with this kind of stuff, you might be playing with fire.
     
    Last edited: Jan 23, 2021
    LemonadeSergeant likes this.
  3. LemonadeSergeant

    LemonadeSergeant

    Joined:
    Mar 26, 2019
    Posts:
    3
    Brilliant, Thanks very much. That's a massive help that's going to save me a hell of a lot time.