Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
老鱼er
et
提交
70a8c3d6
E
et
项目概览
老鱼er
/
et
与 Fork 源项目一致
从无法访问的项目Fork
通知
10
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
et
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
70a8c3d6
编写于
6月 22, 2017
作者:
T
tanghai
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
资源管理类应该不需要热更
上级
321d68bc
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
162 addition
and
8 deletion
+162
-8
Unity/Assets/Scripts/Component/ResourcesComponent.cs
Unity/Assets/Scripts/Component/ResourcesComponent.cs
+145
-0
Unity/Assets/Scripts/Component/ResourcesComponent.cs.meta
Unity/Assets/Scripts/Component/ResourcesComponent.cs.meta
+12
-0
Unity/Unity.csproj
Unity/Unity.csproj
+5
-8
未找到文件。
Unity/Assets/Scripts/Component/ResourcesComponent.cs
0 → 100644
浏览文件 @
70a8c3d6
using
System
;
using
System.Collections.Generic
;
using
System.Threading.Tasks
;
using
UnityEngine
;
namespace
Model
{
[
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
;
TimerComponent
timerComponent
=
Game
.
Scene
.
GetComponent
<
TimerComponent
>();
while
(
true
)
{
WWWAsync
wwwAsync
=
null
;
try
{
++
count
;
if
(
count
>
1
)
{
await
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
Unity/Assets/Scripts/Component/ResourcesComponent.cs.meta
0 → 100644
浏览文件 @
70a8c3d6
fileFormatVersion: 2
guid: 509cf7df1ed2a52438a824cb162c208a
timeCreated: 1498124686
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Unity/Unity.csproj
浏览文件 @
70a8c3d6
...
...
@@ -12,15 +12,12 @@
<ProjectTypeGuids>
{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
</ProjectTypeGuids>
<TargetFrameworkIdentifier>
.NETFramework
</TargetFrameworkIdentifier>
<TargetFrameworkVersion>
v4.6
</TargetFrameworkVersion>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<CompilerResponseFile>
</CompilerResponseFile>
<TargetFrameworkProfile></TargetFrameworkProfile>
<CompilerResponseFile></CompilerResponseFile>
<UnityProjectType>
Game:1
</UnityProjectType>
<UnityBuildTarget>
StandaloneWindows64:19
</UnityBuildTarget>
<UnityVersion>
2017.1.0b8
</UnityVersion>
<RootNamespace>
</RootNamespace>
<RootNamespace></RootNamespace>
<LangVersion>
6
</LangVersion>
</PropertyGroup>
<PropertyGroup
Condition=
" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "
>
...
...
@@ -377,6 +374,7 @@
<Compile
Include=
"Assets\Scripts\Component\NetInnerComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\NetOuterComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\NetworkComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\ResourcesComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\TimerComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Config\ACategory.cs"
/>
<Compile
Include=
"Assets\Scripts\Config\AConfig.cs"
/>
...
...
@@ -440,7 +438,6 @@
<Compile
Include=
"Assets\Scripts\Object\ObjectEvents.cs"
/>
<Compile
Include=
"Assets\Scripts\ReferenceCollector.cs"
/>
</ItemGroup>
<ItemGroup
/>
<Import
Project=
"$(MSBuildToolsPath)\Microsoft.CSharp.targets"
/>
<Target
Name=
"GenerateTargetFrameworkMonikerAttribute"
/>
</Project>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录