提交 e83b89d2 编写于 作者: T tanghai

一般只能从hotfix层调用到mono层,尽量少从mono层调到脚本层

上级 56ff7d9f
......@@ -7,15 +7,13 @@ namespace Model
/// 消息分发组件
/// </summary>
[ObjectEvent(EntityEventId.MessageDispatherComponent)]
public class MessageDispatherComponent: Component, IAwake<AppType>, ILoad
public class MessageDispatherComponent: Component, IAwake, ILoad
{
private AppType AppType;
private Dictionary<ushort, List<IInstanceMethod>> handlers;
private DoubleMap<ushort, Type> opcodeTypes = new DoubleMap<ushort, Type>();
public void Awake(AppType appType)
public void Awake()
{
this.AppType = appType;
this.Load();
}
......
fileFormatVersion: 2
guid: 8e97599ee372f6f40aaa5e743851a74c
timeCreated: 1498117781
timeCreated: 1498200974
licenseType: Free
MonoImporter:
serializedVersion: 2
......
......@@ -99,17 +99,9 @@ namespace Model
{
// 异步load资源到内存cache住
UnityEngine.Object[] assets;
AssetBundleLoaderAsync assetBundleLoaderAsync = null;
try
{
assetBundleLoaderAsync = new AssetBundleLoaderAsync(assetBundle);
assets = await assetBundleLoaderAsync.LoadAllAssetsAsync();
}
finally
{
assetBundleLoaderAsync?.Dispose();
using (AssetBundleLoaderAsync assetBundleLoaderAsync = new AssetBundleLoaderAsync(assetBundle))
{
assets = await assetBundleLoaderAsync.LoadAllAssetsAsync();
}
......
......@@ -21,8 +21,6 @@
public string Name { get; set; }
public SceneType SceneType { get; private set; }
public Scene(): base(EntityType.Scene)
{
}
......
......@@ -34,7 +34,7 @@ namespace Model
this.RegisterDelegate();
this.RegisterRedirection();
IType hotfixInitType = AppDomain.LoadedTypes["Hotfix.HotfixInit"];
IType hotfixInitType = AppDomain.LoadedTypes["Hotfix.Init"];
start = hotfixInitType.GetMethod("Start", 0);
update = hotfixInitType.GetMethod("Update", 0);
onApplicationQuit = hotfixInitType.GetMethod("OnApplicationQuit", 0);
......@@ -50,9 +50,9 @@ namespace Model
private void Update()
{
ObjectEvents.Instance.Update();
this.AppDomain.Invoke(this.update, null, this.param0);
ObjectEvents.Instance.Update();
}
private void OnApplicationQuit()
......@@ -75,14 +75,14 @@ namespace Model
public unsafe void RegisterRedirection()
{
var mi = typeof(Log).GetMethod("Debug", new System.Type[] { typeof(string) });
MethodInfo mi = typeof(Log).GetMethod("Debug", new Type[] { typeof(string) });
this.AppDomain.RegisterCLRMethodRedirection(mi, ILRedirection.LogDebug);
}
public void RegisterDelegate()
{
AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
AppDomain.DelegateManager.RegisterMethodDelegate<System.Byte[], System.Int32, System.Int32>();
AppDomain.DelegateManager.RegisterMethodDelegate<byte[], int, int>();
}
......
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Model;
using UnityEngine;
namespace Hotfix
{
[ObjectEvent(EntityEventId.ResourcesComponent)]
public class ResourcesComponent: Component
{
public static AssetBundleManifest AssetBundleManifestObject { get; set; }
private readonly Dictionary<string, UnityEngine.Object> resourceCache = new Dictionary<string, UnityEngine.Object>();
private readonly Dictionary<string, AssetBundle> bundleCaches = new Dictionary<string, AssetBundle>();
public K GetReference<K>(string bundle, string prefab, string key) where K : class
{
GameObject gameObject = this.GetAsset<GameObject>(bundle, prefab);
return gameObject.GetComponent<ReferenceCollector>().Get<K>(key);
}
public K GetAsset<K>(string bundleName, string prefab) where K : class
{
string path = $"{bundleName}.unity3d/{prefab}".ToLower();
UnityEngine.Object resource = null;
if (this.resourceCache.TryGetValue(path, out resource))
{
return resource as K;
}
if (Define.LoadResourceType == LoadResourceType.Async)
{
if (!this.bundleCaches.ContainsKey($"{bundleName}.unity3d".ToLower()))
{
return null;
}
throw new Exception($"异步加载资源,资源不在resourceCache中: {bundleName} {path}");
}
try
{
resource = ResourceHelper.LoadResource(bundleName, prefab);
this.resourceCache.Add(path, resource);
}
catch (Exception e)
{
throw new Exception($"加载资源出错,输入路径:{path}", e);
}
return resource as K;
}
public async Task DownloadAndCacheAsync(string uri, string assetBundleName)
{
assetBundleName = (assetBundleName + ".unity3d").ToLower();
AssetBundle assetBundle;
// 异步下载资源
string url = uri + "StreamingAssets/" + assetBundleName;
int count = 0;
while (true)
{
WWWAsync wwwAsync = null;
try
{
++count;
if (count > 1)
{
await Hotfix.Scene.GetComponent<TimerComponent>().WaitAsync(2000);
}
if (this.Id == 0)
{
return;
}
wwwAsync = new WWWAsync();
await wwwAsync.LoadFromCacheOrDownload(url, ResourcesComponent.AssetBundleManifestObject.GetAssetBundleHash(assetBundleName));
assetBundle = wwwAsync.www.assetBundle;
break;
}
catch (Exception e)
{
Log.Error(e.ToString());
}
finally
{
wwwAsync?.Dispose();
}
}
if (!assetBundle.isStreamedSceneAssetBundle)
{
// 异步load资源到内存cache住
UnityEngine.Object[] assets;
AssetBundleLoaderAsync assetBundleLoaderAsync = null;
try
{
assetBundleLoaderAsync = new AssetBundleLoaderAsync(assetBundle);
assets = await assetBundleLoaderAsync.LoadAllAssetsAsync();
}
finally
{
assetBundleLoaderAsync?.Dispose();
}
foreach (UnityEngine.Object asset in assets)
{
string path = $"{assetBundleName}/{asset.name}".ToLower();
this.resourceCache[path] = asset;
}
}
if (this.bundleCaches.ContainsKey(assetBundleName))
{
throw new Exception($"重复加载资源: {assetBundleName}");
}
this.bundleCaches[assetBundleName] = assetBundle;
}
public override void Dispose()
{
if (this.Id == 0)
{
return;
}
base.Dispose();
foreach (var assetBundle in bundleCaches)
{
assetBundle.Value?.Unload(true);
}
}
}
}
\ No newline at end of file
......@@ -23,7 +23,7 @@ namespace Hotfix
this.params3[0] = scene;
this.params3[1] = type;
this.params3[2] = parent;
return (UI)Init.Instance.AppDomain.Invoke(this.methodInfo, this.instance, this.params3);
return (UI)Model.Init.Instance.AppDomain.Invoke(this.methodInfo, this.instance, this.params3);
}
}
......
......@@ -19,9 +19,9 @@
{
public Scene Parent { get; set; }
public string Name { get; set; }
public Model.Scene ModelScene { get; set; } = new Model.Scene();
public SceneType SceneType { get; private set; }
public string Name { get; set; }
public Scene(): base(EntityType.Scene)
{
......@@ -39,6 +39,8 @@
}
base.Dispose();
this.ModelScene.Dispose();
}
}
}
\ No newline at end of file
......@@ -3,13 +3,14 @@ using Model;
namespace Hotfix
{
public static class HotfixInit
public static class Init
{
private static void Start()
{
try
{
Hotfix.Scene.AddComponent<ResourcesComponent>();
Hotfix.Scene.ModelScene.AddComponent<NetOuterComponent>();
Hotfix.Scene.ModelScene.AddComponent<ResourcesComponent>();
Hotfix.Scene.AddComponent<UIComponent>();
Hotfix.Scene.AddComponent<UnitComponent>();
Hotfix.Scene.GetComponent<EventComponent>().Run(EventIdType.InitSceneStart);
......
using Model;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Hotfix
{
......@@ -9,7 +8,7 @@ namespace Hotfix
{
public UI Create(Scene scene, int type, UI parent)
{
GameObject bundleGameObject = scene.GetComponent<ResourcesComponent>().GetAsset<GameObject>("uilobby", "Lobby");
GameObject bundleGameObject = scene.ModelScene.GetComponent<ResourcesComponent>().GetAsset<GameObject>("uilobby", "Lobby");
GameObject lobby = UnityEngine.Object.Instantiate(bundleGameObject);
lobby.layer = LayerMask.NameToLayer(LayerNames.UI);
UI ui = new UI(scene, type, parent, lobby);
......
......@@ -56,7 +56,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Helper\JsonHelper.cs" />
<Compile Include="HotfixInit.cs" />
<Compile Include="Init.cs" />
<Compile Include="LitJson\IJsonWrapper.cs" />
<Compile Include="LitJson\JsonData.cs" />
<Compile Include="LitJson\JsonException.cs" />
......@@ -71,7 +71,6 @@
<Compile Include="Component\EventComponent.cs" />
<Compile Include="Component\GameObjectComponent.cs" />
<Compile Include="Component\KVComponent.cs" />
<Compile Include="Component\ResourcesComponent.cs" />
<Compile Include="Component\RobotComponent.cs" />
<Compile Include="Component\TimeComponent.cs" />
<Compile Include="Component\TimerComponent.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册