当前位置:网站首页>Unity通过反射创建动态类
Unity通过反射创建动态类
2022-04-21 06:28:00 【菜菜码农柠檬哥】
在游戏制作过程中,难免会遇到一些不方便进行挂载缺需要调用的脚本,这时候我们可以使用反射来动态创建类,并且调用其类中的方法。
1.首先先要获取到需要动态创建类的类名。CurrentWeapon为当前武器名称,这里使用了武器名+Fire的统一命名方法来区分不同武器的开火类。
private WeaponBase m_CurrentWeapon;
Type type = Utility.Assembly.GetType(CurrentWeapon+"Fire");
2.如果该类存在且不为空,则动态创建此类,这里使用了派生类使用as转换为基类的方法,因为每个武器都有开火的方法,所以动态创建的所有枪,都作为了武器基类的派生类。
if (type != null)
{
m_CurrentWeapon = Activator.CreateInstance(type) as WeaponBase;
}
3.随后调用了自己的开火方法。
m_CurrentWeapon.OnFire(PlayerTrans,FireToPos);
版权声明
本文为[菜菜码农柠檬哥]所创,转载请带上原文链接,感谢
https://blog.csdn.net/y1139735983/article/details/120721944
边栏推荐
- 【保姆安装教程】Linux操作系统中源码下载MySQL5.7
- you-get 命令与踩坑
- WordPress plugin - display posts
- Advanced system settings click no response, can not open the solution
- Udevd retrieves the kernel module and loads the demo
- UCOSIII移植到UCOSII—任务挂起/恢复/钩子/(保姆级教程)
- Dpdk problem analysis: dpdk-20.11 ice 100g network card RSS_ Invalid hash configuration
- 程序启动顺序引发的血案之 dpdk 进程死锁
- PowerShell - because running scripts is prohibited on this system
- PowerShell - 因为在此系统上禁止运行脚本
猜你喜欢

WordPress plugin - display posts

Fundamentals of graphics | area light rendering based on LTC

【LeetCode 67】二个进制数求和

WordPress address siteurl error, unable to log in solution

【保姆安装教程】Linux操作系统中源码下载MySQL5.7

Substring Inversion (Easy Version)

PG database cannot use zh_ CN. UTF-8:initdb: error: locale “zh_CN.UTF-8“ requires unsupported encoding “GBK“

Oracle uses DBMS_ Crypto realizes MD5 value calculation
图形学基础|基于SDF的卡通阴影图

C#中的类型转换
随机推荐
WordPress modify upload file size limit
Oracle生成随机数
数据的增删查改
动画—Keyframes介绍
Data heterogeneity scheme
D. Optimal Partition 线段树优化dp
虚幻引擎之多线程渲染机制
dpdk-16.04 igb crc length 统计问题
从 systemd-udevd 运行 log 中研究其自动加载内核模块的过程
MMIO 与 PMIO 技术
【LeetCode 150 】逆波兰表达式求值
传染病模型进行仿真Matlab(仅做Matlab仿真学习与练习,非实际情况与应用)
读书笔记-思维的精进
view-source 解析
The use of NuMA by dpdk and the basis of performance tuning under multi NUMA architecture
【眼见非实-Bugku CTF】
MySQL workbench cannot use clear text authentication over non SSL connections problem solving
《21天实战caffe》1-7天学习笔记一
对个人与环境的几个观点
Reading notes - refinement of thinking