那些经历过的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 = 0; i < temp_list02.Length; i++)
        {
            for (int j = 0; 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 == 8)
        {
            objs.Add(other.gameObject);
            other.gameObject.SetActive(false);
        }
    }
    /// <summary>
    /// 拿到所有的DataStore
    /// </summary>
    void GetAllDataStores()
    {
        for (int i = 0; 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); 就可以了。

  

原文地址:https://www.cnblogs.com/Peng18233754457/p/8674624.html