Unity3D basics, unity3d

Source: Internet
Author: User

Unity3D basics, unity3d

Mouse event:

OnMouseEnter (): enter with the mouse

OnMouseExit (): move the mouse out

OnMouseDown (): Click

OnMouseUp (): move the mouse up

Static GameObject Instantiate () Clone

Static void Destroy ()

 

Position, material, collision, rendering

Terrain, pre-made, camera, collision

 

Using UnityEngine; using System. collections; public class shubiao: MonoBehaviour {// Use this for initialization void Start () {}// Update is called once per frame void Update (){}//: enter void OnMouseEnter () {gameObject. renderer. material. color = Color. black;} void OnMouseExit () {gameObject. renderer. material. color = Color. blue ;}//: move the mouse out of void OnMouseDown () {gameObject. renderer. material. color = Color. yellow;} //: Click void OnMouseUp () {gameObject. renderer. material. color = Color. red; GameObject. destroy (gameObject);} //: move the mouse up}

GUI Layout

Using UnityEngine; using System. collections; public class GIU: MonoBehaviour {private string str = "zhangsan"; private string str2 = ""; private bool sex = false; private string _ userName; // Use this for initialization void Start () {}// Update is called once per frame void Update () {} void OnGUI () {/* if (GUI. button (new Rect (10, 10, 70, 20), "Button") {print ("A");} if (GUI. repeatButton (new Rect (10, 100, 70, 20), "button") {print ("B") ;}str = GUI. textArea (new Rect (10, 20,100, 20), str); str2 = GUI. passwordField (new Rect (10,150,200, 20), str2, '*'); sex = GUI. toggle (new Rect (10,100, 50, 50), sex, "male"); // check */GuI. label (new Rect (10, 10,), "User Name"); _ userName = GUI. textArea (new Rect (100, 10,200, 20), _ userName); _ sex = GUI. toggle (new Rect (10, 70, 50, 20), _ sex, "male"); _ sex = GUI. toggle (new R Ect (10,140, 50, 20 ),! _ Sex, "female ");}}

GUILayOut Layout

Example 1: private string str3 = "tested string"; // global variable
GUILayout. button ("GUILayout Button"); if (GUILayout. button (str3) {str3 + = str3;} if (GUI. button (new Rect (50,100,100,100), str) {str3 + = str3 ;}

 

Example 2: GUILayout. beginHorizontal (); // horizontally arrange GUILayout. button ("I am GUILayoutButton"); GUILayout. button ("I am GUILayoutButton"); GUILayout. button ("I am GUILayoutButton"); GUILayout. button ("I am GUILayoutButton"); GUILayout. endHorizontal (); GUILayout. beginVertical (); // vertically arranged GUILayout. button ("I am GUILayoutButton"); GUILayout. button ("I am GUILayoutButton"); GUILayout. button ("I am GUILayoutButton"); GUILayout. button ("I am GUILayoutButton"); GUILayout. endVertical ();

Move w key A key S key D key:

Using UnityEngine; using System. collections; public class fouth: MonoBehaviour {// The right side is the Z axis, the top is the Y axis, and the X axis is invisible; GameObject go; // Use this for initialization void Start () {go = GameObject. find ("Cube3"); go. renderer. material. color = Color. red;} // Update is called once per frame void Update () {if (Input. getKey (KeyCode. w) {go. transform. translate (-5 * Time. deltaTime, 0, 0, Space. self);} if (Input. getKey (KeyCode. s) {go. transform. translate (5 * Time. deltaTime, 0, 0, Space. self);} if (Input. getKey (KeyCode. a) {go. transform. translate (0, 0,-5 * Time. deltaTime, Space. self);} if (Input. getKey (KeyCode. d) {go. transform. translate (0, 0, 5 * Time. deltaTime, Space. self );}}}

Rotation:

Using UnityEngine; using System. collections; public class NewBehaviourScript: MonoBehaviour {// Use this for initialization void Start () {}// Update is called once per frame void Update () {// rotate gameObject. transform. rotate (0, 15 * Time. deltaTime, 0, Space. self );}}

Collision:

void OnCollisionEnter(Collision co)    {        co.gameObject.renderer.material.color = Color.blue;    }

Pre-fabricated (clone ):

void Update () {        if (Input.GetKeyUp (KeyCode.Space)) {            GameObject.Instantiate(go,gameObject.transform.position,gameObject.transform.rotation);                }    }
Void Update () {if (Input. getKey (KeyCode. leftShift) {if (Input. getKey (KeyCode. leftShift) {gameObject. animation. play ("run"); gameObject. transform. transform (Vector3.forward * Time. deltaTime * 6); // forward and backward movement Distance Speed} else {gameObject. animation. play ("walk"); gameObject. transform. transform (Vector3.forward * Time. deltaTime * 0); // forward and backward movement Distance Speed} else {gameObject. animation. play ("stand"); gameObject. transform. transform (Vector3.forward * Time. deltaTime * 3); // forward and backward movement speed }}
 

 

 

Shadow baking: sets a shadow for the lamp, sets the rest for the baking, and enables the windows light baking back;

Animation Editing: None

C # animation control:

 

Application

Switch scenario

  if (Input.GetKeyDown (KeyCode.A)) {            Application.LoadLevel(0);                }

Screenshots

if (Input.GetKeyDown (KeyCode.Space)) {            Application.CaptureScreenshot(@"c:\1.png")                }

Open a website

Application. OpenURL ("H"); // open a URL

Exit:

Application. Quit (); // exit

Left-click the game object to disappear

If (Input. getMouseButtonDown (0) // If you press left to hit {Ray ray = camera. main. screenPointToRay (Input. mousePosition); // get the clicked position RaycastHit hitInfo; if (Physics. raycast (ray, out hitInfo) // click {Destroy (hitInfo. collider. gameObject); // destroy the game object }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.