这是我的代码:
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using System;
using System.IO;
public class CarMove : MonoBehaviour
{
public unsafe struct TrafficRoadSystem { };
public unsafe struct CarsSimulation { };
public unsafe struct CarPostion
{
double position_x;
double position_y;
double position_z;
};
// Use this for initialization
[DllImport("Assets/traffic.dll", CharSet = CharSet.Ansi)]
public unsafe static extern int traffic_import_osm([MarshalAs(UnmanagedType.LPStr)] string osm_pbf_path, TrafficRoadSystem** out_road_system);
[DllImport("Assets/traffic.dll")]
public unsafe static extern int create_simulation(TrafficRoadSystem* road_system, CarsSimulation** out_simulation, double speed, int cars_count);
[DllImport("Assets/traffic.dll")]
public static extern void update_simulation(ref CarsSimulation simulation, double time);
[DllImport("Assets/traffic.dll")]
public static extern CarPostion get_car_postion(ref CarsSimulation simulation, int car_number);
unsafe CarsSimulation* carSimulation;
unsafe TrafficRoadSystem* trafficRoadSystem;
void Start()
{
unsafe
{
string osmPath = "Assets/Resources/map.osm.pbf";
int results;
results = traffic_import_osm(osmPath, &trafficRoadSystem);
}
}
// Update is called once per frame
void Update()
{
}
}
这是从dll C库。函数
int traffic_import_osm()
可作为引用的TrafficRoadSystem* trafficRoadSystem;
对象使用,我想访问void Update()
中的对象。这可以在一个函数中很好地工作,但是我无法获取类变量的地址并且出现错误Error CS0212 You can only take the address of an unfixed expression inside of a fixed statement initializer
在
results = traffic_import_osm(osmPath, &trafficRoadSystem);
行中我尝试使用此解决方案
https://msdn.microsoft.com/en-us/library/29ak9b70(v=vs.90).aspx
我写了这个:
TrafficRoadSystem trafficRoadSystem;
void Start()
{
unsafe
{
string osmPath = "Assets/Resources/map.osm.pbf";
CarMove carMove = new CarMove();
int results;
fixed( TrafficRoadSystem* ptr = &carMove.trafficRoadSystem)
{
results = traffic_import_osm(osmPath, &ptr);
}
}
}
我在一行中收到错误
CS0459 Cannot take the address of a read-only local variable
results = traffic_import_osm(osmPath, &ptr);
最佳答案
在Unity中制作C或C++插件需要对这些语言有广泛的了解。这意味着您应该花时间学习指针,然后再尝试在Unity中使用原始指针。因为即使您可以编译它,也可能会遇到难以修复的崩溃的麻烦。
你有:
unsafe TrafficRoadSystem* trafficRoadSystem;
并希望将其传递给以下功能:
public unsafe static extern int traffic_import_osm(..., TrafficRoadSystem** out_road_system);
1 。
trafficRoadSystem
变量是一个指针,您将需要创建另一个指向trafficRoadSystem
的指针。这称为“指向指针的指针”TrafficRoadSystem** addressOfTrafficRoadSystem = &trafficRoadSystem;
注意双“
**
”。2 。您必须使用
fixed
关键字来执行我在#1中提到的内容。fixed (TrafficRoadSystem** addressOfTrafficRoadSystem = &trafficRoadSystem)
{
}
3 。您可以将该指针传递给
traffic_import_osm
函数的指针地址。全新的启动功能:
void Start()
{
unsafe
{
fixed (TrafficRoadSystem** addressOfTrafficRoadSystem = &trafficRoadSystem)
{
string osmPath = "Assets/Resources/map.osm.pbf";
int results;
results = traffic_import_osm(osmPath, addressOfTrafficRoadSystem);
}
}
}
不相关的将来可能出现的问题:
1 。我注意到您在更新的问题中做了
CarMove carMove = new CarMove();
。此处不要使用new
关键字,因为CarMove
继承自MonoBehaviour
。请参阅this答案以进行操作。2 。我还注意到您使用了
"Assets/Resources/map.osm.pbf";
。当您构建项目时,此路径将无效。考虑使用StreamingAssets文件夹,而不是仅适用于 Resources
API的Resources文件夹。您将Application.streamingAssetsPath
与StreamingAssets文件夹一起使用。关于c# - 获取结构的地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46707234/