Category Archives: Unity

Fix for Android SDK Errors in Unity on Linux

When I add Android support for Unity on Linux as an additional module in Unity Hub, I find that Unity is unable to the SDK.

The problem appears to be that Unity does not set the binaries included as executable after extracting. I believe this may be fixed on newer versions of the Editor, but this is an issue for me on 2020.3.28f1.

The simple fix is to make all files in the AndroidPlayer executable. If Unity is open, close it.

Open Konsole or another terminal app.

cd ~/Unity/Hub/Editor/[Unity version]/Editor/Data/PlaybackEngines/AndroidPlayer
find . -type f -exec chmod +x {} \;

When you re-launch Unity, it should have no problem using the Android SDK.

VS Code Namespace Not Found With New Unity Input System

If you’re switching to the new Unity Input System and using Visual Studio code, you’ll probably hit the hiccup I did.

After switching back-ends, you’ll find that you’re not able to use the new namespace. If you try, you’ll get “namespace not found.”

using UnityEngine.InputSystem;

To fix this, you’ll simply need to create the csproj file so VS Code knows to use the new assembly.

In Unity Editor go to Edit->Preferences
Choose “External Tools”
Click the “Regenerate project files” button

After that, VS Code should recognize the new UnityEngine.InputSystem namespace!

Unity3d On Fedora

I recently switched from Ubuntu to Fedora and realized that Omnisharp in VS Code for Unity3D projects was not working correctly, even with Use Global Mono set to Always.

The fix ended up being simple: even though they are the same version, install Mono from the mono project’s repository, not Fedora’s.

First: Install mono from here:
https://www.mono-project.com/download/stable/#download-lin

Then, open VS Code and go to settings. To make this change for all projects, click User. To change for just this project (which I’d recommend) click Workspace. This will allow you to do your regular .Net development using the built-in mono (and eventually .Net Core).

Set Omnisharp: Use Global Mono to “always.”

That’s it! That should do the trick.

SSLStream Failure with .Net 4.x in Unity 2018.1

When Unity 2018.1 was released I jumped (perhaps too quickly) to using “.Net 4.x Equivalent” for the scripting runtime version under “Build Settings->Player Settings.”

Everything ran fine in the Unity Editor, but socket reads would silently fail after a few seconds on iOS.

I use SSLStream and SSLStream.Read() in a while loop running under its own thread. This runs without any issue using IL2CPP under the .Net 3.5 runtime, but fails almost immediately under iOS using 4.x.

I’m not sure what the exact problem is, but a forum post on unity.com seems to indicate that TLS related functions aren’t going to be fully ready using the .Net 4.x runtime and IL2CPP until 2018.2:
https://forum.unity.com/threads/questions-around-tls-support-on-2018-1.524917/

Either way, if you come across this issue, simply reverting back to .Net 3.5 seems to resolve the problem.

Create 1 Frame Animations in Unity 3D

I was looking to make a 1 frame animation in Unity and wasn’t able to find a guide. In case anyone is curious, this is what I did:

When making a multi-frame animation, you can simply select all of the frames in a sprite and drag them to the prefab. This creates the animation for you. However, if you drag just one frame from the sprite, it will add it as a sprite renderer.

If you would like  a 1 frame animation from that sprite, right click the frame you want in the Unity Editor and click Create->Animation

Ta da! You’ve now got a 1 frame animation.

New UI Prefab Scaling Solution

I recently changed the settings on my UI Canvas from “Screen Space – Overlay” to “Screen Space – Camera”. Which is how I’m sure it was supposed to be done in the first place (n00b here).

Anyway, in doing so, all of my UI prefabs were coming in scaled to 53.3333%.

I did a quick hack using a script to set those back to 1, but I figured there must be an elegant solution.

I finally found the (so, so simple) solution in the Unity forums.

When instantiating a prefab into the UI, you need to add the “false” argument when setting the parent.

Use

GameObject obj = (GameObject)Instantiate (prefab);
obj.transform.SetParent (parent.transform, false);

 

NOT

GameObject obj = (GameObject)Instantiate (prefab);
obj.transform.SetParent (parent.transform);

Deleting Child Objects Recursively in Unity

It took me a bit of Binging to find a method that worked the way I needed.

1st, create a public GameObject to hold the parent of the items you are looking to delete. Set this either from the Unity Editor or in code.

Then simply call a foreach loop on the parent GameObject as below to delete all children.

foreach (Transform child in searchResultParent.transform)
{
     Destroy(child.gameObject);
}

SSL at Last

It’s long over due, but I finally got an SSL certificate for shernet.com! Was happy to find (sorry for the advertisement) a Comodo Wildcard SSL cert on SSL2Buy for only $60. Was afraid it might be too good to be true, but everything went great, including testing a certificate reissue.

Unity engine will finally connect to my web server without yelling at me about an bad certificate. What a happy day 🙂

To Unity and Beyond

It’s been a while since I got to sit down and write some code. There’s one project that’s been on my mind for years, but it’s languished for some time. I decided to give it another go, this time using Unity instead of HTML5.

Since this project is probably going to take quite a while, and I’m not really expecting much from it in a return on investment point of view, I’m using the free Unity. Free Unity, as you probably know, lacks .Net sockets for mobile platforms. Since I’d really like to get it working on iOS, and don’t have $3000 to spend on Unity Pro and iOS Pro, I’m going to stick with the old GET/POST method I started with back when I was using HTML5.

To communicate with my server then, I’ve been using the WWW class in Unity.
WWW www = new WWW ("http://server/function.php", pData, headers);

However, because WWW runs as an Enumerator and requires
yield return www
I was having all sorts of trouble getting the result from the WWW GET.

What seems to be working for was to put the WWW command in its own Unity C# script, use evil global variables, and run it by using:
gameObject.AddComponent("Login");

I’m still hoping to find a way to pass parameters to the added component, rather than using global variables, but I feels good to finally start passing some sanity checks!