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<T>(string path) where T : Object
// => UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
//}
/// <summary>
/// 获取当前打开的所有场景中的根物体
/// </summary>
/// <param name="Result">物体列表</param>
public static List<GameObject> GetRootGameObjectsInAllScene()
{
var Result = new List<GameObject>();
List<GameObject> sub = new List<GameObject>();
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);
}
}
|