Exemple #1
0
 /// <summary>
 /// 异步并行加载场景(可加回调)
 /// Load hot update scene async but parallel (can add callback)
 /// </summary>
 /// <param name="path"></param>
 /// <param name="callback"></param>
 /// <param name="additive"></param>
 public static void LoadSceneAsync(string path, Action callback = null, bool additive = false)
 {
     AssetMgr.LoadSceneAsync(path, additive, (p) =>
     {
         LoadSceneProgress = p;
     }, (b) =>
     {
         callback?.Invoke();
         LoadSceneProgress = 1;
     });
 }
Exemple #2
0
        private static async void Init()
        {
            _phrases = new Dictionary <string, Dictionary <string, string> >(0);
            ChangeLanguage(PlayerPrefs.GetString("JEngine.Core.Localization.language", CultureInfo.InstalledUICulture.Name));

            var file = (TextAsset)await AssetMgr.LoadAsync(CsvLoc, typeof(TextAsset));

            if (file == null)
            {
                Log.PrintError("Localization模块无效,因为没有获取到表格文件");
                return;
            }

            //获取全部行
            List <string> allRows = new List <string>(0);

            byte[]       array  = Encoding.UTF8.GetBytes(file.text);
            MemoryStream stream = new MemoryStream(array);
            StreamReader sr     = new StreamReader(stream, Encoding.Default);
            String       line;

            while ((line = sr.ReadLine()) != null)
            {
                allRows.Add(line);
            }
            sr.Close();

            string pattern = ",(?=(?:[^\\" + '"' + "]*\\" + '"' + "[^\\" + '"' + "]*\\" + '"' + ")*[^\\" + '"' + "]*$)";

            //获取语言
            var header = Regex.Split(allRows[0], pattern);

            for (int i = 1; i < header.Length; i++)
            {
                //某语言
                string lang = _getExactValue(header[i]).ToLower();
                //获取key和value
                Dictionary <string, string> p = new Dictionary <string, string>();
                for (int j = 1; j < allRows.Count; j++)
                {
                    //某一行
                    string row = allRows[j];
                    //切割
                    var cells = Regex.Split(row, pattern);
                    //添加key-value
                    p.Add(_getExactValue(cells[0]), _getExactValue(cells[i]));
                }
                //添加lang
                _phrases.Add(lang, p);
            }
        }
Exemple #3
0
        /// <summary>
        /// Set value
        /// </summary>
        /// <param name="classData"></param>
        public void SetVal(ClassData classData)
        {
            string classType =
                $"{classData.classNamespace + (classData.classNamespace == "" ? "" : ".")}{classData.className}";
            Type t           = classData.ClassType; //获取实际属性
            var  clrInstance = classData.ClrInstance;

            //绑定数据
            classData.BoundData = false;
            var fields      = classData.fields.ToArray();
            var bindingAttr = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance |
                              BindingFlags.Static;

            foreach (ClassField field in fields)
            {
                if (field.fieldType == ClassField.FieldType.NotSupported)
                {
                    continue;
                }

                object obj = null;
                try
                {
                    if (field.fieldType == ClassField.FieldType.Number)
                    {
                        var fieldType = t.GetField(field.fieldName, bindingAttr).FieldType ??
                                        (t.BaseType.GetField(field.fieldName, bindingAttr).FieldType ??
                                         (t.GetProperty(field.fieldName, bindingAttr).PropertyType ??
                                          t.BaseType.GetProperty(field.fieldName, bindingAttr).PropertyType));
                        fieldType = fieldType is ILRuntimeWrapperType wrapperType ? wrapperType.RealType : fieldType;

                        if (fieldType == typeof(SByte))
                        {
                            obj = SByte.Parse(field.value);
                            classData.BoundData = true;
                        }
                        else if (fieldType == typeof(Byte))
                        {
                            obj = Byte.Parse(field.value);
                            classData.BoundData = true;
                        }
                        else if (fieldType == typeof(Int16))
                        {
                            obj = Int16.Parse(field.value);
                            classData.BoundData = true;
                        }
                        else if (fieldType == typeof(UInt16))
                        {
                            obj = UInt16.Parse(field.value);
                            classData.BoundData = true;
                        }
                        else if (fieldType == typeof(Int32))
                        {
                            obj = Int32.Parse(field.value);
                            classData.BoundData = true;
                        }
                        else if (fieldType == typeof(UInt32))
                        {
                            obj = UInt32.Parse(field.value);
                            classData.BoundData = true;
                        }
                        else if (fieldType == typeof(Int64))
                        {
                            obj = Int64.Parse(field.value);
                            classData.BoundData = true;
                        }
                        else if (fieldType == typeof(UInt64))
                        {
                            obj = UInt64.Parse(field.value);
                            classData.BoundData = true;
                        }
                        else if (fieldType == typeof(Single))
                        {
                            obj = Single.Parse(field.value);
                            classData.BoundData = true;
                        }
                        else if (fieldType == typeof(Decimal))
                        {
                            obj = Decimal.Parse(field.value);
                            classData.BoundData = true;
                        }
                        else if (fieldType == typeof(Double))
                        {
                            obj = Double.Parse(field.value);
                            classData.BoundData = true;
                        }
                    }

                    else if (field.fieldType == ClassField.FieldType.String)
                    {
                        obj = field.value;
                        classData.BoundData = true;
                    }
                    else if (field.fieldType == ClassField.FieldType.Bool)
                    {
                        field.value         = field.value.ToLower();
                        obj                 = field.value == "true";
                        classData.BoundData = true;
                    }

                    if (field.fieldType == ClassField.FieldType.GameObject)
                    {
                        GameObject go = field.gameObject;
                        if (go == null)
                        {
                            try
                            {
                                go = field.value == "${this}"
                                    ? gameObject
                                    : GameObject.Find(field.value);
                                if (go == null) //找父物体
                                {
                                    go = FindSubGameObject(field);
                                    if (go == null) //如果父物体还不存在
                                    {
                                        continue;
                                    }
                                }
                            }
                            catch (Exception) //找父物体(如果抛出空异常)
                            {
                                go = FindSubGameObject(field);
                                if (go == null) //如果父物体还不存在
                                {
                                    continue;
                                }
                            }
                        }

                        obj = go;
                        classData.BoundData = true;
                    }
                    else if (field.fieldType == ClassField.FieldType.UnityComponent)
                    {
                        GameObject go = field.gameObject;
                        if (go == null)
                        {
                            try
                            {
                                if (field.value.Contains("."))
                                {
                                    field.value =
                                        field.value.Remove(field.value.IndexOf(".", StringComparison.Ordinal));
                                }

                                go = field.value == "${this}"
                                    ? gameObject
                                    : GameObject.Find(field.value);
                                if (go == null) //找父物体
                                {
                                    go = FindSubGameObject(field);
                                    if (go == null) //如果父物体还不存在
                                    {
                                        continue;
                                    }
                                }
                            }
                            catch (Exception) //找父物体(如果抛出空异常)
                            {
                                go = FindSubGameObject(field);
                                if (go == null) //如果父物体还不存在
                                {
                                    continue;
                                }
                            }
                        }

                        var tp = t.GetField(field.fieldName, bindingAttr);
                        if (tp == null)
                        {
                            tp = t.BaseType?.GetField(field.fieldName, bindingAttr);
                        }
                        if (tp != null)
                        {
                            var fieldType = tp.FieldType;
                            fieldType = fieldType is ILRuntimeWrapperType wrapperType ? wrapperType.RealType : fieldType;

                            if (fieldType is ILRuntimeType) //如果在热更中
                            {
                                var components = go.GetComponents <CrossBindingAdaptorType>();
                                foreach (var c in components)
                                {
                                    if (c.ILInstance.Type.ReflectionType == fieldType)
                                    {
                                        obj = c.ILInstance;
                                        classData.BoundData = true;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                var component = go.GetComponent(fieldType);
                                if (component != null)
                                {
                                    obj = component;
                                    classData.BoundData = true;
                                }
                            }
                        }
                        else
                        {
                            var pi = t.GetProperty(field.fieldName, bindingAttr);
                            if (pi == null)
                            {
                                pi = t.BaseType?.GetProperty(field.fieldName, bindingAttr);
                            }
                            if (pi != null)
                            {
                                var fieldType = pi.PropertyType;
                                fieldType = fieldType is ILRuntimeWrapperType wrapperType ? wrapperType.RealType : fieldType;

                                if (fieldType is ILRuntimeType) //如果在热更中
                                {
                                    var components = go.GetComponents <CrossBindingAdaptorType>();
                                    foreach (var c in components)
                                    {
                                        if (c.ILInstance.Type.ReflectionType == fieldType)
                                        {
                                            obj = c.ILInstance;
                                            classData.BoundData = true;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    var component = go.GetComponent(fieldType);
                                    if (component != null)
                                    {
                                        obj = component;
                                        classData.BoundData = true;
                                    }
                                }
                            }
                            else
                            {
                                Log.PrintError(
                                    $"自动绑定{name}出错:{classType}.{field.fieldName}赋值出错:{field.fieldName}不存在");
                            }
                        }
                    }
                    else if (field.fieldType == ClassField.FieldType.HotUpdateResource)
                    {
                        obj = AssetMgr.Load(field.value);
                        classData.BoundData = true;
                    }
                }
                catch (Exception except)
                {
                    Log.PrintError(
                        $"自动绑定{name}出错:{classType}.{field.fieldName}获取值{field.value}出错:{except.Message},已跳过");
                }

                //如果有数据再绑定
                if (classData.BoundData)
                {
                    void _setVal(MemberInfo mi)
                    {
                        try
                        {
                            switch (mi)
                            {
                            case null:
                                throw new NullReferenceException();

                            case FieldInfo info:
                                info.SetValue(clrInstance.ILInstance, obj);
                                break;

                            case PropertyInfo inf:
                                inf.SetValue(clrInstance.ILInstance, obj);
                                break;
                            }
                        }
                        catch (Exception e)
                        {
                            Log.PrintError(
                                $"自动绑定{name}出错:{classType}.{field.fieldName}赋值出错:{e.Message},已跳过");
                        }
                    }

                    var fi = t.GetField(field.fieldName, bindingAttr);
                    if (fi == null)
                    {
                        fi = t.BaseType?.GetField(field.fieldName, bindingAttr);
                    }
                    if (fi != null)
                    {
                        _setVal(fi);
                    }
                    else
                    {
                        var pi = t.GetProperty(field.fieldName, bindingAttr);
                        if (pi == null)
                        {
                            pi = t.BaseType?.GetProperty(field.fieldName, bindingAttr);
                        }
                        _setVal(pi);
                    }
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// 异步并行加载热更资源(可加回调)
        /// Load hot update resource async but parallel (can add callback)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <param name="callback"></param>
        /// <param name="mode"></param>
        public static async void LoadResAsync <T>(string path, Action <T> callback, MatchMode mode = MatchMode.AutoMatch) where T : UnityEngine.Object
        {
            var asset = await AssetMgr.LoadAsync(path, typeof(T));

            callback?.Invoke(asset as T);
        }
Exemple #5
0
 /// <summary>
 /// 卸载热更资源
 /// Load hot update resource
 /// </summary>
 /// <param name="path"></param>
 /// <param name="mode"></param>
 /// <returns></returns>
 public static void UnloadRes(string path, MatchMode mode = MatchMode.AutoMatch)
 {
     AssetMgr.Unload(ResPath(path, mode));
 }
Exemple #6
0
 /// <summary>
 /// 加载热更资源
 /// Load hot update resource
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="path"></param>
 /// <param name="mode"></param>
 /// <returns></returns>
 public static T LoadRes <T>(string path, MatchMode mode = MatchMode.AutoMatch) where T : UnityEngine.Object
 {
     return(AssetMgr.Load(ResPath(path, mode), typeof(T)) as T);
 }