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<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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_1208409371

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值