/// <summary> /// 添加lua /// </summary> /// <param name="formName"></param> /// <param name="luaStr"></param> public static void AddUIPrefabNode(UIPrefabConfigNode node) { if (!m_PrefabsDict.ContainsKey(node.UIFormName)) { m_PrefabsDict[node.UIFormName] = node; if (UNITY_Android && !UNITY_Editor) { LoadAndroidSteamingAsset(node); } } }
//将streamingAssetsPath路径下的资源加载到缓存 private static void LoadAndroidSteamingAsset(UIPrefabConfigNode uiPrefabInfoNode) { //加载预设 if (uiPrefabInfoNode.UIFormPrefabDirType == "streamingAssetsPath") { Instance.StartCoroutine(WWWLoadAssetBundle(GetPath(uiPrefabInfoNode.UIFormPrefabDirType, uiPrefabInfoNode.UIFormPrefabUrl), (bundle) => { if (bundle != null) { GameObject prefab = bundle.LoadAsset <GameObject>(uiPrefabInfoNode.UIFormPrefabName); AndroidStreamingAssetsPathPrefab[uiPrefabInfoNode.UIFormName] = prefab; } })); } //加载Lua脚本 if (uiPrefabInfoNode.UIFormLuaScripDirType == "streamingAssetsPath") { if (uiPrefabInfoNode.UIFormLuaScriptAssetBudle) { Instance.StartCoroutine(WWWLoadAssetBundle(GetPath(uiPrefabInfoNode.UIFormLuaScripDirType, uiPrefabInfoNode.UIFormLuaScriptUrl), (bundle) => { if (bundle != null) { TextAsset text = bundle.LoadAsset <TextAsset>(uiPrefabInfoNode.UIFormClassName); AndroidStreamingAssetsPathText[uiPrefabInfoNode.UIFormClassName] = text == null ? null : text.text; } })); } else { Instance.StartCoroutine(WWWLoadFileStr(GetPath(uiPrefabInfoNode.UIFormLuaScripDirType, uiPrefabInfoNode.UIFormLuaScriptUrl), (text) => { AndroidStreamingAssetsPathText[uiPrefabInfoNode.UIFormClassName] = text; })); } } }
/// <summary> /// 获取预设 /// </summary> /// <param name="uiFormName"></param> /// <returns></returns> public static GameObject LoadUIPrefab(string uiFormName) { UIPrefabConfigNode uiPrefabInfoNode = null; m_PrefabsDict.TryGetValue(uiFormName, out uiPrefabInfoNode); if (uiPrefabInfoNode == null) { Debug.LogError(uiFormName + ":UIFormName is null!"); return(null); } GameObject prefab = null; if (LoadPrefab != null) { prefab = (GameObject)LoadPrefab(uiPrefabInfoNode.UIFormPrefabName); } else { if (uiPrefabInfoNode.UIFormPrefabDirType == "Resources") { prefab = Resources.Load <GameObject>(uiPrefabInfoNode.UIFormPrefabUrl); } else if (uiPrefabInfoNode.UIFormPrefabDirType == "streamingAssetsPath") { if (UNITY_Android && !UNITY_Editor) { AndroidStreamingAssetsPathPrefab.TryGetValue(uiPrefabInfoNode.UIFormName, out prefab); } else { prefab = LoadAssetBundleObj <GameObject>(GetPath(uiPrefabInfoNode.UIFormPrefabDirType, uiPrefabInfoNode.UIFormPrefabUrl), uiPrefabInfoNode.UIFormPrefabName); } } } if (prefab == null) { Debug.LogError("prefab is null!" + uiPrefabInfoNode.UIFormPrefabUrl); return(null); } GameObject go = GameObject.Instantiate(prefab); go.name = prefab.name; Component uibase = go.GetComponent <UIBase>(); if (uibase == null) { System.Type monoType = null; if (!uiPrefabInfoNode.UIFormLuaScript) { uibaseTypes.TryGetValue(uiPrefabInfoNode.UIFormClassName, out monoType); //monoType = System.Type.GetType(uiPrefabInfoNode.UIFormClassName); } else { uibaseTypes.TryGetValue("JXFrame.View.LuaUIBehavior", out monoType); //monoType = System.Type.GetType("JXFrame.View.LuaUIBehavior"); } if (monoType == null) { Debug.LogError("monoType is null!:" + uiPrefabInfoNode.UIFormLuaScript); return(null); } uibase = go.AddComponent(monoType); if (uibase.GetType().Name == "LuaUIBehavior") { System.Reflection.MethodInfo mi = uibase.GetType().GetMethod("LoadLuaString"); if (mi != null) { mi.Invoke(uibase, new System.Object[] { uiPrefabInfoNode.UIFormLuaScriptAssetBudle, uiPrefabInfoNode.UIFormLuaScripDirType, uiPrefabInfoNode.UIFormClassName, uiPrefabInfoNode.UIFormLuaScriptUrl }); } else { Debug.LogError(uibase.GetType().Name + ":LoadLuaString(bool isAssetBundle, string dirType, string scriptName, string scriptPath) is null"); } //LuaUIBehavior luaui = uibase as LuaUIBehavior; //luaui.LoadLuaString(uiPrefabInfoNode.UIFormLuaScriptAssetBudle, uiPrefabInfoNode.UIFormLuaScripDirType, uiPrefabInfoNode.UIFormClassName, uiPrefabInfoNode.UIFormLuaScriptUrl); } } return(uibase.gameObject); }