那些经历过的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,方法名一 ...
随机推荐
- Struts2返回JSON数据的具体应用范例
早在我刚学Struts2之初的时候,就想写一篇文章来阐述Struts2如何返回JSON数据的原理和具体应用了,但苦于一直忙于工作难以抽身,渐渐的也淡忘了此事.直到前两天有同事在工作中遇到这个问题,来找 ...
- preventDefault()对象
preventDefault() 方法 Event 对象 定义和用法 取消事件的默认动作. 语法 event.preventDefault() 说明 该方法将通知 Web 浏览器不要执行与事件关联的默 ...
- asp 控件定时器 局部刷新
<asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePan ...
- Percona Toolkit安装、使用
percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,这些任务包括: l 检查master和slave数据的一致性 l 有效地对记 ...
- Shell-2-命令之乐
1.cat (1)基本用法 [root@cai tmp]# cat 1.txt 2.txt this is a test1 this is a test 2 (2)cat -s file(删除额外空白 ...
- Java 大数相乘、大数相加、大数相减
思路来源:: https://blog.csdn.net/lichong_87/article/details/6860329 /** * @date 2018/6/22 * @description ...
- 使用js实现水波效果
使用到的工具:jQuery Ripples Plugin 下载安装 git clone https://github.com/sirxemic/jquery.ripples.git 引入jquery, ...
- [HAOI2012]音量调节 BZOJ2748 dp
题目描述 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都需要改变一次音量.在演出开始之前,他已经做好一个列表,里面写着每首歌开始之前他想要改变的音量是多少. ...
- .net mvc 框架实现后台管理系统 3
左侧菜单实现 默认html <div class="layui-side layui-bg-black h-slide"> <div class="la ...
- Step by Step: 基于MFC下的COM组件开发-Helloworld
http://blog.csdn.net/sybifei/article/details/45008745 [这篇文章有问题, 仅供参考] http://blog.csdn.net/define_us ...