0

MonoBehaviourUnity 3D エンジンを複製しようとしています。私は Linux で monodevelop を使用しており、ほとんどのテストは Windows で行われますUnity 3D engine editor

詳細については、ここMonoBehaviour.Updateで読むことができます

Update10ミリ秒ごとに継承するすべてのタイプでメソッドを呼び出したいですMonoBehavior

これが私がスタートでやっている方法です

using System;
using System.Reflection;
public class MonoBehaviour{

    public static void Main (){
        Type[] types = Assembly.GetExecutingAssembly().GetTypes();

        foreach (Type type in types) {
            if (type.IsSubclassOf(typeof(MonoBehaviour))){

                System.Reflection.MethodInfo mInfo = type.GetMethod ("Start", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { }, null); // it is run 2 times
                if (mInfo != null) {
                    ConstructorInfo ctor = type.GetConstructor (new Type[] { });
                    if (ctor != null) {
                        object inst = ctor.Invoke (new object[] { });
                        mInfo.Invoke (inst, new object[] { });
                    }
                }
            }
        }
    }
}

class example : MonoBehaviour{
    void Start(){
        // this works perfectly
        Console.WriteLine ("HelloWorld");
    }
    void Update(){
        // I want this to be run every 10 ms
        Console.WriteLine ("HelloinUpdate");
    }
}
4

3 に答える 3

2

リフレクションを使用せずにこれを行うことができます。これは少し高速 (かつ安全) です。

using System;
using System.Linq;
using System.Timers;

public static class Program
{
    public static void Main()
    {
        IEnumerable<Type> assemblyTypes = typeof(MonoBehaviour).Assembly.GetTypes();

        IEnumerable<Type> behaviourTypes = assemblyTypes
            .Where(type => typeof(MonoBehaviour).IsAssignableFrom(type))
            .Where(type => !type.IsAbstract);

        List<MonoBehaviour> behaviours = behaviourTypes
            .Select(Activator.CreateInstance)
            .Cast<MonoBehaviour>()
            .ToList();

        foreach (MonoBehaviour monoBehaviour in behaviours)
        {
            monoBehaviour.Start();
        }

        var timer = new Timer(10 /* Milliseconds */);

        timer.Elapsed += (sender, eventArgs) =>
        {
            foreach (MonoBehaviour monoBehaviour in behaviours)
            {
                monoBehaviour.Update();
            }
        };

        timer.Start();

        Console.WriteLine("Press a key to stop.");
        Console.ReadKey();
    }
}

public abstract class MonoBehaviour
{
    public virtual void Start()
    {
    }

    public virtual void Update()
    {
    }

    protected static void Log(string message)
    {
        Console.WriteLine("{0} - {1}", DateTime.Now, message);
    }
}

public class Behaviour1 : MonoBehaviour
{
    public override void Start()
    {
        Log("Behaviour1 - Start");
    }

    public override void Update()
    {
        Log("Behaviour1 - Update");
    }
}

public class Behaviour2 : MonoBehaviour
{
    public override void Start()
    {
        Log("Behaviour2 - Start");
    }

    public override void Update()
    {
        Log("Behaviour2 - Update");
    }
}:

出力は次のようになります (タイマーを 1000ms に調整しました)。

出力

于 2014-03-13T12:41:23.967 に答える