OnDemandResources.mm
3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#import "Foundation/Foundation.h"
#if ENABLE_IOS_ON_DEMAND_RESOURCES
#import "Foundation/NSBundle.h"
#endif
typedef void (*OnDemandResourcesRequestCompleteHandler)(void* handlerData, const char* error);
#if ENABLE_IOS_ON_DEMAND_RESOURCES
struct OnDemandResourcesRequestData
{
NSBundleResourceRequest* request;
};
extern "C" OnDemandResourcesRequestData* UnityOnDemandResourcesCreateRequest(NSSet * tags, OnDemandResourcesRequestCompleteHandler handler, void* handlerData)
{
OnDemandResourcesRequestData* data = new OnDemandResourcesRequestData();
data->request = [[NSBundleResourceRequest alloc] initWithTags: tags];
[data->request beginAccessingResourcesWithCompletionHandler:^(NSError* error) {
dispatch_async(dispatch_get_main_queue(), ^{
const char* errorMessage = error ? [[error localizedDescription] UTF8String] : NULL;
handler(handlerData, errorMessage);
});
}];
return data;
}
extern "C" void UnityOnDemandResourcesRelease(OnDemandResourcesRequestData* data)
{
[data->request endAccessingResources];
delete data;
}
extern "C" float UnityOnDemandResourcesGetProgress(OnDemandResourcesRequestData* data)
{
return data->request.progress.fractionCompleted;
}
extern "C" float UnityOnDemandResourcesGetLoadingPriority(OnDemandResourcesRequestData* data)
{
float priority = (float)data->request.loadingPriority;
return priority;
}
extern "C" void UnityOnDemandResourcesSetLoadingPriority(OnDemandResourcesRequestData* data, float priority)
{
if (priority < 0.0f)
priority = 0.0f;
if (priority > 1.0f)
data->request.loadingPriority = NSBundleResourceRequestLoadingPriorityUrgent;
else
data->request.loadingPriority = (double)priority;
}
extern "C" NSString* UnityOnDemandResourcesGetResourcePath(OnDemandResourcesRequestData * data, const char* resource)
{
NSString* path = [[data->request bundle] pathForResource: [NSString stringWithUTF8String: resource] ofType: nil];
return path;
}
#else // ENABLE_IOS_ON_DEMAND_RESOURCES
struct OnDemandResourcesRequestData
{
};
extern "C" OnDemandResourcesRequestData* UnityOnDemandResourcesCreateRequest(NSSet * tags, OnDemandResourcesRequestCompleteHandler handler, void* handlerData)
{
OnDemandResourcesRequestData* data = new OnDemandResourcesRequestData();
if (handler)
handler(handlerData, NULL);
return data;
}
extern "C" void UnityOnDemandResourcesRelease(OnDemandResourcesRequestData* data)
{
delete data;
}
extern "C" float UnityOnDemandResourcesGetProgress(OnDemandResourcesRequestData* data)
{
return 1.0f;
}
extern "C" float UnityOnDemandResourcesGetLoadingPriority(OnDemandResourcesRequestData* data)
{
return 0.5;
}
extern "C" void UnityOnDemandResourcesSetLoadingPriority(OnDemandResourcesRequestData* data, float priority)
{
}
extern "C" NSString* UnityOnDemandResourcesGetResourcePath(OnDemandResourcesRequestData * data, const char* resource)
{
return [NSString stringWithUTF8String: resource];
}
#endif // ENABLE_IOS_ON_DEMAND_RESOURCES