Unity冷门语法用法

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Example : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        fun01();
    }

    public void Fun001()
    {
        bool isInit = false;
        string a = isInit ? "Test" : "hello";
        Debug.Log(a);
    }

    //public class AeeetEditorLoad
    //{
    //    public static T Load(string path) where T : Object
    //    => UnityEditor.AssetDatabase.LoadAssetAtPath(path);

    //}
    /// 
    /// 获取当前打开的所有场景中的根物体
    /// 
    /// 物体列表
    public static List GetRootGameObjectsInAllScene()
    {
        var Result = new List();
        List sub = new List();
        for (int i = 0; i < SceneManager.sceneCount; i++)
        {
            SceneManager.GetSceneAt(i).GetRootGameObjects(sub);
            Result.AddRange(sub);
        }
        return Result;
    }


    public void fun01()
    {
        TestA a=new TestA();
        Type t = a.GetType();
        
        MethodInfo method01 = t.GetMethod("ReadData");
        MethodInfo method02 = t.GetMethod("ReadData2");
        object[] parameters = new object[] { "ReadData" };

        object[] parameters02 = new object[] { "ReadData2" };
        object obj = Activator.CreateInstance(t);//非静态方法调用
        method01.Invoke(obj, parameters);

        method02?.Invoke(null, parameters02);
        //调用自身类
        MethodInfo method = this.GetType().GetMethod("Fun001");
        method?.Invoke(this, null);
    }
  
}
 class TestA
{

    public void ReadData(string a)
    {
        Debug.Log(a);
    }

    public static void ReadData2(string b)
    {
        Debug.Log(b);
    }
}

你可能感兴趣的:(Unity冷门语法用法)