Mac*_*ysy 4 c# dll dllimport unity-game-engine
这是我的代码:
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()
{
}
}
Run Code Online (Sandbox Code Playgroud)
这是来自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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到错误CS0459 Cannot take the address of a read-only local variable
的行
results = traffic_import_osm(osmPath, &ptr);
在Unity中制作C或C++插件需要对这些语言有广泛的了解.这意味着在尝试在Unity中使用原始指针之前,您应该花时间学习指针.因为即使你编译它,你也可能遇到很难修复崩溃.
你有:
unsafe TrafficRoadSystem* trafficRoadSystem;
Run Code Online (Sandbox Code Playgroud)
并希望将其传递给下面的函数:
public unsafe static extern int traffic_import_osm(..., TrafficRoadSystem** out_road_system);
Run Code Online (Sandbox Code Playgroud)
1个 .The trafficRoadSystem
变量是一个指针,你将需要另一个指针指向trafficRoadSystem
.这被称为"指针指针"
TrafficRoadSystem** addressOfTrafficRoadSystem = &trafficRoadSystem;
Run Code Online (Sandbox Code Playgroud)
注意双" **
".
2,你必须使用fixed
关键字来做我在#1中提到的事情.
fixed (TrafficRoadSystem** addressOfTrafficRoadSystem = &trafficRoadSystem)
{
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
未来可能出现的相关问题:
1,我注意到你CarMove carMove = new CarMove();
在更新的问题中做了.不要在new
这里使用关键字,因为CarMove
继承自MonoBehaviour
.请参阅此答案以了解该怎么做.
2,我也注意到你用过了"Assets/Resources/map.osm.pbf";
.构建项目时,此路径无效.考虑使用StreamingAssets文件夹而不是仅适用于Resources
API 的Resources文件夹.您Application.streamingAssetsPath
与StreamingAssets文件夹一起使用.
归档时间: |
|
查看次数: |
1579 次 |
最近记录: |