当前位置:网站首页>Unity3D不同脚本函数或参数之间调用
Unity3D不同脚本函数或参数之间调用
2022-07-20 14:10:00 【AplusX】
假如,我们有两个脚本:Main.cs
,SliderControl.cs
。现在希望从SliderControl.cs
调用Main.cs
内的函数或参数。
(一)、被调用脚本函数为static类型,调用时直接用 类名.参数
public class Main: MonoBehaviour {
public static int index = 0;
}
// 在SliderControl.cs中调用index
int para = Main.index;
(二)、GameObject.Find(“脚本所挂载在的物体的名字”)找到游戏对象,再通过GetComponent<脚本名>().函数名()调用脚本中的函数,只能调用public类型函数
public class Main: MonoBehaviour {
public Vector3 CalculateTransPose(){
//注意:必须是public函数
...
}
}
// 在SliderControl.cs中调用CalculateTransPose()
public class SliderControl : MonoBehaviour
{
private Slider silder;
public GameObject _GG1;
// Start is called before the first frame update
void Start()
{
silder = GetComponent<Slider>();
}
// Update is called once per frame
void Update()
{
silder.value += 0.1f * Time.deltaTime;
//调用Main.cs中的CalculateTransPose函数
Vector3 func= _GG1.GetComponent<Main>().CalculateTransPose();
}
}
(三)、GameObject.Find(“脚本所在的物体的名字”).SendMessage(“函数名”);(还未尝试)
//能调用public和private类型函数
据说,这是早期unity提供的方式,这个方法已经过时,在效率上比较低,故这里不再推荐。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
private void TakeDamage()
{
}
}
// 在其他脚本中调用TakeDamage()函数
GameObject.Find("Enemy_1").SendMessage("TakeDamage", SendMessageOptions.DontRequireReceiver);
除了SendMessage,还有SendMessageUpwards和BroadcastMessage函数,三个函数的参数相似,都是方法名+方法的参数+额外信息选项组成。
SendMessageOptions
- SendMessageOptions.RequireReceiver //如果没有找到相应函数,会报错(默认是这个状态)
- SendMessageOptions.DontRequireReceiver //即使没有找到相应函数,也不会报错,自动忽略
区别
SendMessage仅向指定对象的所有脚本推送消息
SendMessageUpwards向指定对象和它的所有父物体推送消息
BroadcastMessage向指定对象和它的所有子物体推送消息
注意:
传递的对象似乎一次只能传一个变量,因此可以将多个变量打包进一个数组进行传递。
边栏推荐
- mysql乐观锁总结和实践(一)
- 时间的魔术
- 计协C语言第四次培训 练习题
- 【等保常见问题解答】等保测评机构能帮忙做等保整改吗?
- (homework) C language questions: structural alignment number, bit segment, Consortium
- 7.16 - 每日一题 - 408
- (affectation) c langue: numéro d'alignement de la structure, segment de position, Consortium
- leetcode:730. 统计可以生成多少个不同回文子序列
- Summary and practice of MySQL optimistic lock (I)
- Matlab point cloud roughness calculation
猜你喜欢
7.19 - 每日一题 - 408
暑假打工 2 个月,让我明白了 Keepalived 高可用的三种路由方案
After working for two months in the summer vacation, I understood three routing schemes of keepalived high availability
菜鸟教程之工具使用——IDEA 查看子类(实现类)类图
今日睡眠质量记录73分
斯坦福、Meta AI新研究:实现AGI之路,数据剪枝比我们想象得更重要
App automated test-2 Appium recording test cases
Judge whether view slides in or out of the visible area of the screen
MLX90640 红外热成像传感器测温模块开发笔记(二)
7.16 - 每日一题 - 408
随机推荐
淘宝/天猫同款商品搜索API接口详细演示
计协C语言第四次培训 练习题
免费拥有个人云主机——AWS免费EC2套餐
These colleges and universities will score! Please note that!
如何只将人体步态轮廓中的人给裁剪出来
今日睡眠质量记录73分
菜鸟教程之工具使用——IDEA 查看子类(实现类)类图
什么是IFTMCS指示合同状态报文?
Mlx90640 infrared thermal imaging sensor temperature measurement module development notes (I)
Icml2022 Awards announced: 15 outstanding papers, selected by Fudan University, Xiamen University and Shanghai Jiaotong University
App automated test -1 Appium installation and configuration
MATLAB 点云粗糙度计算
查看磁盘空间占用情况
Thinking about fruit
Fork in multi process
ICML2022奖项公布:15篇杰出论文,复旦、厦大、上交大研究入选
Stay away from the market
【性能优化】MySQL常用慢查询分析工具
Scala advanced (VIII): collection content summary (Part 2)
网易严选,使用uni-app实现,包含后台数据文件