OnDemandResources.mm 3.0 KB

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