deal with Unity 5 UI in augmented reality app - user-interface

I'm trying to make an augmented reality application with vuforia and unity.
whenever it recognize the image target, it must tell a story by showing text , and it should enable the user to press next and back to go on reading the different parts of this story, I'm totally new to unity and don't know how to handle with UI throughout scripting, I need some help on how to accomplish the part of "going forward and backward on showing the story by hitting Next and Back buttons", and all these parts of story should be related to the same image target in the same scene.
I appreciate it if you help me with an example code.

You should create some script that attach on trackable object, maybe something like this.
public class DataBook {
string[] dataBook;
string idText;
bool isActive;
}
Then you must create another script to set that trackable object is active or not, this link can help you to get that.
https://developer.vuforia.com/forum/faq/unity-how-do-i-get-list-active-trackables
Then after you get the active trackable object, you can set the dialog from the book by create another controller script for button, example
public void Next() {
DataBook[] books = FindObjectsOfType<DataBook>(); // if the object more than one, it will be more easy if it only the one
foreach (var book in books)
{
if (book.isActive) {
book.idText += 1;
textUI.text = book.dataBook[idText]; //textUI assign to object text on canvas
}
}
}
you can learn about unity UI Button on this :
https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button
Good luck

Related

Detect controller input in VRTK

I'm kind of new to this so sorry if I'm writing in the wrong place - let me know and I'll move / delete this comment.
I'm currently having issues detecting controller input while using VRTK.
For example, when I have a collision between two objects, I want to be able to detect what buttons are being pressed on the controllers but can't seem to work out how I can do this.
Also, I have implemented the Interact Use functionality but I'm struggling to work out how to make two buttons do different actions.
For example:
once I grab an object with the simple pointer I want one button to
bring the object closer and another to move it away, but I've only
managed to implement one or the other.
Any suggestions? I've looked everywhere in the docs, examples and Google and can't seem to find anything. Any help would be MUCH appreciated! Pulling my hair out here!
You could utilise the Grabbedmethod on the InteractableObject: https://vrtoolkit.readme.io/docs/vrtk_interactableobject#section-grabbed-1
Or you could use the ControllerGrabInteractableObject event on The InteractGrab script: https://vrtoolkit.readme.io/docs/vrtk_interactgrab#section-class-events
Or you could have an Update routine and check the grabbed status on the controller doing GetGrabbedObject() != null (which checks to see if the controller has an object grabbed if it's null then one isn't grabbed).
Then you can use the ControllerEvents button bools to do something on a button press. So a script with this in that sits on the controller script alias gameobject next to the interact grab script:
void Update() {
if (GetComponent<VRTK_InteractGrab>().GetGrabbedObject != null) {
var controllerEvents = GetComponent<VRTK_ControllerEvents>();
if (controllerEvents.IsButtonPressed(VRTK_ControllerEvents.ButtonAlias.Trigger_Press) {
//Do something on trigger press
}
if (controllerEvents.IsButtonPressed(VRTK_ControllerEvents.ButtonAlias.Grip_Press) {
//Do something on grip press
}
}
}

Unity GUI Text won't work

I have the following problem, and I'd appreciate greatly if someone could help.
I've been trying create a roll-a-ball game in Unity, similar as the one presented in one of the tutorials on their official website, but with one difference: I'd like to add GUI text that would show the score. Every time the ball hits (and destroys) one of the rotating cubes, the score should increase by 1, and the text should show the current score.
I've watched the following tutorial:
http://unity3d.com/learn/tutorials/projects/space-shooter/counting-points
I have created the UI text, called ScoreText, and, on the other hand, I have written the script that makes an instance of GUIText class, called guitext, show the current score. However, I cannot connect my particular GUI text (ScoreText) with the script, i.e. I cannot tell the script to use that particular GUI text to show score. In the video, at 16:35, they just drag the GUI text to the instance of the GUIText class from the script (in my case, guitext), in order to tell the script it's that particular text that should show the score. But when I try to drag it, I simply can't do it. I have no idea why.
I don't know if this could cause the problem: under "Create" in the free version of Unity, I could not find anything called "GUI text" but instead I created a UI text. I know it's the same thing, but... In the script, I define an instance of GUIText class - guitext - so maybe, when I try to add my UI text to it, it won't work because my text really doesn't belong to the GUIText class?
Since you are using Unity 4.6 which has a new GUI system and not the Legacy UI which was present when the tutorial which you mentioned was made. There are two options - either you create an empty gameobject and add add a GUIText component to it. You can add the GUIText to empty gameobject by selecting it in the Hierarchy which should display the objects properties in the Inspector panel.
In the Inspector, you should see an Add Component button, click on it and search for GUIText. Add it and that should solve your problem.
Oh yea, the second option is to migrate to Unity 4.6 new GUI, which should take time unless you are pretty good with unity.
If you want to learn Unity 4.6 GUI, you can refer these links
Unity Official
Unity 4.6 tutorials by TheGameContriver
I am only posting on this because many come to this one and get lost. Here is how to make this work in Unity5.3 at the time of this response.
This is in C#
Simply use the GameController game object to attach the script to. Second create a UI text. Toy only call it score or scoretext for your own referral otherwise the name itself does not matter.
using UnityEngine;
using UnityEngine.UI;///needed to reference the new UI setup in this script type
using System.Collections;/// <summary>
/// allows collections of related information.
/// </summary>
public class GameController : MonoBehaviour {
public GameObject targets;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public Text Text; /// <summary>
/// change this because you dont actually use the words UI or GUI. Thats just for the reference and not clarified by instructors.
/// </summary>
private int score;
void Start()
{
score = 0;
UpdateScore();
StartCoroutine(SpawnWaves());
}
IEnumerator SpawnWaves()///this depends on the above using System.Collections
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(targets, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
void UpdateScore()
{
Text.text = "Score: " + score;
}
}
Confirmed. In the GameController.cs file, change the declaration of public GUIText scoreText to public Text scoreText, and then it will let you drag it on the Game Controller as the video shows.
Update your Unity tutorials, peoples! Just spent an hour trying to figure this out!
One more thing...check the Z position. My UI looked perfect head on in the scene view, but my text never showed in the game...then I rotated the scene view and found my text was way behind the camera. Seems when you add something to the UI, the z value is somewhat random.

Get selected text from any application by means of another applicaiton

I'm working on a concept that works like copy & paste but uses my own algorithm instead of using the clipboard.
We have users that use many different programs that contain part numbers. This is how my concept would work.
User highlights part number from any application (word, excel, pdf, JDE, etc)
Either by hotkey or clicking on another application the user launches my routine.
My routine grabs that text from the original application and processes it accordingly.
I know how to use the clipboard to get text.
What I'm not sure of is how to get currently selected text from the application that was active prior to running my code? Do I need to force my user to copy to clipboard first and then run my app or can I create my own copy/paste type windows add-in?
Preferred VB for this but can also use C++ or C# if easier.
As to the question why, because we want to the action to be seamless to the user. There will be several behind the scenes actions that take place and then the user will be presented a browser with pertinent information related to that part number. Ultimately, I don't want an in-between screen to pop up, but rather completely hidden from the user. If I require them to copy and then run my routine then I'm adding in one extra step to the user path that I'm hoping to avoid.
I looked into right click menu context a bit but found that each program can have their own override. I wasn't able to locate a way to globally override all right click context menus to include a new action.
From this article:
var element = AutomationElement.FocusedElement;
if (element != null)
{
object pattern;
if (element.TryGetCurrentPattern(TextPattern.Pattern, out pattern))
{
var tp = (TextPattern) pattern;
var sb = new StringBuilder();
foreach (var r in tp.GetSelection())
{
sb.AppendLine(r.GetText(-1));
}
var selectedText = sb.ToString();
}
}
It's in C# but it should be pretty trivial to translate.

Mono navigation

I just started out with mono and I've already gotten problems. I'm used to play with c# code and was told that mono would be easy for me, but no no. I simply want to start a new activity and close the one i just were using. I checked out some mono API examples, but they are simply too complicated for this task. It has to be some easier way of doing it. This is my first activity class:
[Activity(Label = "CryptotoDroid", MainLauncher = true, Icon = "#drawable/icon")]
public class Activity1 : Activity
{
EditText inputpassword;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
inputpassword = FindViewById<EditText>(Resource.Id.beforetext);
button.Click += delegate
{
if (inputpassword.Text == "Moo")
{
StartActivity(typeof(ActivityContacts));
}
};
}
}
This is what i tried, but the program crashes. I Simply want to make the program to start a new activity when the password is "moo".
The activity I want to start is:
[Activity(Label = "My Activity")]
public class ActivityContacts : Activity
{
protected override void OnCreate(Bundle bundle)
{
SetContentView(Resource.Layout.Main);
var contactgrid = FindViewById<GridView>(Resource.Id.gridview);
}
}
Later on, i would also like to fill out my gridview with all contacts in the phone, but that belongs to another topic.
Did you mean to set both activities' content views to the Main layout?
SetContentView(Resource.Layout.Main);
public void setContentView (View view)
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead.
For starting with MonoDroid, I recommend:
getting hold of one of the excellent books out there (Wally, Greg, Chris or others will be along with suggestions - I've personally yet to see a bad one so won't give a recommendation!)
try watching a couple of the Xaminar's on youTube -http://www.yourepeat.com/g/Xaminar
try building the Xamarin sample programs and then try adapting them - it's sometimes easier to adjust working programs than it is to start new ones (sometimes!)
try building new things just as you have above.
When you hit problems - as we all do - then please do ask questions here or on the Xamarin forums - people will help.
However, when you hit a crash or exception, please try:
Give us as much info as you can about the crash/exception - including there are some ways to get extra debug logs off of a phone or emulator - http://docs.xamarin.com/Android/Guides/Deployment,_Testing,_and_Metrics/Android_Debug_Log - these logs often contain important text which will help us help you diagnose the crash
If you are running under the VS2010, VS2012 or MonoDevelop debugging tools, try adding extra Console.WriteLine statements and/or use breakpoints - this can help you and us work out which line is causing the crash - or if the crash is occurring somewhere in setup before the code even runs.
Speaking personally, I believe Mono for Android does help experience C# devs exploit their skills on Android - but there's still new things to learn, and there are still embedded development frustrations to tackle and overcome (like these sort of crashes)

Microsoft Visual Studio Objects

Hello is it possible to create an object that would function like a button?
Because I'm making a room management system for a hotel and the manager wants me to put a graphical representation of the rooms. I'm thinking it would be user friendly if I create an object that would represent the room e.g (rectangle) because I think it's too awful if I put many buttons in it. (it's too painful in the eyes).
The object should be clickable because when the user clicks or double clicks it. The room details would appear.
Thank you very much...
Yes, it is. All of the controls (as long as they inherit from Control, which basically all UI elements do) have a Click-Event, which you can register to so you get notified when it is clicked.
If you tell me if you are using WinForms or WPF I can give you an example of drawing a custom clickabel object.
You can either just set the size of a button to represent the room, or you can catch the Click event of any element you like to use to represent the room, for example a Panel.
You can also create a class that inherits from a control and implements some more features, that way it's easy to reuse it. Example:
public class Room : Panel {
// perhaps something to keep track of what room it is
private int _id;
// a constructor that sets the data that you need
public Room(int id) {
_id = id;
}
protected override OnClick(EventArgs e) {
// here you can handle the click
}
}
You create an object that would inherit from Control, and do custom drawing code using System.Drawing, It's a pretty simple task. With Control you're exposed to regular events like MouseDown, MouseUp, MouseEnter, MouseLeave, OnPaint, PaintBackground. These are events you're going to want if you add effects.

Resources