Developing with Unity and Vuforia

Learning Unity

Unity Documentation

Unity Tips

  • Unity provides a Virtual Joystick in the Standard Assets (Mobile) package that you can use on your mobile device.
    • To use it, import it using: (Menu bar) Assets→Import Package→Standard Assets (Mobile)
      • Then after it is imported, (Project View) Assets→Standard Assets (Mobile)→Prefabs→Single Joystick.
    • Unfortunately, while it is very useful, it is not documented. Please follow the instructions below to use it in your scene.
    • The package needs to be moved from the Standard Assets (Mobile) folder in order to prevent an in-editor compilation issue.
      • Before starting, you will want to create a new folder in Assets called Standard Assets .
      • Move all the folders in the Standard Assets (Mobile) directory into the Standard Assets directory you just created.
    • The Virtual Joystick is packaged as a Prefab and can be found in the Prefabs folder under Assets→Standard Assets →Prefabs
    • Drag the Single Joystick prefab to the Hierarchy View or the Scene View.
    • This will add the Virtual Joystick to the scene.
      • Its position on screen is controlled by the X and Y coordinates in its Transform component.
    • It can be accessed in the same way as any other GameObject Component.
    • It is possible that MonoDevelop will not recognize the class. If this occurs, building the script in MonoDevelop will result in a compilation error.
      • You can ignore the MonoDevelop compilation error, and for this script, rely on Unity's compilation.
        • To determine if Unity has thrown a compilation error for that script, check the Console View after saving your script in MonoDevelop.
    • To use it in your script, bind the component to a variable using the Joystick class.
      • The Joystick class has a Vector2 member variable called “position,” that will give you x and y values between -1 and 1 representing normalized offsets from the center of the joystick.
        • Example: currentX = aJoystickExample.position.x
    • Please note that the Joystick class, as provided by Unity, will not normally function in Play Mode.
      • You can extend the functionality of the Joystick class (written in JavaScript) in order to add support for mouse-based input to the Joystick GameObject.
  • GUILayout
    • Horizontal Sliders
      • If using GUILayout.HorizontalSlider:
        • You can set the width and height of the control manually.
          • Example: GUILayout.HorizontalSlider (sliderValue, minValue, maxValue, GUILayout.Width (300), GUILayout.Height(200));
        • The width parameter will stretch the slider horizontally.
        • The height parameter will not increase the visible height of the slider, but instead affect the surface area of the control, giving you a larger input area in which a mouse/touch event will be recorded for the control.
          • Note: with a standard GUIStyle, the default alignment rules will be used. Thus, as you increase the height, you will see an greater amount of empty space below the slider, but the slider itself will remain aligned to the top left of the control area. Mouse/Touch input events will be recorded in that empty space and still be applied to the slider.
            • Using GUILayout.FlexibleSpace() helps to align the object within the enclosing container/area but will not adjust the alignment of the slider within its own control.
            • To modify the alignment, you need to set the GUIStyle parameters for the horizontal slider and thumb in the GUILayout.HorizontalSlider function. See the GUILayout.HorizontalSlider documentation for additional details.
    • Vertical Sliders
      • Vertical Sliders can be modified similarly to the horizontal slider above, but the effect of the width and height parameters will be different.
        • Setting the width will increase the horizontal input area but not stretch the control horizontally.
        • Setting the height will stretch the control vertically.
    • Buttons
      • Button size can also be set manually using the width and height parameters in the control's function.
        • Use GUILayout.Width and GUILayout.Height to increase the size of the button.
        • Example: GUILayout.Button(“MyButton”, GUILayout.Width(100), GUILayout.Height(100));
    • Fonts:
      • You can change the size and color of the font used in GUI and GUILayout components by passing a GUIStyle parameter to the particular component's function.
      • First, declare a GUIStyle variable.
        • GUIStyle myStyle = new GUIStyle();
      • Second, define font size:
        • myStyle.fontSize = 50;
      • Third (optional), set the color of the font:
        • myStyle.normal.textColor = Color.White;
      • Fourth, pass the GUIStyle to a GUI or GUILayout object
        • GUILayout.Label(myLabel, myStyle);
        • Or, when using optional layout parameters: GUILayout.Label(myLabel, myStyle, GUILayout.Width (40), GUILayout.Height(30));
    • 4172 Students:
      • GUILayoutUtils.cs (updated version uploaded to CourseWorks on 02/20/2014).
        • GUILayoutUtils provides functions to facilitate creation of a Label/Slider/Text Input group.
          • GUILayoutUtils.HorizontalSliderAndText will create the Label/Slider/Text Input group using a horizontal slider.
          • GUILayoutUtils.VerticalSliderAndText will create the Label/Slider/Text Input group using a vertical slider.
        • If you want to modify the size of any element of the group, set the appropriate static variable.
          • Example: Set the horizontal slider width to exactly 100 pixels:
            • GUILayoutUtils.HSliderWidth = 100;
      • The updated code (02/20/2014) removed the FlexibleSpace calls and added additional static member variables to allow for greater control in the creation of the control group.
    • Porting Tips:
      • You may find that you will need to adjust the sizes of your GUI controls when porting your application to a mobile device.
        • Increase the size of your controls using the suggested approaches in the HorizontalSlider, VerticalSlider, Button, and GUILayoutUtils guides above.
        • Increase the font size of your labels using the approach described in the font size guide above.
        • Choose values that will allow easy access to controls without obscuring important objects in the scene. Finding the right balance is crucial to creating a usable interface on a mobile device.
        • Consider the resolution of your device's display when generating your GUI:
  • Raycast - GUI
    • When using Unity's built in Raycast function and a GUI component at the same time, you will find that rays will still be cast as you manipulate the GUI control.
      • You can prevent this behavior in a number of ways:
        • When checking if the mouse was pressed, also check if the value of GUIUtility.hotControl is 0.
          • If the value is set to 0, the mouse did not overlap a GUI element when it was pressed.
          • This will only work with an OnMouseDown event.
          • This will not work for a GUILayout/GUI window.
        • You can also determine if the mouse was overlapping the GUI/GUILayout rect:
  • Reparenting a GameObject
    • If you want to change the parent of a GameObject in a script, you can assign the transform component of the new parent to the Transform.parent member variable of the child GameObject.
    • See Transform.parent for more details.
  • Quality Settings
    • Unity allows you to set the graphical quality of its rendering pipeline. Quality settings are customizable and can be enabled/disabled for any platform Unity supports.
      • The Quality Settings Inspector panel can be opened by going to Edit→Project Settings→Quality.
      • At the top of the Quality Settings Inspector panel you will find a matrix of check boxes.
        • The rows of the matrix represent the quality setting.
        • The columns of the matrix represent the platform.
      • To change the default quality setting, select the small black triangle in the default row under the platform you wish to deploy to, and in the drop down list, select the quality setting you want to use as default.
      • Below the settings matrix, you will find options that define the settings for the currently selected row in the matrix.
      • The Quality settings are broken into three categories, Rendering, Shadows and Other.
        • Please see Unity reference page on Quality Settings for a detailed breakdown of each setting.
  • Physics Settings.
    • Unity has a robust physics engine, but you may need to adjust the default settings in order to tune performance to your particular needs.
    • To open the Physics Manager Inspector Panel, go to Edit→Project Settings→Physics.
    • Please see the Unity reference page on the Physics manager for a detailed breakdown of each setting.
    • Important Notes:
      • Gravity:
        • You may need to change the Gravity setting when using Tracking Targets in Vuforia.
          • Determine which axis will represent “down” in your system and change the magnitude and axis in the Gravity setting here.
        • The Bounce threshold will determine at what relative velocity bouncing will stop.
          • Setting the value too high may stop bouncing top quickly.
          • Setting the value too low may result in endless bouncing.
          • Determine what settings are appropriate for your system through experimentation.
      • Min Penetration for Penality
        • This sets how many meters two colliding GameObjects may overlap before the physics engine pushes them apart.
        • As with the Bounce threshold, determine what is right for your system through experimentation.
  • Physics Materials
    • Unity allows you modify the effect of friction and “bounciness” on colliding GameObjects through Physics Materials.
      • Physics Materials must be imported, as they are not included by default.
      • To add Physics Materials to your project, go to Assets→Import Package→Physics Materials
        • When the Importing Package dialog window opens, choose the assets you want to import and press Import.
        • The Materials will be added to the (Project View) Assets→Standard Assets→Physics Materials directory,
      • You can change the settings of a Physics Material by selecting the material in Project View and editing the properties in the Inspector.
      • To add a Physics Material to a GameObject:
        • Drag the Physics Material onto the GameObject name in the Hierarchy View,
        • Drag the Physics Material onto the GameObject in the Scene View, or
        • Drag the Physics Material into (Inspector Panel) GameObject Inspector→Collider Component→Material.
          • If your GameObject does not have a Collider component, you will need to add one.
    • See the Unity Reference page on Physics Materials for additional details.
  • Managing Project size
    • The Unity Project may become very large as packages and assets are added.
    • Consider removing unneeded packages.
    • However, ensure that asset chains and required Prefabs are not removed.
      • Backup your project before attempting to clean it up.
  • Networking
    • Unity has builtin networking support.
    • Networking can be handled in various ways, and your implementation might call for a specific approach.
    • General Networking overview:
      • See the Unity Reference Guide on Networking.
      • More details coming soon.
    • .NET Sockets.
      • More details coming soon.
    • Networking on mobile devices.
      • More details coming soon.
    • Pitfalls:
      • More details coming soon.



Learning Vuforia




Personal Tools