这是我的代码:
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()中访问对象.这在一个函数中运行良好,但我无法得到类变量的地址,我得到错误
错误CS0212您只能在固定语句初始值设定项中获取未固定表达式的地址
in line 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无法获取只读局部变量的地址
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;
注意双“**”.
你必须使用固定关键字做我在#1中提到的事情.
fixed (TrafficRoadSystem** addressOfTrafficRoadSystem = &trafficRoadSystem)
{
}
3.您可以将指针传递给traffic_import_osm函数的指针地址.
全新的Start功能:
void Start()
{
unsafe
{
fixed (TrafficRoadSystem** addressOfTrafficRoadSystem = &trafficRoadSystem)
{
string osmPath = "Assets/Resources/map.osm.pbf";
int results;
results = traffic_import_osm(osmPath, addressOfTrafficRoadSystem);
}
}
}
未来可能出现的相关问题:
我注意到你做了CarMove carMove = new CarMove();在您更新的问题中.不要在此处使用new关键字,因为CarMove继承自MonoBehaviour.有关如何操作,请参阅this答案.
2.我还注意到你使用了“Assets / Resources / map.osm.pbf”;.构建项目时,此路径无效.请考虑使用StreamingAssets文件夹而不是仅适用于Resources
API的Resources文件夹.您将Application.streamingAssetsPath与StreamingAssets文件夹一起使用.
原文地址:https://codeday.me/bug/20190527/1163164.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。