1 / 7

Chapter 6

Chapter 6. Cursor Control. HideCursor Script. #pragma strict function Start () { } function Update () { if ( Input.GetButton ("Horizontal") || Input.GetButton ("Vertical") || Input.GetButton ("Turn") || Input.GetButton ("ML Enable") ){

janus
Download Presentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 Cursor Control

  2. HideCursor Script #pragma strict function Start () { } function Update () { if (Input.GetButton("Horizontal") || Input.GetButton("Vertical") || Input.GetButton("Turn") || Input.GetButton("ML Enable") ){ // a navigation key is being pressed, so hide the cursor Screen.showCursor = false; } else { // no navigation keys are being pressed, so show the cursor again Screen.showCursor = true; } }

  3. Custom Cursors • GUI Texture Cursor • MIP Mapping • When an image is copied and reduced in size and clarity to provide blurrier images at greater distances • 2D UI objects do not scale relative to screen size • The Awake function is used to initialize any variables or scene sates before the game starts but after all objects have been created • Screen resolution can be compensated for but it is easier to override users choices

  4. GUITexture Cursor #pragma strict varcontrolCenter : GameObject; internal varmouseOverColor : Color; function Start () { guiTexture.enabled = false; // disable the GUITexture object at start up mouseOverColor= controlCenter.GetComponent(GameManager).mouseOverColor; } function Update () { // gets the current cursor position as a Vector2 type variable varpos = Input.mousePosition; // feed its x and y positions back into the GUI Texture object’s parameters guiTexture.pixelInset.x = pos.x; guiTexture.pixelInset.y = pos.y - 32; // offset to top } function CursorColorChange (colorize: boolean) { if (colorize) guiTexture.color = mouseOverColor; else guiTexture.color = Color.white; }

  5. Object-to-object Communication #pragma strict varmouseOverColor = Color.green; internal var navigating : boolean; // flag for navigation state internal vargamePointer : GameObject; function Awake () { Screen.SetResolution (1280, 800, false); } function Start () { Screen.showCursor = false; // hide the os cursor // assign the actual GamePointer object to the gamePointer variable gamePointer = GameObject.Find("GamePointer"); } function Update () { if (Input.GetButton("ML Enable") || Input.GetButton("Horizontal") || Input.GetButton("Vertical") || Input.GetButton("Turn")) { navigating = true; gamePointer.guiTexture.enabled = false; // turn off the pointer } else { navigating = false; gamePointer.guiTexture.enabled = true; // turn on the pointer } }

  6. Interactor #pragma strict // Gain access to these objects vargamePointer : GameObject; internal var cam : GameObject; // Pick and Mouseover Info internal vartriggerDistance : float = 7.0; // distance the camera must be to the object before mouse over function Start () { // search the scene for an object named “Main Camera” and assign it to the cam var cam = GameObject.Find("Main Camera"); } function OnMouseEnter () { //print (DistanceFromCamera()); if (DistanceFromCamera() > triggerDistance) return; gamePointer.SendMessage("CursorColorChange", true); // colorize the pointer } function OnMouseExit () { gamePointer.SendMessage("CursorColorChange", false); // turn the pointer white } function DistanceFromCamera () { // get the direction the camera is heading so you only process stuff in the line of sight var heading : Vector3 = transform.position - cam.transform.position; //calculate the distance from the camera to the object var distance : float = Vector3.Dot(heading, cam.transform.forward); return distance; }

  7. Summary In this chapter, you experimented with cursor control, finding that you could hide the operating system’s cursor with a simple Screen.showCursor = false. After creating a variable called navigating, which checked your virtual buttons to see if any were active, you were able turn the cursor off while moving and back on as soon as you stopped. To allow for more artistic license, you substituted a GUITexture object as a cursor with the help of the Texture Importer presets. You learned that GUI objects in Unity are not sized relevant to screen size and, so, explored forcing screen resolution via scripting. By getting the operating system’s cursor’s position with Input.mousePosition, you were able to set your own cursor’s Pixel Inset values with guiTexture.pixelInset.x and guiTexture.pixelInset.y. A downside to the GUI Texture cursor was a bit of lag, so you next tried using your own texture for the operating system or hardware cursor. It had great responsiveness and was guaranteed to draw on top of everything else in the scene but was too limited in size and color options. Your final cursor test involved a purely scripted cursor, using the UnityGUI system. It also replaced the operating system cursor, so there was a bit of lag, but it had the ability to have its color and size adjusted, as needed. You learned that it will be able to draw on top of the rest of the scene with no problems. Next, you started designing a script that would hold and manage the master game information and be able to communicate with other object’s scripts. You found that after creating a variable to hold a GameObject, you could either drag one into it in the Inspector or use GameObject.Find in the Start function to locate the object you wanted to communicate with. Names, you discovered, are only properties of objects, materials, and components and representatives of their objects. The same variable names used in different scripts don’t store the same values, unless you specifically update them. With the introduction of OnMouseEnter and OnMouseExit, you were able to instigate color changes for your cursor, as it passed over and off your action objects. By using GetComponent, you found you could access variables from the script components you created, allowing you to keep important data on a single script that could be accessed by all. In the next chapter, you’ll work with imported objects and their animations and start to get them acting like actual game assets.

More Related