提交 b436a558 编写于 作者: T tanghai

1.修复服务端一个position报错

2.ResourceComponent并发加载修复,简化封装的Unity异步加载ab包的方法
3.更新运行指南
上级 b72bef52
# 运行步骤
##### 1.visual studio必须使用vs2017(更新到最新版), VS2017需要勾选安装以下内容:
##### 1.visual studio必须使用vs2019(更新到最新版), VS2019需要勾选安装以下内容:
a. .net 桌面开发
b. visual studio tools for unity
c. 去net core 官网下载安装 .net core 2.1
##### 2. master分支需要unity2018.3版(目前还是beta版), ET4.0请使用unity2017.4版
b. 去net core 官网下载安装 .net5
##### 2. master分支需要unity2020.3版
##### 3. 启动unity, 菜单 File->open project->open 选中ET/Unity文件夹,点击选择文件夹按钮。
##### 4.点击Unity菜单Assets->open C# project启动vs 编译(一定要编译,右键VS解决方案,全部编译)
##### 5.用vs2017打开ET/Server/Server.sln 编译(一定要编译,右键VS解决方案,全部编译)
##### 6.打开Unity->tools菜单->命令行配置,选择LocalAllServer.txt 这是启动单一App的方式,如果要启动一组多App服务器,在命令行工具中选择127.0.0.1.txt,点击启动即可,具体配置都可以自己用这个命令行配置工具修改
##### 7.点击工具中的启动,这样就启动了服务端(也可以用VS启动,方便单步调试)
##### 8.运行Unity,输入帐号,点击登录这时日志 连接Gate成功,表示运行OK!
##### 5.用vs2019打开ET/Client-Server.sln 编译(一定要编译,右键VS解决方案,全部编译)
# 测试状态同步demo, 帧同步demo已经删除,需要的话请看ET4.0
......@@ -28,8 +23,8 @@ c. 去net core 官网下载安装 .net core 2.1
一. 出错原因都是:
1.中文目录。
2.vs没有安装vs tools或者不是最新的vs tools。
3.没安装 .net core 2.1
2.vs没有安装vs
3.没安装 .net5
4.没编译服务端
5.VS要更新到最新版本
......
......@@ -4,7 +4,7 @@ __讨论QQ群 : 474643097__
[ET论坛](https://et-framework.cn)
# 注意!现在master正在开发中,商业使用请用5.0分支,6.0(master分支)可以参考
# 注意!现在master(6.0版)还在开发中,不过基本架构已经完成
# ET的介绍:
ET是一个开源的游戏客户端(基于unity3d)服务端双端框架,服务端是使用C# .net core开发的分布式游戏服务端,其特点是开发效率高,性能强,双端共享逻辑代码,客户端服务端热更机制完善,同时支持可靠udp tcp websocket协议,支持服务端3D recast寻路等等
......
......@@ -6,7 +6,12 @@ namespace ET
{
protected override async ETTask Run(EventType.ChangePosition args)
{
Transform transform = args.Unit.GetComponent<GameObjectComponent>().GameObject.transform;
GameObjectComponent gameObjectComponent = args.Unit.GetComponent<GameObjectComponent>();
if (gameObjectComponent == null)
{
return;
}
Transform transform = gameObjectComponent.GameObject.transform;
transform.position = args.Unit.Position;
await ETTask.CompletedTask;
}
......
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
namespace ET
{
public class AssetsBundleLoaderAsyncSystem : UpdateSystem<AssetsBundleLoaderAsync>
{
public override void Update(AssetsBundleLoaderAsync self)
{
self.Update();
}
}
public class AssetsBundleLoaderAsync : Entity
{
private AssetBundleCreateRequest request;
private ETTaskCompletionSource<AssetBundle> tcs;
public void Update()
{
if (!this.request.isDone)
{
return;
}
ETTaskCompletionSource<AssetBundle> t = tcs;
t.SetResult(this.request.assetBundle);
}
public override void Dispose()
{
if (this.IsDisposed)
{
return;
}
base.Dispose();
}
public ETTask<AssetBundle> LoadAsync(string path)
{
this.tcs = new ETTaskCompletionSource<AssetBundle>();
this.request = AssetBundle.LoadFromFileAsync(path);
return this.tcs.Task;
}
}
}
fileFormatVersion: 2
guid: f59fcdbebced4076b5c33870f5df3463
timeCreated: 1510746129
\ No newline at end of file
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
namespace ET
{
public class AssetsLoaderAsyncAwakeSystem : AwakeSystem<AssetsLoaderAsync, AssetBundle>
{
public override void Awake(AssetsLoaderAsync self, AssetBundle a)
{
self.Awake(a);
}
}
public class AssetsLoaderAsyncUpdateSystem : UpdateSystem<AssetsLoaderAsync>
{
public override void Update(AssetsLoaderAsync self)
{
self.Update();
}
}
public class AssetsLoaderAsync : Entity
{
private AssetBundle assetBundle;
private AssetBundleRequest request;
private ETTaskCompletionSource tcs;
public void Awake(AssetBundle ab)
{
this.assetBundle = ab;
}
public void Update()
{
if (!this.request.isDone)
{
return;
}
ETTaskCompletionSource t = tcs;
t.SetResult();
}
public override void Dispose()
{
if (this.IsDisposed)
{
return;
}
base.Dispose();
this.assetBundle = null;
this.request = null;
}
public async ETTask<UnityEngine.Object[]> LoadAllAssetsAsync()
{
await InnerLoadAllAssetsAsync();
return this.request.allAssets;
}
private ETTask InnerLoadAllAssetsAsync()
{
this.tcs = new ETTaskCompletionSource();
this.request = assetBundle.LoadAllAssetsAsync();
return this.tcs.Task;
}
}
}
fileFormatVersion: 2
guid: f5aab0e13efc491cbae3d11cc8ed42fc
timeCreated: 1510746129
\ No newline at end of file
......@@ -19,6 +19,7 @@ namespace ET
self.AssetBundle = a;
self.Name = abName;
self.RefCount = 1;
self.AlreadyLoadAssets = false;
}
}
......@@ -31,25 +32,21 @@ namespace ET
self.RefCount = 0;
self.Name = "";
self.AlreadyLoadAssets = false;
self.AssetBundle = null;
}
}
public class ABInfo: Entity
{
public string Name
{
get;
set;
}
public string Name { get; set; }
public int RefCount
{
get;
set;
}
public int RefCount { get; set; }
public AssetBundle AssetBundle;
public bool AlreadyLoadAssets;
public void Destroy(bool unload = true)
{
if (this.AssetBundle != null)
......@@ -64,6 +61,22 @@ namespace ET
// 用于字符串转换,减少GC
public static class AssetBundleHelper
{
public static async ETTask<AssetBundle> UnityLoadBundleAsync(string path)
{
var tcs = new ETTaskCompletionSource<AssetBundle>();
AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
request.completed += operation => { tcs.SetResult(request.assetBundle); };
return await tcs;
}
public static async ETTask<UnityEngine.Object[]> UnityLoadAssetAsync(AssetBundle assetBundle)
{
var tcs = new ETTaskCompletionSource<UnityEngine.Object[]>();
AssetBundleRequest request = assetBundle.LoadAllAssetsAsync();
request.completed += operation => { tcs.SetResult(request.allAssets); };
return await tcs;
}
public static string IntToString(this int value)
{
string result;
......@@ -120,37 +133,29 @@ namespace ET
public class ResourcesComponent: Entity
{
public static ResourcesComponent Instance
{
get;
set;
}
public static ResourcesComponent Instance { get; set; }
private AssetBundleManifest AssetBundleManifestObject { get; set; }
public AssetBundleManifest AssetBundleManifestObject
{
get;
set;
}
public Dictionary<int, string> IntToStringDict = new Dictionary<int, string>();
public Dictionary<string, string> StringToABDict = new Dictionary<string, string>();
public Dictionary<string, string> BundleNameToLowerDict = new Dictionary<string, string>() { { "StreamingAssets", "StreamingAssets" } };
private readonly Dictionary<string, Dictionary<string, UnityEngine.Object>> resourceCache = new Dictionary<string, Dictionary<string, UnityEngine.Object>>();
private readonly Dictionary<string, Dictionary<string, UnityEngine.Object>> resourceCache =
new Dictionary<string, Dictionary<string, UnityEngine.Object>>();
private readonly Dictionary<string, ABInfo> bundles = new Dictionary<string, ABInfo>();
public void Awake()
{
Instance = this;
if (Define.IsAsync)
{
LoadOneBundle("StreamingAssets");
AssetBundleManifestObject = (AssetBundleManifest)GetAsset("StreamingAssets", "AssetBundleManifest");
LoadOneBundle("StreamingAssets");
AssetBundleManifestObject = (AssetBundleManifest) GetAsset("StreamingAssets", "AssetBundleManifest");
}
}
......@@ -165,7 +170,7 @@ namespace ET
Instance = null;
foreach (KeyValuePair<string, ABInfo> abInfo in this.bundles)
foreach (var abInfo in this.bundles)
{
abInfo.Value.Destroy();
}
......@@ -177,7 +182,7 @@ namespace ET
this.BundleNameToLowerDict.Clear();
}
private string[] GetDependencies(string assetBundleName, bool isScene = false)
private string[] GetDependencies(string assetBundleName)
{
string[] dependencies = new string[0];
if (DependenciesCache.TryGetValue(assetBundleName, out dependencies))
......@@ -188,10 +193,7 @@ namespace ET
if (!Define.IsAsync)
{
#if UNITY_EDITOR
if (isScene == false)
{
dependencies = AssetDatabase.GetAssetBundleDependencies(assetBundleName, true);
}
dependencies = AssetDatabase.GetAssetBundleDependencies(assetBundleName, true);
#endif
}
else
......@@ -203,19 +205,19 @@ namespace ET
return dependencies;
}
public string[] GetSortedDependencies(string assetBundleName, bool isScene = false)
private string[] GetSortedDependencies(string assetBundleName)
{
Dictionary<string, int> info = new Dictionary<string, int>();
List<string> parents = new List<string>();
CollectDependencies(parents, assetBundleName, info, isScene);
var info = new Dictionary<string, int>();
var parents = new List<string>();
CollectDependencies(parents, assetBundleName, info);
string[] ss = info.OrderBy(x => x.Value).Select(x => x.Key).ToArray();
return ss;
}
private void CollectDependencies(List<string> parents, string assetBundleName, Dictionary<string, int> info, bool isScene = false)
private void CollectDependencies(List<string> parents, string assetBundleName, Dictionary<string, int> info)
{
parents.Add(assetBundleName);
string[] deps = GetDependencies(assetBundleName, isScene);
string[] deps = GetDependencies(assetBundleName);
foreach (string parent in parents)
{
if (!info.ContainsKey(parent))
......@@ -233,14 +235,14 @@ namespace ET
throw new Exception($"包有循环依赖,请重新标记: {assetBundleName} {dep}");
}
CollectDependencies(parents, dep, info, isScene);
CollectDependencies(parents, dep, info);
}
parents.RemoveAt(parents.Count - 1);
}
// 缓存包依赖,不用每次计算
public Dictionary<string, string[]> DependenciesCache = new Dictionary<string, string[]>();
private readonly Dictionary<string, string[]> DependenciesCache = new Dictionary<string, string[]>();
public bool Contains(string bundleName)
{
......@@ -286,6 +288,7 @@ namespace ET
{
await TimerComponent.Instance.WaitFrameAsync();
}
this.UnloadBundle(bundle, unload);
}
}
......@@ -325,7 +328,7 @@ namespace ET
{
return;
}
//Log.Debug($"---------------truly unload one bundle {assetBundleName} refcount: {abInfo.RefCount}");
this.bundles.Remove(assetBundleName);
this.resourceCache.Remove(assetBundleName);
......@@ -357,7 +360,7 @@ namespace ET
//Log.Debug($"-----------dep load finish {assetBundleName} dep: {dependencies.ToList().ListToString()}");
}
public void AddResource(string bundleName, string assetName, UnityEngine.Object resource)
private void AddResource(string bundleName, string assetName, UnityEngine.Object resource)
{
Dictionary<string, UnityEngine.Object> dict;
if (!this.resourceCache.TryGetValue(bundleName.BundleNameToLower(), out dict))
......@@ -379,7 +382,7 @@ namespace ET
//Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
return;
}
if (!Define.IsAsync)
{
string[] realPath = null;
......@@ -392,8 +395,6 @@ namespace ET
AddResource(assetBundleName, assetName, resource);
}
if (realPath.Length > 0)
{
abInfo = EntityFactory.CreateWithParent<ABInfo, string, AssetBundle>(this, assetBundleName, null);
......@@ -430,7 +431,7 @@ namespace ET
if (!assetBundle.isStreamedSceneAssetBundle)
{
// 异步load资源到内存cache住
UnityEngine.Object[] assets = assetBundle.LoadAllAssets();
var assets = assetBundle.LoadAllAssets();
foreach (UnityEngine.Object asset in assets)
{
AddResource(assetBundleName, asset.name, asset);
......@@ -439,42 +440,61 @@ namespace ET
abInfo = EntityFactory.CreateWithParent<ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
this.bundles[assetBundleName] = abInfo;
//Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
}
/// <summary>
/// 异步加载assetbundle
/// 异步加载assetbundle, 加载ab包分两部分,第一部分是从硬盘加载,第二部分加载all assets。两者不能同时并发
/// </summary>
/// <param name="assetBundleName"></param>
/// <param name="isScene"></param>
/// <returns></returns>
public async ETTask LoadBundleAsync(string assetBundleName, bool isScene = false)
public async ETTask LoadBundleAsync(string assetBundleName)
{
assetBundleName = assetBundleName.BundleNameToLower();
string[] dependencies = GetSortedDependencies(assetBundleName);
//Log.Debug($"-----------dep load async start {assetBundleName} dep: {dependencies.ToList().ListToString()}");
using var tasts = ListComponent<ETTask>.Create();
async ETTask loadDependency(string dependency)
using (var abInfos = ListComponent<ABInfo>.Create())
{
using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Resources, dependency.GetHashCode()))
async ETTask LoadDependency(string dependency, List<ABInfo> abInfosList)
{
await this.LoadOneBundleAsync(dependency, isScene);
using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Resources, dependency.GetHashCode()))
{
ABInfo abInfo = await this.LoadOneBundleAsync(dependency);
if (abInfo == null || abInfo.RefCount > 1)
{
return;
}
abInfosList.Add(abInfo);
}
}
}
foreach (string dependency in dependencies)
{
if (string.IsNullOrEmpty(dependency))
// LoadFromFileAsync部分可以并发加载
using (var tasks = ListComponent<ETTask>.Create())
{
continue;
foreach (string dependency in dependencies)
{
tasks.List.Add(LoadDependency(dependency, abInfos.List));
}
await ETTaskHelper.WaitAll(tasks.List);
}
// ab包从硬盘加载完成,可以再并发加载all assets
using (var tasks = ListComponent<ETTask>.Create())
{
foreach (ABInfo abInfo in abInfos.List)
{
tasks.List.Add(LoadOneBundleAllAssets(abInfo));
}
await ETTaskHelper.WaitAll(tasks.List);
}
tasts.List.Add(loadDependency(dependency));
}
await ETTaskHelper.WaitAll(tasts.List);
}
private async ETTask LoadOneBundleAsync(string assetBundleName, bool isScene)
private async ETTask<ABInfo> LoadOneBundleAsync(string assetBundleName)
{
assetBundleName = assetBundleName.BundleNameToLower();
ABInfo abInfo;
......@@ -482,65 +502,40 @@ namespace ET
{
++abInfo.RefCount;
//Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
return;
return null;
}
string p = "";
AssetBundle assetBundle = null;
if (!Define.IsAsync)
{
#if UNITY_EDITOR
if (isScene)
string[] realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
foreach (string s in realPath)
{
p = Path.Combine(Application.dataPath, "../SceneBundle/", assetBundleName);
if (File.Exists(p)) // 如果场景有预先打包
{
using (AssetsBundleLoaderAsync assetsBundleLoaderAsync = EntityFactory.CreateWithParent<AssetsBundleLoaderAsync>(this))
{
assetBundle = await assetsBundleLoaderAsync.LoadAsync(p);
}
string assetName = Path.GetFileNameWithoutExtension(s);
UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
AddResource(assetBundleName, assetName, resource);
}
if (assetBundle == null)
{
// 获取资源的时候会抛异常,这个地方不直接抛异常,因为有些地方需要Load之后判断是否Load成功
Log.Warning($"Scene bundle not found: {assetBundleName}");
return;
}
abInfo = EntityFactory.CreateWithParent<ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
this.bundles[assetBundleName] = abInfo;
}
if (realPath.Length > 0)
{
abInfo = EntityFactory.CreateWithParent<ABInfo, string, AssetBundle>(this, assetBundleName, null);
this.bundles[assetBundleName] = abInfo;
//Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
}
else
{
string[] realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
foreach (string s in realPath)
{
string assetName = Path.GetFileNameWithoutExtension(s);
UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
AddResource(assetBundleName, assetName, resource);
}
if (realPath.Length > 0)
{
abInfo = EntityFactory.CreateWithParent<ABInfo, string, AssetBundle>(this, assetBundleName, null);
this.bundles[assetBundleName] = abInfo;
//Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
}
else
{
Log.Error("Bundle not exist! BundleName: " + assetBundleName);
}
Log.Error("Bundle not exist! BundleName: " + assetBundleName);
}
// 编辑器模式也不能同步加载
await TimerComponent.Instance.WaitAsync(20);
await TimerComponent.Instance.WaitAsync(100);
#endif
return;
return abInfo;
}
p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName);
if (!File.Exists(p))
{
......@@ -550,40 +545,47 @@ namespace ET
if (!File.Exists(p))
{
Log.Error("Async load bundle not exist! BundleName : " + p);
return;
return null;
}
using (AssetsBundleLoaderAsync assetsBundleLoaderAsync = EntityFactory.CreateWithParent<AssetsBundleLoaderAsync>(this))
{
assetBundle = await assetsBundleLoaderAsync.LoadAsync(p);
}
assetBundle = await AssetBundleHelper.UnityLoadBundleAsync(p);
if (assetBundle == null)
{
// 获取资源的时候会抛异常,这个地方不直接抛异常,因为有些地方需要Load之后判断是否Load成功
Log.Warning($"assets bundle not found: {assetBundleName}");
return;
return null;
}
if (!assetBundle.isStreamedSceneAssetBundle)
abInfo = EntityFactory.CreateWithParent<ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
this.bundles[assetBundleName] = abInfo;
return abInfo;
//Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
}
// 加载ab包中的all assets
private async ETTask LoadOneBundleAllAssets(ABInfo abInfo)
{
using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Resources, abInfo.Name.GetHashCode()))
{
// 异步load资源到内存cache住
UnityEngine.Object[] assets;
using (AssetsLoaderAsync assetsLoaderAsync = EntityFactory.CreateWithParent<AssetsLoaderAsync, AssetBundle>(this, assetBundle))
if (abInfo.IsDisposed || abInfo.AlreadyLoadAssets)
{
assets = await assetsLoaderAsync.LoadAllAssetsAsync();
return;
}
foreach (UnityEngine.Object asset in assets)
if (abInfo.AssetBundle != null && !abInfo.AssetBundle.isStreamedSceneAssetBundle)
{
AddResource(assetBundleName, asset.name, asset);
// 异步load资源到内存cache住
var assets = await AssetBundleHelper.UnityLoadAssetAsync(abInfo.AssetBundle);
foreach (UnityEngine.Object asset in assets)
{
AddResource(abInfo.Name, asset.name, asset);
}
}
}
abInfo = EntityFactory.CreateWithParent<ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
this.bundles[assetBundleName] = abInfo;
//Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
abInfo.AlreadyLoadAssets = true;
}
}
public string DebugString()
......
......@@ -69,7 +69,6 @@
<Compile Include="Assets\Model\Core\Async\ETCancellationToken.cs" />
<Compile Include="Assets\Model\Core\Object\IAwakeSystem.cs" />
<Compile Include="Assets\Model\Module\Numeric\NumericType.cs" />
<Compile Include="Assets\Model\Module\Resource\AssetsLoaderAsync.cs" />
<Compile Include="Assets\Model\Core\Helper\EnumHelper.cs" />
<Compile Include="Assets\Model\Core\Object\BaseAttribute.cs" />
<Compile Include="Assets\Model\Generate\Config\StartZoneConfig.cs" />
......@@ -198,7 +197,6 @@
<Compile Include="Assets\Model\Module\Message\MessagePool.cs" />
<Compile Include="Assets\Model\Module\Config\IConfig.cs" />
<Compile Include="Assets\Model\Module\Message\ResponseTypeAttribute.cs" />
<Compile Include="Assets\Model\Module\Resource\AssetsBundleLoaderAsync.cs" />
<Compile Include="Assets\Model\Core\Object\ObjectPool.cs" />
<Compile Include="Assets\Model\Core\Async\ETTaskHelper.cs" />
<Compile Include="Assets\Model\Module\NetworkTCP\TChannel.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册