uQuery - jQuery for Unity
Hello (web) developers!
Here’s my weekend project: jQuery to Unity.
If you’re a ninja with jQuery and want to get familiar with Unity coding, this is the tool for you!
It’s still a subset of jQuery, but I believe it will help developers like me ![]()
Source code
Source can be found here:
https://github.com/jehna/uQuery
Selectors:
Selectors help you grab the Unity’s object you need.
Current behavior of selectors are:
// Select objects by tag:
uQuery("MainCamera"); // Returns all objects tagged "MainCamera"
// Select objects by class:
uQuery(".NewBehaviourScript"); // Returns every script named "NewBehaviourScript" (class = name of your script)
// Select object by identifier
uQuery("#Cube"); // Returns first GameObject, that you have named "Cube"
Manipulation
You can use .each to manipulate all the objects you have selected.
Note: Variable “this” behaves different than in normal JavaScript, so you’ll need to assign a helper variable:
(in the examples I’m using var _this as equivalent to this)
// Print all names of GameObjects tagged as "Enemy"
uQuery("Enemy").each(function(_this) {
// Now we have current object in variable "_this"
var myName = uQuery(_this)[0].gameObject.name; // Get name of current object
Debug.Log(myName);
});
// You can typecast objects to direct manipulation of attributes.
// eg. manipulation of GUITexture
uQuery(".GUITexture").each(function(_this : GUITexture) {
_this.pixelInset.y = 10;
});
You can get and set object’s properties with .attr() function!
// Get enemy's health
Debug.Log( uQuery(".Enemy").attr("health") );
// Get vertical position of the object
var vertical = "y";
Debug.Log( uQuery(transform).attr(vertical, "localPosition") );
// Set all MeshRenderers off
uQuery(".MeshRenderer").attr("enabled", false);
NEW! AJAX
Now you can use the jQuery’s famous .get, .post and $.ajax functions!
Effortless requests with couple lines of code:
// Get a simple string data from the Internet
uQuery.Get("http://pastebin.com/raw.php?i=gyYybxa8", function(data, xhr) {
Debug.Log(data);
});
// Grab a kitten image and pass it as a texture
uQuery.Get("http://placekitten.com/400/301", function(data, xhr : uQueryXHR) {
uQuery(".GUITexture")[0].texture = xhr.www.texture;
});
// Perform a POST request to a local server
uQuery.post("http://localhost/test.php", {"test" : "data"}, function(data, xhr) {
Debug.Log(data);
});
Supported functions
You can use .show(), .hide() to toggle visibility of current object.
You can use .fadeIn() and .fadeOut() to easy animating your objects.
All class-manipulation functions are also supported (.hasClass(), .addClass(), .removeClass() and .toggleClass()).
// Hide and show camera
uQuery("MainCamera").hide();
yield WaitForSeconds(3);
uQuery("MainCamera").show();
// Fade all GUITextures out and in
uQuery(".GUITexture").fadeOut();
uQuery(".GUITexture").fadeIn();
// Check if camera doesn't have script named "CameraMover" and add it if necessary
if(!uQuery("MainCamera").hasClass("CameraMover")) {
uQuery("MainCamera").addClass("CameraMover");
}
Events
Basically, you can use .bind() to set triggers to different gameObjetcts in Unity, like you would do in jQuery. You can even set your own custom triggers that you can fire using .trigger().
Shorthands for commonly-used bindings are:
.click()
.mousedown()
.mouseup()
.mouseenter()
.mouseleave()
.mouseout()
.mouseover()
// Print out when any GUITexture was clicked
uQuery(".GUITexture").click(function() {
Debug.Log("Clicked GUITexture");
});
// Print out gameobject's name when any of it's childrens are clicked
uQuery(".Collider", this).click(function() {
Debug.Log("Clicked: " + gameObject.name);
});
// Create a custom trigger that is fired when time has passed 2 seconds
uQuery(this).bind("twoseconds", function() {
Debug.Log("Two seconds has passed");
});
yield WaitForSeconds(2.0);
uQuery(this).trigger("twoseconds");
Let me know what you think. ![]()