Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Unity uses C++ as the game logic script analysis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "Unity uses C++ as game logic script analysis". In daily operation, I believe many people have doubts about Unity using C++ as game logic script analysis. Xiaobian consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "Unity uses C++ as game logic script analysis". Next, please follow the editor to study!

Communication between C# and C++

The interaction between C # and C++ is also very similar. C # can call C++ 's function directly through P/Invoke, while C++ calls the function of C #, and C++ 's function is encapsulated into DLL and put in the Plugins in the project file of Unity, it needs to be operated based on .NET, using Marshal.GetFunctionPointerForDelegate to get the function pointer, and then passing it to C++ for operation.

Second, realize real-time compilation and script update under the editor

In Unity, we can directly compile the file of c # in the open Unity, so that we do not need to close the project and open it every time, and C++ needs to close the project, update DLL, and then open the project every time C++ is called through DLL. This operation is extremely expensive for the development under the editor.

For the repeated switch project mentioned above to perform DLL updates, you can use the properties of [DllImport] to achieve the update under the editor.

This attribute is based on OS, so there is no cross-platform problem.

Third, sample code display

Show the code

C# code part:

Using System;using System.IO;using System.Runtime.InteropServices;using UnityEngine;class TestScript:MonoBehaviour {# if UNITY_EDITOR / / pointer handle to the C++ DLL public IntPtr libarayHandle; public delegate void InitDelegate (IntPtr gameObjectNew, IntPtr gameObjectGetTransform, IntPtr transformSetPosition); # endif} # if UNITY_EDITOR_OSX | | Import [DLLImport ("_ Internal")] public static extern IntPtr dlopen (string path, int flag) under UNITY_EDITOR_LINUX / / OSX and Linux [DllImport ("_ Internal")] public static extern IntPtr dlsym (IntPtr handle, string symbolName); [DllImport ("_ Internal")] public static extern int dlclose (IntPtr handle); public static IntPtr OpenLibrary (string path) {IntPtr handle = dlopen (path, 0); if (handle = = IntPtr.Zero) {throw new Exception ("Couldn't open native library:" + path);} return handle } public static void CloseLibrary (IntPtr libraryHandle) {dlclose (libraryHandle);} public static T GetDelegate (IntPtr libraryHandle, string functionName) where T: class {IntPtr symbol = dlsym (libraryHandle, functionName); if (symbol = = IntPtr.Zero) {throw new Exception ("Couldn't get function:" + functionName) } return Marshal.GetDelegateForFunctionPointer (symbol, typeof (T)) as T;} # elif UNITY_EDITOR_WIN / / win editor [DllImport ("kernel32")] public static extern IntPtr LoadLibrary (string path); [DllImport ("kernel32")] public static extern IntPtr GetProcAddress (IntPtr libraryHandle, string symbolName); [DllImport ("kernel32)] public static extern bool FreeLibrary (IntPtr libraryHandle) Public static IntPtr OpenLibrary (string path) {IntPtr handle = LoadLibrary (path); if (handle = = IntPtr.Zero) {throw new Exception ("Couldn't open native library:" + path);} return handle;} public static void CloseLibrary (IntPtr libraryHandle) {FreeLibrary (libraryHandle) } public static T GetDelegate (IntPtr libraryHandle, string functionName) where T: class {IntPtr symbol = GetProcAddress (libraryHandle, functionName); if (symbol = = IntPtr.Zero) {throw new Exception ("Couldn't get function:" + functionName);} return Marshal.GetDelegateForFunctionPointer (symbol, typeof (T)) as T } # else / / Local load [DllImport ("NativeScript")] static extern void Init (IntPtr gameObjectNew, IntPtr gameObjectGetTransform, IntPtr transformSetPosition); [DllImport ("NativeScript")] static extern void MonoBehaviourUpdate (); # endif delegate int GameObjectNewDelegate (); delegate int GameObjectGetTransformDelegate (int thisHandle); delegate void TransformSetPositionDelegate (int thisHandle, Vector3 position); # if UNITY_EDITOR_OSX const string LIB_PATH = "/ NativeScript.bundle/Contents/MacOS/NativeScript" # elif UNITY_EDITOR_LINUX const string LIB_PATH = "/ NativeScript.so"; # elif UNITY_EDITOR_WIN const string LIB_PATH = "/ NativeScript.dll"; # endif void Awake () {# if UNITY_EDITOR / / open the native library libraryHandle = OpenLibrary (Application.dataPath + LIB_PATH); InitDelegate Init = GetDelegate (libraryHandle, "Init"); MonoBehaviourUpdate = GetDelegate (libraryHandle, "MonoBehaviourUpdate"); # endif / / init the C++ Library ObjectStore.Init (1024) Init (Marshal.GetFunctionPointerForDelegate (new GameObjectNewDelegate (GameObjectNew)), Marshal.GetFunctionPointerForDelegate (new GameObjectGetTransformDelegate (GameObjectGetTransform)), Marshal.GetFunctionPointerForDelegate (new TransformSetPositionDelegate (TransformSetPosition));} void Update () {MonoBehaviourUpdate ();} void OnApplicationQuit () {# if UNITY_EDITOR CloseLibrary (libraryHandle); libraryHandle = IntPtr.Zero # endif} / / c # function for C++ call static int GameObjectNew () {GameObject go = new GameObject (); return ObjectStore.Store (go);} static int GameObjectGetTransform (int thisHandle) {GameObject go = (GameObject) ObjectStore.Get (thisHandle); Transform transform = go.transform; return ObjectStore.Store (transform) } static void TransformSetPosition (int handle, Vector3 position) {Transform t = (Transform) ObjectStore.Get (handle); t.position = position;}

C++ code part:

# ifdef _ WIN32 # define DLLEXPORT _ declspec (dllexport) # else # define DLLEXPORT#endifextern "C" {/ / C # VECTOR STRUCT struct Vector3 {float x; float y; float z;} / / c # function for C++ to call int (* GameObjectNew) (); int (* GameObjectGetTransform) (int thisHandle); void (* TransformSetPosition) (int thisHandle, Vector3 position); / / C++ functions for c # to call int numCreated DLLExport void Init (int (* gameObjectNew) (), int (* gameObjectGetTrasform) (int), void (* transformSetPosition) (int, Vector3)) {GameObjectNew = gameObjectNew; GameObjectGetTransform = gameObjectGetTransform; TransformSetPosition = trasformSetPosition; numCreated = 0;} / DLLEXPORT void MonoBehaviourUpdate (int thisHandle) {if (numCreated < 10) {/ / get the function handle, then operate int goHandle = GameObjectNew () Int transformHandle = GameObejctGetTransform (goHandle); float comp = 10.0f * (float) numCreated; Vector3 position = {comp, comp, comp}; TransformSetPosition (transformHandle, position); numCreated++;} this ends the study of "Unity uses C++ as a game logic script analysis". I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report