UNITY3D学习笔记12

来源:互联网 发布:遂了其事乎 编辑:程序博客网 时间:2024/06/07 06:20




using UnityEngine;using System.Collections;public class Script_Enemy : MonoBehaviour {public const int STATE_STAND = 0;public const int STATE_WALK = 1;public const int STATE_RUN = 2;public const int STATE_PAUSE = 3;private int enemyState;private GameObject hero;private float backUptime;public const int AI_THINK_TIME = 2;public const int AI_ATTACK_DISTANCE = 10;bool showBlood = false;public Texture2D tex_red;public Texture2D tex_black;public Texture2D tex_hp;private int HP = 100;private bool ishatred = false;Object[] texmube;// Use this for initializationvoid Start () {hero = GameObject.Find("/Hero");texmube = Resources.LoadAll("number");enemyState = STATE_STAND;}void OnGUI(){if(showBlood){GUI.DrawTexture(new Rect(5,0,tex_hp.width,tex_hp.height),tex_hp);Tools.DrawImageNumber(200,0,HP,texmube);int blood_width = tex_red.width * HP/100;GUI.DrawTexture(new Rect(5,50,tex_black.width,tex_black.height),tex_black);GUI.DrawTexture(new Rect(5,50,blood_width,tex_red.height),tex_red);}}// Update is called once per framevoid Update () {if(Vector3.Distance(transform.position,hero.transform.position)< AI_ATTACK_DISTANCE || ishatred){gameObject.animation.Play("run");enemyState = STATE_RUN;transform.LookAt(hero.transform);int rand = Random.Range(0,20);if(rand == 0){hero.SendMessage("HeroHurt");}}else{if(Time.time - backUptime >= AI_THINK_TIME){backUptime = Time.time;int rand = Random.Range(0,2);if(rand == 0){gameObject.animation.Play("idle");enemyState = STATE_STAND;}else if(rand == 1){Quaternion rotate = Quaternion.Euler(0,Random.Range(1,5)*90,0);transform.rotation = Quaternion.Slerp(transform.rotation,rotate,Time.deltaTime * 1000);gameObject.animation.Play("walk");enemyState = STATE_WALK;}}}switch(enemyState){case STATE_STAND:break;case STATE_WALK:transform.Translate(Vector3.forward*Time.deltaTime);break;case STATE_RUN:if(Vector3.Distance(transform.position,hero.transform.position)>3){transform.Translate(Vector3.forward * Time.deltaTime * 3);}break;} }void OnMouseDown(){if(HP > 0){HP -= 5;transform.Translate(Vector3.back * 0.1f);ishatred = true;}else{Destroy(gameObject);hero.SendMessage("EnemyHurt");}}void OnMouseUp(){transform.Translate(Vector3.forward * 0.1f);}void OnMouseOver(){showBlood = true;}void OnMouseExit(){showBlood = false;}}



using UnityEngine;using System.Collections;public class Tools : MonoBehaviour {public static void DrawImageNumber(int x,int y,int mumber,Object[] texmube){char[] chars = mumber.ToString().ToCharArray();Texture2D tex = (Texture2D)texmube[0];int width = tex.width;int height = tex.height;foreach(char s in chars){int i = int.Parse(s.ToString());GUI.DrawTexture(new Rect(x,y,width,height),(Texture2D)texmube[i]);x+=width;}}// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}}



using UnityEngine;using System.Collections;public class Script_Game : MonoBehaviour {public const int STATE_GAME = 0;public const int STATE_WIN = 1;public const int STATE_LOSE =2;public enum RotationAxes {MosueXAndY = 0,MouseX=1,MouseY = 2}public RotationAxes axes = RotationAxes.MosueXAndY;float rotationY = 0f;public Texture2D tex_fire;public Texture2D tex_red;public Texture2D tex_black;public Texture2D tex_hp;public Texture2D tex_win;public Texture2D tex_lose;public AudioSource music;public int HP = 100;Object[] texmube;int gameState;// Use this for initializationvoid Start () {Screen.showCursor = false;texmube = Resources.LoadAll("number");gameState  = STATE_GAME;if(rigidbody){rigidbody.freezeRotation = true;}}// Update is called once per framevoid Update () {switch(gameState){case STATE_GAME:UpdateGame();break;case STATE_WIN:case STATE_LOSE:if(Input.GetKey(KeyCode.Escape)){Application.LoadLevel("Scene_Menu");}break;}}void OnGUI(){switch(gameState){case STATE_GAME:RenderGame();break;case STATE_WIN:GUI.DrawTexture(new Rect(0,0,tex_win.width,tex_win.height),tex_win);break;case STATE_LOSE:GUI.DrawTexture(new Rect(0,0,tex_lose.width,tex_lose.height),tex_lose);break;}}void UpdateGame(){if(Input.GetKey(KeyCode.Escape)){Application.LoadLevel("Scene_Menu");}if(axes == RotationAxes.MouseX){transform.Rotate(0,Input.GetAxis("Mouse X"),0);}else{transform.localEulerAngles = new Vector3(-rotationY,transform.localEulerAngles.y,0);}}void RenderGame(){if(tex_fire){float x = Input.mousePosition.x - (tex_fire.width>>1);float y = Input.mousePosition.y - (tex_fire.height>>1);GUI.DrawTexture(new Rect(x,Screen.height - y,tex_fire.width,tex_fire.height),tex_fire);}int blood_width = tex_red.width * HP / 100;GUI.DrawTexture(new Rect(5,Screen.height - 50,tex_black.width,tex_black.height),tex_black);GUI.DrawTexture(new Rect(5,Screen.height - 50,blood_width,tex_red.height),tex_red);GUI.DrawTexture(new Rect(5,Screen.height - 80,tex_hp.width,tex_hp.height),tex_hp);Tools.DrawImageNumber(200,Screen.height - 80,HP,texmube);}void HeroHurt(){HP--;if(HP<=0){HP = 0;gameState = STATE_LOSE;}}void HeroAddBlood(){HP+=50;if(HP>=100){HP = 100;}}void EnemyHurt(){GameObject[] enemy = GameObject.FindGameObjectsWithTag("enemy");if(enemy.Length == 1){gameState = STATE_WIN;}}void OnControllerColliderHit(ControllerColliderHit hit){GameObject collObj = hit.gameObject;if(collObj.name =="Cube"){HeroAddBlood();Destroy(collObj);}}}



using UnityEngine;using System.Collections;[AddComponentMenu("Camera-Control/Mouse Lock")]public class MouseLook : MonoBehaviour {public enum RotationAxes {MouseXAndY = 0,MouseX = 1,MouseY = 2}public RotationAxes axes = RotationAxes.MouseXAndY;float rotationY = 0f;public Texture2D tex_fire;// Use this for initializationvoid Start () {Screen.showCursor = false;if(rigidbody){rigidbody.freezeRotation = true;}}// Update is called once per framevoid Update () {if(axes == RotationAxes.MouseX){transform.Rotate(0,Input.GetAxis("Mouse X"),0);}else{transform.localEulerAngles =new Vector3(-rotationY,transform.localEulerAngles.y,0);}}void OnGUI(){if(tex_fire){float x = Input.mousePosition.x -(tex_fire.width>>1);float y = Input.mousePosition.y +(tex_fire.height>>1);GUI.DrawTexture(new Rect(x,Screen.height - y,tex_fire.width,tex_fire.height),tex_fire);}}}



using UnityEngine;using System.Collections;public class Script : MonoBehaviour {public const int STATE_0 = 0;public const int STATE_1 = 1;public const int STATE_2 = 2;public const int STATE_3 = 3;public const int STATE_4 = 4;private int gameState;// Use this for initializationvoid Start () {gameState = STATE_0;}// Update is called once per framevoid Update () {switch(gameState){case STATE_0:break;case STATE_1:break;case STATE_2:break;case STATE_3:break;case STATE_4:break;}}void OnGUI(){switch(gameState){case STATE_0:break;case STATE_1:break;case STATE_2:break;case STATE_3:break;case STATE_4:break;}}}



using UnityEngine;using System.Collections;public class Test5 : MonoBehaviour {public const int STATE_STAND = 0;public const int STATE_WALK = 1;public const int STATE_RUN = 2;private int enemyState;private GameObject hero;private float backUptime;public const int AI_THINK_TIME = 2;public const int AI_ATTACK_DISTANCE = 10;// Use this for initializationvoid Start () {hero = GameObject.Find("/Hero");enemyState = STATE_STAND;}// Update is called once per framevoid Update () {if(Vector3.Distance(transform.position,hero.transform.position)<AI_ATTACK_DISTANCE){gameObject.animation.Play("run");enemyState = STATE_RUN;transform.LookAt(hero.transform);}else{if(Time.time - backUptime >= AI_THINK_TIME){backUptime = Time.time;int rand = Random.Range(0,2);if(rand == 0){gameObject.animation.Play ("idle");enemyState = STATE_STAND;}else if(rand ==1){Quaternion rotate = Quaternion.Euler(0,Random.Range(1,5)*90,0);transform.rotation = Quaternion.Slerp(transform.rotation,rotate,Time.deltaTime*1000);gameObject.animation.Play ("walk");enemyState = STATE_WALK;}}}switch(enemyState){case STATE_STAND:break;case STATE_WALK:transform.Translate(Vector3.forward*Time.deltaTime);break;case STATE_RUN:if(Vector3.Distance(transform.position,hero.transform.position)>3){transform.Translate(Vector3.forward*Time.deltaTime * 3);}break;}}}



using UnityEngine;using System.Collections;public class Test4 : MonoBehaviour {bool showBlood = false;public Texture2D tex_red;public Texture2D tex_black;private int HP = 100;private GameObject hero;// Use this for initializationvoid Start () {hero = GameObject.Find("/Hero");}// Update is called once per framevoid Update () {transform.LookAt(hero.transform);}void OnGUI(){if(showBlood){int blood_width = tex_red.width * HP/100;GUI.DrawTexture(new Rect(5,5,tex_black.width,tex_black.height),tex_black);GUI.DrawTexture(new Rect(5,5,blood_width,tex_red.height),tex_red);}}void OnMouseDown(){if(HP >0){HP -= 5;transform.Translate(Vector3.back * 0.1f);}else{Destroy(gameObject);}}void OnMouseUp(){transform.Translate(Vector3.forward*0.1f);}void OnMouseOver(){showBlood = true;}void OnMouseExit(){showBlood = false;}}



using UnityEngine;using System.Collections;public class Test3 : MonoBehaviour {Object[] texmube;int mumber = 1980;// Use this for initializationvoid Start () {texmube = Resources.LoadAll("Textures");}// Update is called once per framevoid Update () {}void OnGUI(){DrawImageNumber(0,100,mumber,texmube);}void DrawImageNumber(int x,int y,int number,Object[] texmube){char[] chars = mumber.ToString().ToCharArray();Texture2D tex = (Texture2D)texmube[0];int width = tex.width;int height = tex.height;foreach(char s in chars){int i = int.Parse(s.ToString());GUI.DrawTexture(new Rect(x,0,width,height),(Texture2D)texmube[i]);x+=width;}}}



using UnityEngine;using System.Collections;public class Test2 : MonoBehaviour {public Texture2D blood_red;public Texture2D blood_black;private int HP = 100;// Use this for initializationvoid Start () { }// Update is called once per framevoid Update () { }void OnGUI(){if(GUILayout.RepeatButton("add HP")){if(HP<100){HP++;}}if(GUILayout.RepeatButton("div HP")){if(HP>0){HP--;}}int blood_width = blood_red.width * HP/100;GUI.DrawTexture(new Rect(100,100,blood_black.width,blood_black.height),blood_black);GUI.DrawTexture(new Rect(100,100,blood_width,blood_red.height),blood_red);}}



using UnityEngine;using System.Collections;public class Test1 : MonoBehaviour { //Script_10_01public const int STATE_MAINMENU = 0;public const int STATE_STARTGAME = 1;public const int STATE_OPTION = 2;public const int STATE_HELP = 3;public const int STATE_EXIT = 4;public GUISkin mySkin;public Texture textureBG;public Texture tex_startInfo;public Texture tex_helpInfo;public AudioSource music;private int gameState;// Use this for initializationvoid Start () {gameState = STATE_MAINMENU;}// Update is called once per framevoid Update () {}void OnGUI(){switch(gameState){case STATE_MAINMENU:RenderMainMenu();break;case STATE_STARTGAME:RenderStart();break;case STATE_OPTION:RenderOption();break;case STATE_HELP:RenderHelp();break;case STATE_EXIT:Application.Quit();break;}}void RenderMainMenu(){GUI.skin = mySkin;GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),textureBG);if(GUI.Button(new Rect(0,30,623,153),"","start")){gameState = STATE_STARTGAME;}if(GUI.Button(new Rect(0,180,623,153),"","option")){gameState = STATE_OPTION;}if(GUI.Button(new Rect(0,320,623,153),"","help")){gameState = STATE_HELP;}if(GUI.Button(new Rect(0,470,623,153),"","exit")){Application.Quit();}}void RenderStart(){GUI.skin = mySkin;GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),tex_startInfo);if(GUI.Button(new Rect(0,500,403,78),"","back")){gameState = STATE_MAINMENU;}}void RenderHelp(){GUI.skin = mySkin;GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),tex_helpInfo);if(GUI.Button(new Rect(0,500,403,78),"","back")){gameState = STATE_MAINMENU;}}void RenderOption(){GUI.skin = mySkin;GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),textureBG);if(GUI.Button(new Rect(0,0,403,75),"","music_on")){if(!music.isPlaying){music.Play();}}if(GUI.Button(new Rect(0,200,403,75),"","music_off")){music.Stop();}if(GUI.Button(new Rect(0,500,403,78),"","back")){gameState = STATE_MAINMENU;}}}


0 0