Unity Controlling StatusBar on iPhone X - c#

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 :-)

Related

Getting an error in unity Assets/scripts/LearningHowToProgram.cs(7,13): error CS1001: Identifier expected

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LearningHowToProgram : MonoBehaviour
{
int c = 12 + 13;
sum_of_2_numbers(c);
void sum_of_2_numbers(int a)
{
print( a );
}
}
I was trying to get into game development and I learnt about functions recently and when I tried to use them I got an error saying this:
Assets/scripts/LearningHowToProgram.cs(7,13): error CS1001: Identifier expected
I don't know what triggered to show this but it was coming up on console in Unity.
The console error pretty clearly states the line the issue is coming from and your IDE will give you a hint what is wrong here.
This is fundamental to understanding your own code.
In this instance it is your line:
sum_of_2_numbers(c);
You cannot call methods like this from the body of you class.
In a Unity context you would call them from one of the Unity events like Start()
Therefore it is interpreted as you defining something which is incomplete.
Also as others have mentioned your method would not actually sum 2 number only print the value of c.
using UnityEngine;
public class LearningHowToProgram : MonoBehaviour
{
private void Start()
{
sum_of_2_numbers(12, 13);
}
private void sum_of_2_numbers(int a, int b)
{
int mySum = a + b;
print(mySum);
}
}
The name of your method sum_of_2_numbers also does not follow C# naming conventions and should probably be renamed to "SummarizeTwoNumbers"

Unity OnTriggerEnter2d() Not working | Trying to switch between Scenes

I'm pretty new to unity and I've been trying to get this scene switch to work however the trigger doesn't seem to be activating when the player hits it. The debug.Log is doing nothing so I'm stumped. I know my terminology may make no sense so let me show some pictures. If youre able to help it would be extremely helpful. Thank you!
This is the inspector panel for the object I want to trigger
This is the inspector the player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChangeScript : MonoBehaviour
{
public int iLevelToLoad;
public string sLevelToLoad;
public bool useIntegerToLoadLevel = false;
void start()
{
}
void update()
{
}
private void onTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
Debug.Log("Somethings Being Triggered!");
LoadScene();
}
}
void LoadScene()
{
if(useIntegerToLoadLevel)
{
SceneManager.LoadScene(iLevelToLoad);
}
else
{
SceneManager.LoadScene(sLevelToLoad);
}
}
}
Unity Monobehavior Lifecycle methods start with a capital and C# methods are case-sensitive. Therefore, the following methods need to be corrected to be used by Unity:
start => Start
update => Update
onTriggerEnter2D => OnTriggerEnter2D
Since the C# convention is that methods start with uppercase you'll less likely encounter this issue if you assume it starts with an uppercase letter rather than lower case. But it is better to confirm! Also if you are using Visual Studio, you can avoid much of these pains by investing the time to learn some shortcuts.
Hey I just spotted that you initiated the bool useIntegerToLoadLevel to be false and you are using this condition to load the scene in the if statement and the bool is never being set to true.
Try setting this bool to true when you enter the trigger, that is in the OnTriggerEnter2D method, should definitely work. It is a minor bug that anyone can make.

how to check if a virtual camera has fully transitioned or not in C#?

I'm working on a C#/Unity project, and I want to know when the cinemachine virtual camera has fully transitioned to another cinemachine camera. I've seen people using IsBlending, but I'm still not sure how to implement that code in my project.
My code is like..
private CinemachineBlend activeBlend = null;
public bool IsBlending { get { return activeBlend != null; } }
[SerializeField] private CinemachineVirtualCamera[] virtualCameras;
void TestItIsWorking()
{
virtualCameras[0].Priority = 10;
virtualCameras[1].Priority = 0; // -> this starts the blending the virtual camera
Debug.Log("this function is called"); // -> prints "this function is called"
Debug.Log(IsBlending); // -> prints false
}
So I guess I'm doing something wrong because event the virtual camera has started to change, I get a false value as of IsBlending boolean. I also wrote while loop instead of just Debug logging the IsBlending value, but the result was still the same. I want to run another function right after the blending has finished, so if there is a better solution for this, please also let me know....
Are you sure your activeBlend field getting a reference correctly? Using the IsBlending property of your CinemachineBrain is an option by the way.

DllNotFound in Unity

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.

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