Posts Tagged ‘unity’

Printing Unity output to console cout instead of to a log file

Tuesday, October 25th, 2016

When invoking Unity from the command line it will by default not print its output to the console.
You can make it log its output to a log file by using the -logFile unityBuild.log parameter, but if you want it to print its output instead of saving it you can simply omit the path after the -logfile parameter:

Unity.exe -batchmode -quit -logFile

Unity: Get UI element under mouse

Wednesday, March 30th, 2016

To check if the mouse is over any UI element you can use

EventSystem.current.IsPointerOverGameObject()

In case that function isn’t acting to your liking and you need to debug, or if you just want to know what object is under the mouse, you can use

PointerEventData pointerData = new PointerEventData(EventSystem.current) {
	position = Input.mousePosition
};

List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerData, results);

results.ForEach((result) => {
	Debug.Log(result);
});

Remove Boo and UnityScript references from Unity projects.

Tuesday, October 6th, 2015

To stop Resharper in Visual Studio suggesting Boo.Lang when declaring List<>’s and other types common to both libraries, simply put this script in your editor folder.

Original source: https://gist.github.com/jbevain/a982cc580fb796c93e4e

using UnityEditor;
using SyntaxTree.VisualStudio.Unity.Bridge;
 
[InitializeOnLoad]
public class ReferenceRemovalProjectHook
{
	static ReferenceRemovalProjectHook()
	{
		const string references = "\r\n    <Reference Include=\"Boo.Lang\" />\r\n    <Reference Include=\"UnityScript.Lang\" />";

		ProjectFilesGenerator.ProjectFileGeneration += (string name, string content) =>
			content.Replace(references, "");
	}
}

Unity: Is mouse (or any coordinates) within UI element’s rect

Monday, March 30th, 2015

Here’s a quick way to check if the mouse or any other coordinates are within any UI object’s boundaries.

This method doesn’t use raycast so it ignores all overlapping objects, it works just like ActionScript’s object.HitTest(coords) function.

public bool AreCoordsWithinUiObject(Vector2 coords, GameObject gameObj)
{
	Vector2 localPos = gameObj.transform.InverseTransformPoint(coords);
	return ((RectTransform) gameObj.transform).rect.Contains(localPos);
}

// Example usage
bool isMouseOverIcon = AreCoordsWithinGameObject(Input.mousePosition, _myUiIcon);

Unity: Getting screen size in units (2D)

Wednesday, January 14th, 2015

When doing 2D games in Unity it is often handy to know where the borders of the screen are in units so one can for example spawn units on the edge of the screen or detect when something leaves it.

Fortunately it’s easy to figure out when using orthographic camera.

Camera.main.orthographicSize is always half of the total screen height, with the width changing with the aspect ratio. You can get the top or bottom coordinates by adding/subtracting it to/from the camera’s y position.

For the width you can then multiply the aspect ratio with the orthographic size,
camera.orthographicSize * Screen.width/Screen.height , to get half of the total screen width.

You can then get the x position of the screen’s left border like this:

float leftSideOfScreen = Camera.main.transform.position.x - Camera.main.orthographicSize * Screen.width / Screen.height;

Unity: Networking errors when changing scenes.

Tuesday, November 18th, 2014

I had a really simple project set up that had two scenes, “Init” which I use to connect to server, and “Main”, where all my synced objects using networkViews are.
When the player had connected to the server I’d change to “Main” where I wanted all the objects already on the server to automatically show up.
However every time I ran the project I got these three errors:

ms_IDToPointer->find (obj->GetInstanceID ()) == ms_IDToPointer->end ()
View ID AllocatedID: 1 not found during lookup. Strange behaviour may occur
Received state update for view id' AllocatedID: 1' but the NetworkView doesn't exist

What was happening was that the server was sending us the objects before we had a chance to load “Main”, which was weird because I was using “Network.isMessageQueueRunning = false;” before connecting to the server and turning it back on when “Main” was loaded.

Fortunately this has a simple fix, apparently you have to set isMessageQueueRunning AFTER calling Network.Connect(..). I moved it to “void OnConnectedToServer()” and everything worked fine.

Init.unity

void Start()
{
	// Don't set isMessageQueueRunning here
	Network.Connect(hostData);
}

void OnConnectedToServer()
{
	Network.isMessageQueueRunning = false;
	Application.LoadLevel("Main");
}

Main.unity

void Start()
{
	Network.isMessageQueueRunning = true;
}