当前位置:   article > 正文

unity3d功能脚本大全_gui.drawtexture(new rect(0, 0, screen.width, scree

gui.drawtexture(new rect(0, 0, screen.width, screen.height), image);

1.右键菜单

  1. function OnGUI(){
  2. if(Input.GetMouseButton(1))
  3. {
  4. GUILayout.BeginArea (new Rect (Input.mousePosition.x,Screen.height-Input.mousePosition.y,200,200));
  5. GUILayout.Box("This is a Context Menu");
  6. GUILayout.EndArea ();
  7. }
  8. }
  9. /*
  10. if(Input.GetMouseButton(0))
  11. Debug.Log("Pressed left click.");
  12. if(Input.GetMouseButton(1))
  13. Debug.Log("Pressed right click.");
  14. if(Input.GetMouseButton(2))
  15. Debug.Log("Pressed middle click.");
  16. */

2.用辅助键/双键控制视角

  1. using UnityEngine;
  2. using System.Collections;
  3. public class ControlCamerMove : MonoBehaviour {
  4. void Update () {
  5. if(Input.GetKeyDown(KeyCode.LeftControl)){
  6. this.GetComponent<MouseLook>().enabled = false;
  7. }
  8. if(Input.GetKeyUp(KeyCode.LeftControl)){
  9. this.GetComponent<MouseLook>().enabled = true;
  10. }
  11. }
  12. }

3.与网页交互-打开网页

  1. function OnGUI()
  2. {
  3. GUILayout.Label("当前场景:"+ Application.loadedLevelName);
  4. if(GUILayout.Button("打开网页"))
  5. Application.OpenURL("www.baidu.com");
  6. }

4.在unity中播放视频--绘制

  1. #pragma strict
  2. var movTexture:MovieTexture;
  3. function Start ()
  4. {
  5. movTexture.loop = true;
  6. }
  7. function OnGUI()
  8. {
  9. GUI.DrawTexture(new Rect (0,0,Screen.width,Screen.height),movTexture,ScaleMode.StretchToFill);
  10. if(GUILayout.Button("播放/继续"))
  11. {
  12. if (!movTexture.isPlaying)
  13. {
  14. movTexture.Play();
  15. }
  16. }
  17. if(GUILayout.Button("暂停播放"))
  18. {
  19. movTexture.Pause();
  20. }
  21. if(GUILayout.Button("停止播放"))
  22. { 贴图
  23. movTexture.Stop();
  24. }
  25. }

5.在unity中播放视频--作为plane的材

  1. #pragma strict
  2. var movTexture:MovieTexture;
  3. function Start ()
  4. {
  5. this.renderer.material.mainTexture = movTexture;
  6. movTexture.loop = true;
  7. }
  8. function OnGUI()
  9. {
  10. if(GUILayout.Button("播放/继续"))
  11. {
  12. if (!movTexture.isPlaying)
  13. {
  14. movTexture.Play();
  15. }
  16. }
  17. if(GUILayout.Button("暂停播放"))
  18. {
  19. movTexture.Pause();
  20. }
  21. if(GUILayout.Button("停止播放"))
  22. {
  23. movTexture.Stop();
  24. }
  25. }

6.用材质偏移模拟水流效果

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Whatflow : MonoBehaviour {
  4. public float scrollSpeed=3;
  5. void Update () {
  6. float offset = Time.time*scrollSpeed;
  7. renderer.material.SetTextureOffset("_MainTex",new Vector2(0,offset));
  8. }
  9. }

7.从网页上获取一张图片并动态使用

  1. var url = "http://img.sucai.redocn.com/attachments/images/201011/20101110/20101109_da85542f8635f3c5b963vil1B8qeeS9O.jpg";
  2. function Start () {
  3. var www : WWW = new WWW (url);
  4. //定义www为WWW类型并且等于被下载的内容。
  5. yield www;
  6. //等待www完全下载。
  7. this.renderer.material.mainTexture = www.texture;
  8. //将下载下来的WWW中的图片赋予到默认物体的材质上进行渲染出来
  9. }

8.点击物体使其变为半透明

  1. //要将物体的shader方式改为Tansparent/Diffuse,才可以实现点击时变为半透明
  2. function Start()
  3. {
  4. this.renderer.material.shader = Shader.Find("Transparent/Diffuse");
  5. }
  6. function OnMouseDown () {
  7. this.renderer.material.color.a=0.5;
  8. }
  9. function OnMouseUp() {
  10. if(this.renderer.material.color.a!=1)
  11. {
  12. this.renderer.material.color.a=1;
  13. }
  14. }

9.控制物体移动(3种)

  1. #pragma strict
  2. var movespeed = 20;
  3. //1.键盘控制,按W/A/S/D可以朝着不同方向移动
  4. function Update () {
  5. if(Input.GetKey(KeyCode.W))
  6. {
  7. Debug.Log("W向前移动");
  8. this.transform .Translate(Vector3.forward*Time.deltaTime*movespeed);
  9. }
  10. if(Input.GetKey(KeyCode.S))
  11. {
  12. Debug.Log("S向后移动");
  13. this.transform .Translate(-Vector3.forward*Time.deltaTime*movespeed);
  14. }
  15. if(Input.GetKey(KeyCode.A))
  16. {
  17. Debug.Log("A向左移动");
  18. this.transform .Translate(Vector3.left*Time.deltaTime*movespeed);
  19. }
  20. if (Input.GetKey(KeyCode.D))
  21. {
  22. Debug.Log("D向右移动");
  23. this.transform .Translate(Vector3.right*Time.deltaTime*movespeed);
  24. }
  25. if(Input.GetKey(KeyCode.UpArrow))
  26. {
  27. Debug.Log("向前移动");
  28. this.transform .Translate(Vector3.forward*Time.deltaTime*movespeed);
  29. }
  30. if(Input.GetKey(KeyCode.DownArrow))
  31. {
  32. Debug.Log("向后移动");
  33. this.transform .Translate(-Vector3.forward*Time.deltaTime*movespeed);
  34. }
  35. if(Input.GetKey(KeyCode.LeftArrow))
  36. {
  37. Debug.Log("向左移动");
  38. this.transform .Translate(Vector3.left*Time.deltaTime*movespeed);
  39. }
  40. if(Input.GetKey(KeyCode.RightArrow))
  41. {
  42. Debug.Log("向右移动");
  43. this.transform .Translate(Vector3.right*Time.deltaTime*movespeed);
  44. }
  45. }
  46. //2.在物体上按下鼠标,物体朝着某给定方向移动
  47. function OnMouseUp() {
  48. this.transform .Translate(Vector3.left * Time.deltaTime * movespeed);
  49. }
  50. //3.添加按钮,通过按钮实现移动
  51. function OnGUI()
  52. {
  53. if(GUILayout.Button("向前移动", GUILayout.Height(50)))
  54. this.transform.Translate(Vector3.forward*Time.deltaTime*movespeed);
  55. if(GUILayout.Button("向后移动", GUILayout.Height(50)))
  56. this.transform.Translate(-Vector3.forward*Time.deltaTime*movespeed);
  57. if(GUILayout.Button("向左移动", GUILayout.Height(50)))
  58. this.transform.Translate(Vector3.left*Time.deltaTime*movespeed);
  59. if(GUILayout.Button("向右移动", GUILayout.Height(50)))
  60. this.transform.Translate(Vector3.right*Time.deltaTime*movespeed);
  61. GUILayout.Label("立方体的位置:"+this.transform .position);
  62. }

10.音频控制

  1. using UnityEngine;
  2. using System.Collections;
  3. public class sound : MonoBehaviour {
  4. // Use this for initialization
  5. void Start () {
  6. }
  7. // Update is called once per frame
  8. void Update () {
  9. }
  10. void OnGUI() {
  11. // 这个函数会自动触发,在游戏界面上启动按钮之类的用户界面。
  12. // 下面的代码就顾名思义的吧。自己尝试一下就知道了。
  13. if (GUI.Button(new Rect(0, 60, 100, 50), "Play"))
  14. audio.Play();
  15. if (GUI.Button(new Rect(0, 120, 100, 50), "Pause"))
  16. audio.Pause();
  17. if (GUI.Button(new Rect(0, 180, 100, 50), "Stop"))
  18. audio.Stop();
  19. }
  20. }

11.自定义控制键

  1. #pragma strict
  2. var my_key:KeyCode;
  3. function Start () {
  4. my_key = KeyCode.T;
  5. }
  6. function Update () {
  7. if (Input.GetKeyDown(my_key))
  8. {
  9. this.transform.position.y +=2;
  10. }
  11. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/122511
推荐阅读
相关标签
  

闽ICP备14008679号