博客
关于我
Unity调用其他脚本中的枚举 unity中三种调用其他脚本函数的方法
阅读量:316 次
发布时间:2019-03-04

本文共 986 字,大约阅读时间需要 3 分钟。

一、Unity中三种调用其他脚本函数的方法

在Unity中调用其他脚本中的函数时,可以采用三种不同的方法。每种方法都有其特定的适用场景和限制条件。

第一种方法:通过静态方法调用脚本函数。这种方法非常不实用,因为它只能调用脚本中的静态方法,且不适用于大多数场景。

第二种方法:使用SendMessage函数调用脚本中的函数。这种方法可以通过指定物体名称和函数名称来调用脚本中的函数,无论目标函数是public还是private类型的。语法格式为: GameObject.Find("脚本所在物体名").SendMessage("函数名");

第三种方法:使用GetComponent方法获取脚本组件并调用函数。这种方法可以通过指定物体名称获取脚本组件,然后调用目标函数。语法格式为: GameObject.Find("脚本所在物体名").GetComponent

<脚本名>
().函数名(); 但需要注意的是,这种方法只能用于调用public类型的函数。

二、Unity中调用其他脚本中的枚举

在Unity中,您可以通过脚本组件来调用其他脚本中的枚举值。以下是实现方法:

1. 首先,在目标脚本中定义一个枚举类型:

```c# public enum MyEnum { VALUE1 = 0, VALUE2 = 1 } ```

2. 在调用脚本中使用SendMessage方法传递枚举值:

```c# public void CallEnumFunction() { GameObject otherScript = GameObject.Find("目标物体"); otherScript.SendMessage(MyEnum.VALUE1); } ```

3. 或者使用GetComponent方法获取目标脚本组件并调用函数:

```c# public void CallEnumFunction() { GameObject otherScript = GameObject.Find("目标物体"); MyScript script = otherScript.GetComponent
(); script.FunctionName(MyEnum.VALUE1); } ```

通过以上方法,您可以在Unity中轻松调用其他脚本中的枚举值,并根据具体需求选择合适的调用方法。

转载地址:http://oaiq.baihongyu.com/

你可能感兴趣的文章
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用json节点解析JSON数据
查看>>
Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
查看>>
Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
查看>>
Node-RED中实现HTML表单提交和获取提交的内容
查看>>
Node.js 函数是什么样的?
查看>>