Weekend project: jQuery for Unity - uQuery

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

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. :slight_smile:

2 Likes

Sounds good!

Might be very nice if could do those fade/animate/slide and other effects on objects (gui objects too)… (with basically just 1 line of code…)

Hi mgear

I’m working this weekend with the animations, so you’ll soon be happy to find at least .fadeOut() and .fadeIn() working… :slight_smile:

Now it’s working!
You can use .fadeIn() and .fadeOut() to animate GUI objects and textures (that support transparency).

There’s also a base to general animating, which I plan to extend to support object moving etc…

Let me tell if you have more great feature requests :slight_smile:

Getting and setting object’s attributes (properties) is now possible with uQuery’s .arrt() command! :sunglasses:

Now just needs .click() and maybe .mouseOver(), then can start building menus with ease :slight_smile:

Hi mgear

I’m glad to announce that your wishes are fulfilled. uQuery now supports .click(), .mouseover() and many other bindings that are familiar from the traditional jQuery :slight_smile:

Some problem with mouseout/mouseleave? Couldnt get it to trigger on guitexture… (mouseenter and click works fine)

Hello,

right now i’m working on an app using the Vuforia AR SDK. I have to animate the visibility of models because i have one model for every frame. Just like you would get from a fluid animation you import from Realflow. It works nicely in Maya and Cinema 4d but Unity won’t read the visibility i adjusted in other 3d-software. So i will have to do it in Unity3d. I’m not much of a coder though but i guess one could write an easy script for it. I even would do it manually once, to check out if it runs on an android. Do you have any ideas how i could solve this?

https://dl.dropboxusercontent.com/u/...013_121102.png this ist my project. All models “frames” are visible at the same time so it looks like a strange polygon mayhem. The meshes in the parent directory “test 2 von f 0” on the left side > ofmensh, ofmesh1, ofmesh2… should be just visible in one frame according to their name. Can someone help me?

Thanks in advance for your help.

Greetings from Germany,

Gregor

^ That link is broken…

Hi @mgear

Revived this old project and fixed the mouseout/mouseleave bug.
https://github.com/jehna/uQuery/issues/2

Whoa! Managed to add .get(), .post() and .ajax() functions! :sunglasses:

very cool, thanks a lot for this!

Really good work…

Can i use the Ajax functions in C#? if i can, how i can use this? in C# can’t use functions in params :frowning:

Unfortunately no, since this port works only with JScript.net, since, as you stated, C# does not allow similar syntax to regular jQuery code.

In fact, you could use delegates as anonymous functions, but the core behaviour is so different from JScrip.net’s one that the two are not any way compatible with each other.

Hi everyone i need your help,

i am willing to create a page flip for an interactive book i am making, i was up to imitate Turn.js effects, i am using c# to code this project, thats why jQuery never crossed my mind to integrate then i said it would be great if there are some API, and boom i found it ^^.

Well now reading your comments untill 2014 i think it is not possible to get it work properly with no Ajax support in C# etc…
so i am checking with you guys:

  1. did anyone make it work with C# correctly (something like Page flip in Turns.js) ??
    2)Is there anyway to do so, or to make such effect (i dont want animation i want the reader to feel like it was a real book) [free solution am broke :p]

thank you :slight_smile:

This is awesome! Keep up the good work (if you’re still on it) :slight_smile: