那些经历过的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,方法名一 ...
随机推荐
- netty源码阅读之UnpooledByteBufAllocator
使用IDEA阅读源码Navigate下面的工具是个好东西 .可以帮助分析类的结构等 ByteBufAllocator主要用来生成三种ByteBuf :HeadBuffer,DirectBuffer,C ...
- WordCount-软件测试初体验
github:https://github.com/skz12345/WordCount PSP2.1 PSP阶段 预估耗时(分钟) 实际耗时(分钟) Planning 计划 40 60 · Esti ...
- Android wifi管理类:WifiAdmin
1.wifi的五种状态: WIFI_STATE_DISABLED WIFI网卡不可用 WIFI_STATE_DISABLING WIFI网卡正在关闭 WIFI_STATE_ENABLED ...
- C# WinForm:无法访问已释放的对象
C#在父窗口中调用子窗口的过程: 1. 创建子窗口对象 2. 显示子窗口对象 笔者的程序中,主窗体MainFrm通过菜单调用子窗口ChildFrm.在窗体中定义了子窗口对象,然后在菜单项点击事件中 ...
- C#非泛型集合和泛型集合
第一 : ArrayList(非泛型集合) 与List(泛型集合) ArrayList 是数组的复杂版本.ArrayList 类提供在大多数 Collections 类中提供但不在 Array(数 ...
- C#校验算法列举
以下是工作中常用的几种校验算法,后期将不断更新 和校验 /// <summary> /// CS和校验 /// </summary> /// <param name=&q ...
- UWP&WP8.1 附加属性 和WebView的NavigateToString方法XAML绑定方法
附加属性,即为添加一个没有的属性的. 使用方法和依赖属性相似,个人理解就是特殊形式的依赖属性. 经常的用处,以一个简单的来说,比如一个控件的某一个属性我们想在XAML中给其绑定数据.但是我们在XAML ...
- Codeforces Round #538 (Div. 2)D(区间DP,思维)
#include<bits/stdc++.h>using namespace std;int a[5007];int dp[5007][5007];int main(){ int n ...
- 识别子串 (string)——后缀自动机+线段树
题目 [题目描述] 一般地,对于一个字符串 S,和 S 中第 $ i $ 个字符 x,定义子串 $ T=S(i.j) $ 为一个关于 x 的识别子申,当且仅当: 1.$ i \leq x \leq j ...
- Task4
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...