Exemplo n.º 1
0
    public bool Init()
    {
        _table.Clear();
        var type = typeof(T);

        _name = type.Name.Replace("Table_", "Tables/");

        //本地数据优先读取
        var asset = Resources.Load <TextAsset>(_name);

        // 自定义加载
        if (!asset && LoadRes != null)
        {
            asset = LoadRes(_name);
        }

        if (asset == null || asset.bytes == null)
        {
            return(false);
        }
        var idField = typeof(T).GetField("Id");

        if (idField == null)
        {
            return(false);
        }
        T[] parser = null;
        try
        {
            parser = CsvImporter.Parser <T>(asset.bytes);
        }
        catch (Exception e)
        {
            Debug.LogError("table:" + _name + "Error" + e.Message);
        }

        if (parser == null)
        {
            return(false);
        }
        var count = parser.Length;

        for (var i = 0; i < count; i++)
        {
            var id = (int)idField.GetValue(parser[i]);
            if (!_table.ContainsKey(id))
            {
                _table.Add(id, parser[i]);
            }
            else
            {
                Debug.LogWarningFormat("{0} table.ID {1} is duplicated!", _name, id);
            }
        }
        return(true);
    }