DllNotFound in Unity - c#

I am trying to use my C++ dll on Unity, so I copied it in Assets/Plugins and at the root of the project but I have DllFoundException when I use the Play button or run the .exe file generated by the build. It doesn't even work when I use the absolute path of the dll file in the DllImport.
However, it works fine when I Build&Run the project.
Unity Code
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using Dummiesman;
public class morph_face : MonoBehaviour
{
bool morphed;
[DllImport(#"facemorph", CallingConvention=CallingConvention.Cdecl)]
static extern void morphModelsPoints(string src_model, string src_csv,
string dst_csv, string output_path);
public GameObject model;
// Start is called before the first frame update
void Start()
{
morphed = false;
}
// Update is called once per frame
void Update()
{
if (!morphed && Input.GetKeyDown("space")) {
Debug.Log("SpaceBar pressed the model will be modified.");
morphModelsPoints("Data/src.obj", "Data/src.csv", "Data/dst.csv",
"Data/res.obj");
//disable old mesh
model.SetActive(false);
OBJLoader obj = new OBJLoader();
model = obj.Load("Data/res.obj");
//displays new mesh
model.SetActive(true);
morphed = true;
}
}
}
The Dll was built with this configuration: Release/Win32.
Here's the dll import settings :

※Please correct me if I am wrong
If I am not mistaken, you can not use 32 dlls in Editor because UNITY is 64bit. If you can just rebuild your dll to 64bit. If you build a standalone then you must set your architecture to x86 instead of x86_64 in Build Settings.

Related

Why is GUI.Label not working for me in unity?

I have tried both without OnGUI and with OnGUI and I can not really understand the errors they are giving.
I do not really understand what an error like
Internal build system error. BuildProgram exited with code -2146233088.
error: Build path contains project built with "Create Visual Studio Solution" option, which is incompatible with current build settings. Consider building your project into an empty directory.
System.Exception: Build path contains project built with "Create Visual Studio Solution" option, which is incompatible with current build settings. Consider building your project into an empty directory.
at WinPlayerBuildProgram.WinPlayerPrerequisiteChecks.CheckSafeProjectOverwrite(PlayerBuildConfig playerBuildConfig)
at WinPlayerBuildProgram.WinPlayerBuildProgram..ctor(String[] args)
at PlayerBuildProgramTypeWrapper.Run(String[] args)
at Program.Main(String[] args)
UnityEditor.BuildPlayerWindow:BuildPlayerAndRun ()
means and I do not know the problem with this code
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
public class SquareScript : MonoBehaviour
{
void Start()
{
transform.position = new Vector3(1, 0);
}
void Update()
{
transform.position = new Vector3((float)Math.Cos(Time.time), (float)Math.Sin(Time.time));
}
void OnGUI()
{
GUI.Label(new Rect(-0.5f, 0.5f, -0.5f, 0.5f), "lol");
}
}
And by the way it has nothing to do with the stuff in void Start() or void Update() since I tested that without the GUI stuff and it works fine.

Unity ECS doesn't show created entites since package Update

I tried to spawn some entities and assign a mesh and material to them. Somehow they do not appear, neither in the Editor nor the gameview. When I use a "GameObjectToEntity" Conversion Script the Entity does appear, I tried the new Alpha Version of the Editor (2020.1.0a25) but it didnt help. I'm also using the URP. It might be connected to the updating the preview packagets (Entities, Hybrid Render etc.) since I don't have the problem in a different project where I'm using older versions.
I might have missed something in my Code though.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Rendering;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.Collections;
public class EntityCreator : MonoBehaviour
{
[SerializeField] public Mesh theMesh;
[SerializeField] public Material theMaterial;
// Start is called before the first frame update
void Start()
{
EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
EntityArchetype eArch = entityManager.CreateArchetype(
typeof(RenderMesh),
typeof(Translation),
typeof(LocalToWorld),
typeof(MoveSpeedComponent)
);
NativeArray<Entity> eArray = new NativeArray<Entity>(10, Allocator.Temp);
entityManager.CreateEntity(eArch, eArray);
foreach (Entity ent in eArray)
{
entityManager.SetComponentData(ent, new Translation { Value = new Vector3(0f,0f, 0f) });
entityManager.SetSharedComponentData(ent, new RenderMesh
{
mesh = theMesh, material = theMaterial
});
}
eArray.Dispose();
}
}
Thanks a lot for taking the time
EntityArchetype eArch =
entityManager.CreateArchetype(
typeof(RenderMesh),
typeof(Translation),
typeof(LocalToWorld),
typeof(MoveSpeedComponent),
typeof(RenderBounds));

"Predefined type System.Void is not defined or imported" error trying to compile c# scripts for Unity 2D in Visual Studio 2017

I'm trying to learn creating 2D videogames with Unity but i can't compile my script for CharacterMovement because of several errors.
Even creating a new empty script, the compiler says that "Predefined type System.Void is not defined or imported" and i couldn't find online a way to fix this.
This is the empty script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyPlayerMovement : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
And this is the script i'm trying to compile :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public CharacterController2D controller;
public Animator animator;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
bool crouch = false;
// Update is called once per frame
void Update () {
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
if(horizontalMove == 0)
{
animator.SetBool("Jumping", false);
}
if (Input.GetButtonDown("Jump"))
{
jump = true;
animator.SetBool("Jumping", true);
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
animator.SetBool("Crouching", true);
} else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
animator.SetBool("Crouching", false);
}
}
void FixedUpdate ()
{
// Move our character
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
}
(On my script i get about 60 errors all similar)
change the option in File->BuildSettings->playersettings-api compatibility level to .Net 4X
This happened when I tried to install Microsoft.Toolkit.Uwp.Notification using Install-Package Microsoft.Toolkit.Uwp.Notifications -Version 7.1.2.
Once I do this, I notice a new config file is added to my solution explorer. I have to delete this to fix the error.
Another possible solution for those stumbling across this in the future: Go to Edit->Preferences->External Tools->Regenerate project files to bring back the original CS Project & VS solution files.
That was the only option that worked after trying other common answers like changing API compatibility, upgrading VS, or deleting the project obj folder. I think it broke like that due to me trying to clean out the solution manually and Unity ended up corrupting some packages after I attempted to delete them. So unfortunately it seems you will get this error if you try to do that.

Unity Controlling StatusBar on iPhone X

I have a game, in which all earlier versions of iPhone (3 - 8) is build without the Statusbar, but now with the iPhone X, after altering my UI to fit the iPhone X screen has 2 blank spots in the top.
Instead of leaving them blank, it would be nice to activate the statusbar to fill them out.
This means that I have to do this by script and in runtime.
I have looked at the UnityPlayer.PlayerSettings.statusBarHidden function, but this will not work :-/
I found this post, but am not able to make it work: https://answers.unity.com/questions/688922/hideshow-status-bar-androidios-on-runtime.html
Here is what I have tried:
Added this to plugins -> ios in my unity project:
StatusBar.mm
#import "StatusBar.h"
#implementation StatusBar
+ (void) hideStatusBar: (BOOL)hide
{
[[UIApplication sharedApplication] setStatusBarHidden: hide
withAnimation: UIStatusBarAnimationSlide];
[UnityGetGLViewController() setNeedsStatusBarAppearanceUpdate];
}
#end
extern "C"
{
void StatusBarSwitcherPressed(bool isHide)
{
[StatusBar hideStatusBar: isHide];
}
}
StatusBar.h
#import <Foundation/Foundation.h>
#interface StatusBar : NSObject
{
// Keeps track of available services
NSMutableArray *services;
// Keeps track of search status
NSString* status;
BOOL searching;
}
#end
I then added this c# file inside the plugin folder:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class StatusBarCtrl {
[DllImport ("__Internal")]
private static extern void StatusBarSwitcherPressed (bool isHide);
public static void StausBarHide(bool isHide)
{
// Call plugin only when running on real device
if (Application.platform != RuntimePlatform.OSXEditor)
StatusBarSwitcherPressed(isHide);
}
}
And then calls this when I have figured out if its an iPhone X or not:
StatusBarCtrl.StausBarHide (false);
Hope this makes sense. When running this, nothing happens whether I set it to true or false :-/
Any help is appreciated and thanks in advance :-)

How do I quit in standalone or in editor mode

I'm using unity 5.4.1f1 Personal on windows 10 64bit
I will explain all the steps i did and all the things i tried so far.
Firs time i created a new c# file with the original code:
Now when i press the Escape key it will go back to the editor but will not QUIT the game ! You can't stop the editor play button and quit the game when using [InitializeOnLoad]. To quit the game you can only do it with Build And Run in Standalone mode then you can make Application.Quit();
using UnityEditor;
using UnityEngine;
using System.Collections;
[InitializeOnLoad]
public class FullscreenPlayMode : MonoBehaviour {
//The size of the toolbar above the game view, excluding the OS border.
private static int tabHeight = 22;
static FullscreenPlayMode()
{
EditorApplication.playmodeStateChanged -= CheckPlayModeState;
EditorApplication.playmodeStateChanged += CheckPlayModeState;
EditorApplication.update += Update;
}
static void CheckPlayModeState()
{
if (EditorApplication.isPlaying)
{
FullScreenGameWindow();
}
else
{
CloseGameWindow();
}
}
static EditorWindow GetMainGameView(){
EditorApplication.ExecuteMenuItem("Window/Game");
System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
System.Reflection.MethodInfo GetMainGameView = T.GetMethod("GetMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
System.Object Res = GetMainGameView.Invoke(null,null);
return (EditorWindow)Res;
}
static void FullScreenGameWindow(){
EditorWindow gameView = GetMainGameView();
Rect newPos = new Rect(0, 0 - tabHeight, Screen.currentResolution.width, Screen.currentResolution.height + tabHeight);
gameView.position = newPos;
gameView.minSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height + tabHeight);
gameView.maxSize = gameView.minSize;
gameView.position = newPos;
}
static void CloseGameWindow(){
EditorWindow gameView = GetMainGameView();
gameView.Close();
}
static void Update()
{
if (Input.GetKey (KeyCode.Escape))
{
CloseGameWindow ();
}
}
}
Then since this is not working as i wanted i tried something else.
I deleted the line: [InitializeOnLoad] but didn't change anything else in the rest of the script.
This time after deleting the line [InitializeOnLoad] i dragged the script into the ThirdPersonController.
Now if i will start the game as before form the editor and hit press the escape key it will back to the editor but will not quit the game !
But now if i make in the menu File > Build & Run i will get two errors:
The first error is:
Assets/MyScripts/FullScreenPlayMode.cs(1,7): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?
The second error:
Error building Player because scripts had compiler errors
So i went to this solution what suppose to be a solution in this link:
Not working solution
If i move the file script to the EDITOR directory then i can't drag the script to the ThirdPersonController it will throw a message say the script is editor script.
If i tried the second solution in the link:
#if UNITY_EDITOR
// Editor specific code here
#endif
So i did it in the script in the top:
#if UNITY_EDITOR
using UnityEditor;
#endif
And the script file is not in the Editor directory !
This time when i make Build & Run i'm getting a new another error:
Assets/MyScripts/FullScreenPlayMode.cs(34,16): error CS0246: The type or namespace name `EditorWindow' could not be found. Are you missing a using directive or an assembly reference?
Could not find what is this error with the EditorWindow. This error happen when i used the #if and #endif
So in all the ways i tried so far i got another problem.
I might be simplifying your problem but you could simply do this:
void Update()
{
if (Input.GetKey(KeyCode.Escape))
{
#if UNITY_EDITOR
if (EditorApplication.isPlaying)
{
EditorApplication.isPlaying = false;
}
#else
Application.Quit();
#endif
}

Categories