Unity中使用Xcode制作的静态库Unity应用内存
知识记录
- 如何使用Xcode制作一个静态库
- Unity中如何调用Xcode的静态库
- 将Unity项目正确运行在手机
Xcode制作静态库
1. 新建Xcode工程Static Library
2. 修改静态库里面的内容
- 在MyStaticLibrary.h以及MyStaticLibray.m中实现获取iOS手机CPU使用率的方法。
// MyStaticLibrary.h
// MyStaticLibrary
#import <Foundation/Foundation.h>
@interface MyStaticLibrary : NSObject
+ (NSInteger) GetCPUTakenRate;
@end
// MyStaticLibrary.m
// MyStaticLibrary
//
#import "MyStaticLibray.h"
#import <mach/mach.h>
@implementation MyStaticLibray
+ (NSUInteger) GetCPUTakenRate {
thread_array_t thread_list;
mach_msg_type_number_t thread_count;
thread_basic_info_t basic_info_th;
float app_cpu_usage_rate = 0;
// get threads in the task
kern_return_t kr = task_threads(mach_task_self(), &thread_list, &thread_count);
if (kr != KERN_SUCCESS) {
return app_cpu_usage_rate;
}
thread_info_data_t thinfo;
mach_msg_type_number_t thread_info_count;
// for each thread
for (int idx = 0; idx < (int)thread_count; idx++) {
thread_info_count = THREAD_INFO_MAX;
kr = thread_info(thread_list[idx], THREAD_BASIC_INFO,
(thread_info_t)thinfo, &thread_info_count);
if (kr != KERN_SUCCESS) {
return app_cpu_usage_rate;
}
basic_info_th = (thread_basic_info_t)thinfo;
if (!(basic_info_th->flags & TH_FLAGS_IDLE)) {
app_cpu_usage_rate += basic_info_th->cpu_usage / (float)TH_USAGE_SCALE;
}
}
for(size_t index = 0; index < thread_count; index++)
mach_port_deallocate(mach_task_self(), thread_list[index]);
kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));
NSAssert(kr == KERN_SUCCESS,@"返回值不合法");
return (NSUInteger)(app_cpu_usage_rate*100);
}
@end
- 新建CPUUsageStaticLibrary.h和CPUUsageStaticLibrary.mm文件(其中.mm文件可以先新建.m文件,之后双击文件名即可改名),提供给Unity调用的接口。
// CPUUsageStaticLibrary.h
// MyStaticLibrary
#ifndef CPUUsageStaticLibrary_h
#define CPUUsageStaticLibrary_h
extern "C" {
int C_GetCPUUsage();
}
#endif /* CPUUsageStaticLibrary_h */
// CPUUsageStaticLibrary.m
// MyStaticLibrary
#import <Foundation/Foundation.h>
#import "CPUUsageStaticLibrary.h"
#import "MyStaticLibrary.h"
extern "C" {
int C_GetCPUUsage() {
NSInteger myCPUUsgae = [MyStaticLibrary GetCPUTakenRate];
NSLog(@"THE CPU USGAGE IS %d", myCPUUsgae);
return (int)myCPUUsgae;
}
}
3. 设置配置信息,编译Xcode项目
-
添加头文件至Headers、Copy Files
-
编译项目
Unity中使用静态库
1. 新建Unity项目
2. 创建Unity Scene与Scripts
- 新建MyScene场景以及MySceneScript脚本,并将脚本挂载于场景
- 修改MySceneScript脚本中内容
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class MySceneScript : MonoBehaviour
{
private string myCPUUsage;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
#if UNITY_IOS
[DllImport("__Internal")]
private static extern int C_GetCPUUsage();
#endif
void OnGUI()
{
GUI.Label(new Rect(500, 100, 400, 80), myCPUUsage);
if (GUI.Button(new Rect(100, 100, 300, 80), "<size=30>CPUUsage</size>"))
{
int usage = 0;
#if UNITY_IOS
usage = C_GetCPUUsage();
#endif
myCPUUsage = "<size=30>" + "The Usage is " + usage + "</size>";
}
}
}
-
拖动Xcode生成的libMyStaticLibrary和头文件CPUUsageStaticLibrary.h至新建的Plugins文件夹
-
Unity生成Xcode项目
3. 运行生成的Xcode项目
- 运行效果
点击即可看到Unity项目的CPU使用率: