那些经历过的Bug Unity的Invoke方法
有一个游戏对象,上面挂着 3 个脚本,如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class SaveAndRead : MonoBehaviour { public static SaveAndRead instance; /// <summary>
/// 保存所有 单独的DataStore
/// </summary>
public List<DataStore> allAloneDataStores = new List<DataStore>(); /// <summary>
/// 初始化SaveAndRead类
/// </summary>
public void InitSaveAndRead()
{
if (instance == null)
{
instance = new SaveAndRead();
}
} void Awake()
{
InitSaveAndRead();
GetAll_AreaDataStoresInTrigger();
} void GetAll_AreaDataStoresInTrigger()
{
//拿到所有子物体的 AreaAloneDataStores类
AreaAloneDataStores[] temp_list02 = GetComponentsInChildren<AreaAloneDataStores>();
for (int i = ; i < temp_list02.Length; i++)
{
for (int j = ; j < temp_list02[i].aloneDataStores.Count; j++)
{
allAloneDataStores.Add(temp_list02[i].aloneDataStores[j]);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class DistinguishDataStoresInTrigger : MonoBehaviour { /// <summary>
/// 存放单独游戏对象的类
/// </summary>
AreaAloneDataStores temp_AreaAloneDataStorest;
/// <summary>
/// 存放触发Trigger的所有游戏对象的List
/// </summary>
public List<GameObject> objs = new List<GameObject>(); void Start()
{
//拿到存放单独游戏对象的类
temp_AreaAloneDataStorest = gameObject.GetComponent<AreaAloneDataStores>();
//确保先通过 OnTriggerEnter 拿到游戏对象 , 再调用 GetAllDataStores 拿到游戏对象上的 DataStore
Invoke("GetAllDataStores", 1f);
}
/// <summary>
/// 拿到所有触发Trigger的游戏对象
/// </summary>
/// <param name="other"></param>
void OnTriggerEnter(Collider other)
{
if (other.gameObject.layer == )
{
objs.Add(other.gameObject);
other.gameObject.SetActive(false);
}
}
/// <summary>
/// 拿到所有的DataStore
/// </summary>
void GetAllDataStores()
{
for (int i = ; i < objs.Count; i++)
{
//拿到 Trigger 的 DataStoreSaveToTrigger 脚本里面存的 DataStore
DataStore temp_DataStore = objs[i].GetComponent<DataStoreSaveToTrigger>().dataStore;
//Debug.Log(this.name + "--" + objs[i].name);
//判断 DataStore 是否是Alone的,并加到不同的类的List里
if (temp_DataStore.isAloneSave == true)
{
temp_AreaAloneDataStorest.aloneDataStores.Add(temp_DataStore);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class AreaAloneDataStores : MonoBehaviour { /// <summary>
/// 单独的DataStore的List
/// </summary>
public List<DataStore> aloneDataStores = new List<DataStore>();
}
Bug:
代码的意思是通过 DistinguishDataStoresInTrigger 脚本拿到一些游戏对象,然后对这些游戏对象上的 DataStoreSaveToTrigger 脚本里的 DataStore进行分类,DataStore里的isAloneSave是true时,就把 DataStore 放在 AreaAloneDataStores类 的 aloneDataStores 里。
当时是有2个 DataStore 的 isAloneSave 是 true ,在 DistinguishDataStoresInTrigger 脚本 进行判断后,也确实放了2个 DataStore 到 AreaAloneDataStores类 的 aloneDataStores 里,但是在 SaveAndRead脚本 里通过 temp_list02[i].aloneDataStores 来获得AreaAloneDataStores类 的 aloneDataStores 时,里面却一个也没有。
原因:
是 DistinguishDataStoresInTrigger脚本 里的 Invoke("GetAllDataStores", 1f);
因为它所以在 DistinguishDataStoresInTrigger脚本 执行了 Start 方法 1 秒之后,才开始执行 GetAllDataStores 方法,才把 2 个 DataStore 放到 AreaAloneDataStores类 的 aloneDataStores 里。
而 SaveAndRead脚本 在Awake 方法里就通过 GetAll_AreaDataStoresInTrigger 方法拿到了 AreaAloneDataStores类 的 aloneDataStores 。
所以在 SaveAndRead脚本里拿到 AreaAloneDataStores类 的 aloneDataStores 的时候,DistinguishDataStoresInTrigger脚本 还没有把 2 个 DataStore 放到 AreaAloneDataStores类 的 aloneDataStores 里。
解决方法:
把 SaveAndRead类 Awake 方法里的 GetAll_AreaDataStoresInTrigger(); 改为 Invoke("GetAll_AreaDataStoresInTrigger", 2f); 就可以了。
那些经历过的Bug Unity的Invoke方法的更多相关文章
- Unity Invoke 方法
Invoke() 方法是 Unity3D 的一种委托机制 如: Invoke("a", 5); 它的意思是:5 秒之后调用 a() 方法: 使用 Invoke() 方法需要注意 ...
- (@WhiteTaken)Unity中Invoke的用法
今天无意间读到大神写的代码,看到了Invoke函数,于是产生兴趣.后来才明白自己要学习的东西还有很多. 下面讲用法. Invoke是延时调用函数,在用Invoke函数之前需要引入命名空间using U ...
- Unity中Invoke 和 InvokeRepeating的区别
Invoke() 方法是 Unity3D 的一种委托机制 如: Invoke("Test", 5); 它的意思是:5 秒之后调用 Test() 方法: 使用 Invoke() ...
- Unity MonoBehaviour.Invoke 调用
使用 Invoke() 方法需要注意 3点: 1 :它应该在 脚本的生命周期里的(Start.Update.OnGUI.FixedUpdate.LateUpdate)中被调用: 2:Invoke(); ...
- JAVA深入研究——Method的Invoke方法。
在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一样,去调用父类的对象也会报错,虽然这是很符合多态的现象,也符合java的动态绑定规范,但还是想弄懂java ...
- JAVA深入研究——Method的Invoke方法
http://www.cnblogs.com/onlywujun/p/3519037.html 在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一样,去调用 ...
- JAVA深入研究——Method的Invoke方法(转)
在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一样,去调用父类的对象也会报错,虽然这是很符合多态的现象,也符合java的动态绑定规范,但还是想弄懂java ...
- 深入解析Java反射-invoke方法
博客原文:http://www.sczyh30.com/posts/Java/java-reflection-2/ 上篇文章中回顾了一下Java反射相关的基础内容.这一节我们来深入研究Method类中 ...
- JAVA深入研究——Method的Invoke方法(转)
原文地址:http://www.cnblogs.com/onlywujun/p/3519037.html 在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一 ...
随机推荐
- virtualbox复制虚拟机网络问题
virtulbox复制虚拟机由于mac地址问题会导致网卡不可以用 1:修改mac地址 需要在virtualbox修改虚拟机网络选项卡下面的mac地址 2:修改ifcfg-eth0 把HWADDR的值设 ...
- hustOJ SPJ(special judge)模板
#include <stdio.h> #include <math.h> #define PI acos(-1.0) #define AC 0 #define WA 1 int ...
- Servlet视频-开发第一个java web(最简单的java web程序)(二)
web项目有目录结构要求 WEB-INFO 文件夹 是一个Servlet规范,必须要这么命名,在换个文件夹里面如果创建一个jsp文件是不能直接访问的,在WEB-INfO文件夹之外创建的jsp可以直接访 ...
- c#操作json 使用JavaScriptSerializer
需要引用:System.Web.Extensions /// <summary> /// json的信息.保证定义的变量和json的字段一样(也可以使用struct) /// </s ...
- fiddler抓包后Jmeter实现登录接口
登录接口测试时,先要抓取登录接口,我们使用fiddler来抓包,如下图: 然后再使用Jmeter,填写相对于的参数. 查看结果树: 关键在与抓包,搞清楚抓包的信息,可以用fiddler和火狐和谷歌.我 ...
- 2019-RHCSA-红帽题库(稳定)
RHCSA考题 Hostname:station.rhce.ccIP address:192.168.122.100Netmask:255.255.255.0Gateway:192.168.122.1 ...
- CAT部署集成文档
1. 下载编译 1.1 下载源码 首先,到项目的git网页下载整个项目: https://github.com/dianping/cat 1.2 打包安装 接着就是进入这个项目的目录,运行打包安装命 ...
- ubuntu不能自windows拖拽文件解决办法
由虚拟机的安装更新所致,及时安装更新vmware tools 命令:tar -xzvf VMwareTools-10.0.6-3595377.tar.gz进入解压后的目录,执行:sudo ./wmw ...
- node 后台搭建笔记
*注:本文是个人记录,非常粗略 1.建立server服务的文件被单独放置在一个文件夹下或最外层 2.src文件包含:controller/model/routes 文件夹 和 app.js文件
- jupyter notebook 设置默认目录
1.打开 cmd 输入命令 jupyter notebook --generate-config 可以看到生成文件的路径,这个就是生成的配置文件jupyter_notebook_config.py, ...